Laravel - 自动执行排队作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50967427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Execute queued job automatically
提问by user3242861
I have jobs to send several emails.
我有工作要发送几封电子邮件。
In my controller I call the job:
在我的控制器中,我称之为工作:
dispatch(new SendStartPatEmail($data));
And record is saved in table jobs.
并且记录保存在表作业中。
But to execute the job I have to run php artisan queued:work
manually.
How can I do this automatically?
但是要执行这项工作,我必须php artisan queued:work
手动运行。我怎样才能自动执行此操作?
回答by Jonathon
There are lots of different ways, all depending on the environment that you're using. Laravel tends to recommend using Supervisorto monitor your queue workers and keep them running.
有很多不同的方法,都取决于您使用的环境。Laravel 倾向于推荐使用Supervisor来监控你的队列工作人员并让他们保持运行。
Alternatively, you may wish to have your jobs execute immediately, instead of adding them to a queue. You can do this by setting your queue driver to sync
, either in your config:
或者,您可能希望立即执行您的作业,而不是将它们添加到队列中。您可以通过sync
在配置中将队列驱动程序设置为 来做到这一点:
config/queue.php
配置/队列.php
'default' => env('QUEUE_DRIVER', 'sync'),
or in your .env
file (assuming your config is set up as above)
或在您的.env
文件中(假设您的配置如上设置)
.env
.env
QUEUE_DRIVER=sync
回答by spartanz51
Already answered here
已经在这里回答
Yes, if you use Linux you can use for example supervisor which will run
php artisan queue:listen
(you need to add this command to supervisor configuration file) and it will make sure all the time this command is running.
是的,如果您使用 Linux,您可以使用例如将运行的主管
php artisan queue:listen
(您需要将此命令添加到主管配置文件中),它将确保该命令一直在运行。
回答by Dionisis K
php artisan queue:work
is a simple command that listens to a queue and executes some jobs.
php artisan queue:work
是一个简单的命令,它侦听队列并执行一些作业。
What's the whole concept?
整个概念是什么?
You can run this simple command on the background and all jobs in queue will be executed.
But running a process (queue:work
) on the background is not always safe.
您可以在后台运行这个简单的命令,队列中的所有作业都将被执行。
但是queue:work
在后台运行进程 ( ) 并不总是安全的。
Why? because there is always the chance that the process may be terminated or stuck because of a memory leak.
为什么?因为进程总是有可能因为内存泄漏而终止或卡住。
In this case laravel recommends the use of a Supervisor.The supervisor is another process working like a service.It is responsible to check whether the process that php artisan queue:work
creates, works normally or it should be restarted.
在这种情况下,laravel 推荐使用Supervisor。 supervisor 是另一个像服务一样工作的进程。它负责检查php artisan queue:work
创建的进程是否正常工作或应该重新启动。
This way php artisan queue:work
runs on the background but there is a mechanism (supervisor) which can restart the process in case something goes wrong
这种方式php artisan queue:work
在后台运行,但有一种机制(主管)可以在出现问题时重新启动进程
回答by parker_codes
There is the dispatch_now( ... )
method for specifying those jobs you want run synchronously.
有一种dispatch_now( ... )
方法可以指定要同步运行的那些作业。
I don't care for underscores, so I usually create a helper method dispatchNow( ... )
that calls the underscore version..
我不关心下划线,所以我通常会创建一个dispatchNow( ... )
调用下划线版本的辅助方法..