3

hi i created a laravel queue job to send mails

public function handle() {
    foreach($this->emails as $value) {
            $to         = $value->email;
            $subject    = $this->data['subject'];       
            $this->data['t_firstname']    = $value->firstname;
            $this->data['t_lastname']     = $value->lastname;
            if (view()->exists('mail.requirement_to_tutor')) {
                    $view = view('mail.requirement_to_tutor',$this->data);
                    $html = $view->render();
            }
            file_put_contents('test.txt', 'test database');
            $body = $html;
            $headers  = "From: " . $this->data['from'] . "\r\nReply-To: " . $this->data['from'] . "";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset: utf8\r\n";
            mail($to, $subject, $body, $headers);
    }
}

and also i am pushing datas from repo

$obj = (new SendStudentRequirement($TutorsbyCity,$data));
$this->dispatch($obj);

but it doesnot run as background , the function is waiting untill the queue finish , help me out please

  • did you change the queue driver to something other than 'sync' ? – lagbox Dec 30 '15 at 05:59
  • i followed 1.php artisan queue:table 2.php artisan make:job SendStudentRequirement --queued thats it , i didnt touched queue driver and all –  Dec 30 '15 at 06:06
  • did you try Mail::queue instead of mail() ? – naneri Dec 30 '15 at 06:07
  • Mail queue needs some third party mail sending so i used normal mail –  Dec 30 '15 at 06:12
  • do you have any queue listeners running ? – lagbox Dec 30 '15 at 06:43
  • 1
    If you changed `QUEUE_DRIVER` to `database`, have you run `php artisan config:cache` to create new cached config file ? – Marcin Nabiałek Dec 30 '15 at 06:47
  • i am using database queue and it created a row for each process with attempts = 0, reserved = o, i am looking forward for how to work with queue listener , i am new to laravel thats why lot of confusion , now i think i am almost 90 % to accomplish the task –  Dec 30 '15 at 06:48
  • @MarcinNabiałek yes run that artisan command to clear cache. –  Dec 30 '15 at 06:51
  • Have you changed this config (and cache again) before or after running queue listener? If before, you should stop the listener and start it again to make sure it's using latest config. – Marcin Nabiałek Dec 30 '15 at 06:54
  • everything works fine , and may i know how to automatically process the queue listener –  Dec 30 '15 at 07:08

3 Answers3

5

By default the sync driver is used. You should change this to another driver that is listed in config/queue.php

Look for the following line in your .env file and adjust to a different driver:

QUEUE_DRIVER=sync

Laravel - Docs - Queues

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • 1
    QUEUE_DRIVER=database i changed to this , eventhough the same problem –  Dec 30 '15 at 06:39
0

You are using a default configuration of "sync", that means that all queue jobs will run synchronously instead of "fire and forget" way. To change this default behavior you can follow these steps:

1. Select a diferent Queue Connection

Open the .env configuration file and add QUEUE_DRIVER parameter with one of the supported values: "database", "beanstalkd", "sqs" or "redis". In this case we are going to use a database connection as an example mode: QUEUE_DRIVER=database

2. Edit connection driver

Open the /config/queue.php file and configure your driver connection, for example:

'database' => [
            'driver' => 'mongodb',
            'table' => 'jobs',
            'queue' => 'default',
            'retry_after' => 90
        ]

Then run the next commands to create the new jobs table:

php artisan config:cache && php artisan queue:table

So, you already have the queue as "fire and forget" way; you can try and see how the jobs table was created with the information of the queue.

3. Configure a process manager for a long-running

To execute the pending queues in the future we can use a process manager as supervisor. You can install supervisor for Ubuntu and Debian running the following command: sudo apt-get install supervisor

Then open the supervisor file: sudo nano /etc/supervisor/supervisord.conf and add a line like the following to the end of the file:

[program:laravel-worker-QUEUE_NAME]
process_name=%(program_name)s_%(process_num)02d
command= php /var/www/MY_PROJECT/artisan queue:work --queue=QUEUE_NAME --sleep=15
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/MY_PROJECT/storage/logs/worker.log

Replace the values of: QUEUE_NAME and MY_PROJECT. Note that the --sleep parameter is the time (seconds) to verify for new queues in the database. You can see more details of the configuration file in the official documentation.

Finally execute these commands to enable the program:

sudo supervisorctl reread && sudo supervisorctl update

You can check the status of the queues in the configured log file: /var/www/MY_PROJECT/storage/logs/worker.log

J.C. Gras
  • 4,934
  • 1
  • 37
  • 44
0

use this laravel queue function :- dispatchAfterResponse

for eg : $obj = (new SendStudentRequirement($TutorsbyCity,$data)); $this->dispatchAfterResponse($obj);