运行 Laravel 队列:在共享主机上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46141652/
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
Running laravel queue:work on a shared hosting
提问by lulliezy
I want my laravel queue:work to keep running on a shared hosting, this is a shared hosting (am not on a VPS)I can't install anything because almost all online sources when I was searching for this suggest installing supervisor. So basically I thought I could just create a cron job that checks if queue is not running and starts it, any help on how to go about this because am kinda stuck, thanks.
我希望我的 laravel queue:work 继续在共享主机上运行,这是一个共享主机(不在 VPS 上)我无法安装任何东西,因为几乎所有在线资源在我搜索时都建议安装主管。所以基本上我想我可以创建一个 cron 作业来检查队列是否没有运行并启动它,关于如何解决这个问题的任何帮助,因为我有点卡住了,谢谢。
P.S. Am on a Linux server and Laravel 5.3
PS Am 在 Linux 服务器和 Laravel 5.3 上
回答by Suresh Velusamy
As you mentioned you are using shared hosting, You follow the below steps.
正如您所提到的,您使用的是共享主机,请按照以下步骤操作。
Step 1.you need to setup your queue driver as database
步骤 1.您需要将队列驱动程序设置为database
Step 2you need to setup a cron job with with the following command
php /path/to/application/artisan queue:work --queue=high,default
.
第 2 步,您需要使用以下命令设置 cron 作业
php /path/to/application/artisan queue:work --queue=high,default
。
You can give a try. I hope it will work.
你可以试试。我希望它会起作用。
回答by kikuyu1
This is the solution that worked for me after searching for days.
这是搜索几天后对我有用的解决方案。
flock -n /tmp/latavel_queues.lockfile /usr/bin/php /path/to/laravel/artisan queue:listen
flock -n /tmp/latavel_queues.lockfile /usr/bin/php /path/to/laravel/artisan queue:listen
See https://laracasts.com/discuss/channels/servers/database-queue-on-shared-hosting
请参阅https://laracasts.com/discuss/channels/servers/database-queue-on-shared-hosting
回答by Andrii
One more solution (I solved same problem in this way). You can make script like this:
另一种解决方案(我以这种方式解决了同样的问题)。你可以制作这样的脚本:
# getting running processes with name "queue:work"
QUEUE_STATUS=$(ps aux | grep "queue:work")
# check is queue:work started, if no, start it
if $( echo $QUEUE_STATUS | grep --quiet 'artisan queue:work')
then
exit;
else
php ~/public_html/artisan queue:work
fi
and run it in CRON. I run every 10 min.
并在 CRON 中运行它。我每 10 分钟跑一次。
回答by Amin Eshtiaghi
the best way is to set followin command on your tasks of panel (I'm using plesk control panel, it makes me able to set task there)
最好的方法是在您的面板任务上设置跟随命令(我正在使用 plesk 控制面板,它使我能够在那里设置任务)
php artisan queue:work --once
Note:in my shared host, I must set following values because of their server configuration:
注意:在我的共享主机中,由于它们的服务器配置,我必须设置以下值:
- php:/opt/plesk/php/7.2/bin/php -q
- artisan:/var/www/vhosts/t4f.ir/httpdocs/artisan
- my command:then I should write the command
- php:/opt/plesk/php/7.2/bin/php -q
- 工匠:/var/www/vhosts/t4f.ir/httpdocs/artisan
- 我的命令:那我应该写命令
so, the result would be like this:
所以,结果是这样的:
/opt/plesk/php/7.2/bin/php -q /var/www/vhosts/t4f.ir/httpdocs/artisan queue:work --once
there is another option for runtime which I set to Cron type with value of: * * * * *which means, every minute this code will be executed. as I used --once in end of my commad, once it execute the command and job has been finished, it will be terminated. regarding to concurrent execution, I'm not worried about beacuase it's handling in queueing system and it's responsibility of this system.
运行时还有另一个选项,我将其设置为 Cron 类型,其值为:* * * * *这意味着每分钟都会执行此代码。正如我在命令末尾使用 --once 一样,一旦它执行命令并且作业完成,它将被终止。关于并发执行,我并不担心,因为它在排队系统中处理并且是该系统的责任。
回答by lulliezy
I figured a hack to accomplish this
我想出了一个技巧来完成这个
On file Illuminate\Queue\Worker.php
my current laravel version (5.3) is on line 151; on function runNextJob($connectionName, $queue, WorkerOptions $options)
add else
as below
Illuminate\Queue\Worker.php
我当前的 Laravel 版本 (5.3)在文件中位于第 151 行;在功能上runNextJob($connectionName, $queue, WorkerOptions $options)
添加else
如下
if ($job) {
return $this->process(
$connectionName, $job, $options
);
} else {
$this->stop();
}
Now create cron job that will run the number of times you like with command php artisan queue:work
the moment the queue is exhausted, it will exit (but should be frequent as the process exists)
现在创建 cron 作业,该作业将php artisan queue:work
在队列耗尽的那一刻使用命令运行您喜欢的次数,它将退出(但由于进程存在而应该频繁发生)
UPDATE:Using task schedular with withoutOverlapping()
prevents further calls of the command if its already running, so this is a better option considering the previous one is a change you have to make everytime you composer install
or composer update
更新:withoutOverlapping()
如果命令已经在运行,则使用 task schedular with可防止进一步调用命令,因此这是一个更好的选择,因为前一个是您每次composer install
或composer update