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

10
  • did you change the queue driver to something other than 'sync' ? Commented Dec 30, 2015 at 5:59
  • i followed 1.php artisan queue:table 2.php artisan make:job SendStudentRequirement --queued thats it , i didnt touched queue driver and all Commented Dec 30, 2015 at 6:06
  • did you try Mail::queue instead of mail() ? Commented Dec 30, 2015 at 6:07
  • Mail queue needs some third party mail sending so i used normal mail Commented Dec 30, 2015 at 6:12
  • 1
    If you changed QUEUE_DRIVER to database, have you run php artisan config:cache to create new cached config file ? Commented Dec 30, 2015 at 6:47

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

QUEUE_DRIVER=database i changed to this , eventhough the same problem
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

1 Comment

@codiiv, Was a jobs table automatically created with the information of the queue? (Step 2)
0

use this laravel queue function :- dispatchAfterResponse

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.