[英]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
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: 若要更改此默认行为,您可以按照以下步骤操作:
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
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
表。
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_NAME
和MY_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.