Laravel Queue doesnt run as background

2019-07-18 04:38发布

问题:

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

回答1:

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



回答2:

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