简体   繁体   English

Laravel队列未作为后台运行

[英]Laravel Queue doesnt run as background

hi i created a laravel queue job to send mails 嗨,我创建了一个laravel队列作业来发送邮件

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 但是它没有作为后台运行,功能正在等待队列完成,请帮帮我

By default the sync driver is used. 默认情况下,使用同步驱动程序。 You should change this to another driver that is listed in config/queue.php 您应该将此更改为config/queue.php列出的另一个驱动程序

Look for the following line in your .env file and adjust to a different driver: 在您的.env文件中查找以下行,并调整为其他驱动程序:

QUEUE_DRIVER=sync

Laravel - Docs - Queues Laravel-文件-Queue列

You are using a default configuration of "sync" , that means that all queue jobs will run synchronously instead of "fire and forget" way. 您正在使用默认配置"sync" ,这意味着所有队列作业将同步运行,而不是“即发即弃”的方式。 To change this default behavior you can follow these steps: 若要更改此默认行为,您可以按照以下步骤操作:

1. Select a diferent Queue Connection 1.选择一个不同的队列连接

Open the .env configuration file and add QUEUE_DRIVER parameter with one of the supported values: "database", "beanstalkd", "sqs" or "redis". 打开.env配置文件,并使用支持的值之一添加QUEUE_DRIVER参数:“数据库”,“ beanstalkd”,“ sqs”或“ redis”。 In this case we are going to use a database connection as an example mode: QUEUE_DRIVER=database 在这种情况下,我们将使用数据库连接作为示例模式: QUEUE_DRIVER=database

2. Edit connection driver 2.编辑连接驱动程序

Open the /config/queue.php file and configure your driver connection, for example: 打开/config/queue.php文件并配置驱动程序连接,例如:

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

Then run the next commands to create the new jobs table: 然后运行以下命令来创建新的jobs表:

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. 您可以尝试查看如何使用队列信息创建jobs表。

3. Configure a process manager for a long-running 3.为长期运行配置流程管理器

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 您可以运行以下命令为Ubuntu和Debian安装超级用户: 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: 然后打开sudo nano /etc/supervisor/supervisord.conf文件: sudo nano /etc/supervisor/supervisord.conf并在文件末尾添加以下内容:

[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 . 替换以下值: QUEUE_NAMEMY_PROJECT Note that the --sleep parameter is the time (seconds) to verify for new queues in the database. 请注意, --sleep参数是验证数据库中新队列的时间(秒)。 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 您可以在配置的日志文件中检查队列的状态: /var/www/MY_PROJECT/storage/logs/worker.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM