laravel 无法运行预定命令,而 queue:listen 定期运行 schedule()
问题描述
注意:我的问题与 Laravel 中的命令计划 哪个计划不起作用无关.但在我的问题中,调度有效,但它不能调用工匠命令.
我使用 laravel 调度 artisan 命令.我直接从控制台运行命令,例如 sudo -u www-data/var/www/market/artisan command:printer-serving 281H28.
我知道它有效,因为我在命令的
handle() 函数的入口处设置了
p>Log::info('Working')
.
当我使用 laravel 的调度时.并且 cron 运行良好,因为
Log::info('command:printer-serving 281H28');
将内容连续输出到控制台.
但是 artisan 命令没有执行,它没有向控制台输出任何东西,也没有向数据库写入任何东西
在kernel.php中
command('inspire')->hourly();Log::info('command:printer-serving 281H28');$schedule->command('command:printer-serving --force 281H28')->everyMinute();//$schedule->command('printer-serving')->everyMinute();//$schedule->command(CommandPrinterServing::class, ['281H28'])->everyMinute();}}
命令名称,
protected $signature = 'command:printer-serving {pid}';
注意无论我在
$schedule->command()
函数中输入什么字符串,甚至不是命令,都不会发生任何变化,也不会从 cron 日志或 laravel 日志中报告错误.
我想知道如何调试
$schedule->command()
函数.
<小时>Cron 文件 vi/etc/cron.minutely/printer-task-minute
奇怪的是每 3 秒输出四次日志.
<代码>>[2017-11-29 16:52:14] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:14] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:14] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:14] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:17] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:17] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:17] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:17] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:21] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:21] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:21] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:21] worker_env.INFO:命令:打印机服务 281H28>[2017-11-29 16:52:24] worker_env.INFO:命令:打印机服务 281H28
我对日志频率感到困惑,所以我停止了 cron 和 beanstalkd.但是日志输出并没有停止.即使我停止了 apache2,日志保持输出.然后查看php进程,发现命令ps aux |的输出中有四个
.在这里我找到了为什么以那个频率输出日志,但是我不知道为什么 queue:listen --queue=xxxx --env=worker_env --delay=3
grep phpqueue:listen
执行 schedule()
函数作为这个问题 了解 Laravel 5.2 上的队列和调度程序.
每次我执行一个工匠命令 sudo -u www-data/var/www/market/artisan xxxxx
都会调用一次 schedule()
函数.如果命令是 queue:listen
像 sudo -u www-data/var/www/market/artisan queue:listen xxxxx
则 schedule()
函数将被定期调用.但是 schedule()
中的命令不会运行,除了 Log::info()
.只有在运行 schedule:run
命令时,artisan 命令和 schedule()
中的 Log::info()
都会执行.
我花了一天时间搜索问题、阅读 laravel 文档并运行许多测试.终于搞定了.
Laravel 的 artisan queue:listen
命令会定期调用 Kernel.php 中的 schedule()
,而它只运行 Log::info()
在 schedule()
而不是 $schedule->command()
(这里我也很困惑).p>
我有四个队列正在运行,用于 queue1 sudo -u www-data php artisan queue:work --queue=queue1 --env=worker_env --delay=3 --timeout=600
.queue2,3,4 也一样.所以 schedule()
中的 Log::info()
会每 3 秒执行 4 次.
以上让我觉得日程安排很好.实际上,调度没有被执行.我发现 /etc/cron.minutely/printer-task-minute
没有 x
权限,所以我为其添加了 x
权限.而且我发现我没有配置crontab文件,所以我添加了 * * * * * root test -x/usr/sbin/anacron ||( cd/&& run-parts --report/etc/cron.minutely )
到 /etc/crontab
(我对 cron 不熟悉).
解决方案:
像上面一样配置 cron,或者参考手册.
将queue:listen
改为queue:work
(改为queue:work
时,schedule()
不会执行通过定期排队,我也不知道这里的原因.).
Note: My question has nothing to do with Command schedule in Laravel which schedule not work. But in my question the scheduling works, but it cannot call the artisan command.
I use laravel scheduling artisan command. I run the command directly from the console like this sudo -u www-data /var/www/market/artisan command:printer-serving 281H28
.
I know it works because, I've Log::info('Working')
at the entry of the handle()
function of the command.
While when I use laravel's scheduling. And the cron works well, for below Log::info('command:printer-serving 281H28');
output the content to the console continuously.
But the artisan command not executed, it output nothing to the console, and not write something to the DB
In the Kernel.php
Command name, protected $signature = 'command:printer-serving {pid}';
Note
No matter what string even not a command I put in $schedule->command()
function, nothing will changes and not a error reported from the cron log or laravel log.
I want to know how to debug the $schedule->command()
function.
Cron file vi /etc/cron.minutely/printer-task-minute
The weired thing is log output four thmes every 3 seconds.
I'm puzzled about the log frequency, so I stop cron and beanstalkd. But the log output doesn't stop. Even I stop apache2, the log keeping output. Then I check the php process, and find there are four queue:listen --queue=xxxx --env=worker_env --delay=3
in the output of command ps aux | grep php
. Here I found why the log output in that frequency, but I don't know why queue:listen
execute the schedule()
function as this question Understanding Queues and Scheduler on Laravel 5.2.
Each time I executed a artisan command sudo -u www-data /var/www/market/artisan xxxxx
the schedule()
function will be called one time. And if the command is queue:listen
like sudo -u www-data /var/www/market/artisan queue:listen xxxxx
the schedule()
function will be called periodically. But the command in the schedule()
will not run, except the Log::info()
. Only when run schedule:run
command, both the artisan command and Log::info()
in schedule()
will executed.
It costs me a day to google the issue, read laravel docs, and run many test. Finally I make it out.
Laravel's artisan queue:listen
command call the schedule()
in the Kernel.php periodically, while it only run the Log::info()
in the schedule()
not the $schedule->command()
(Here I'm also confused).
I've four queue running, for queue1 sudo -u www-data php artisan queue:work --queue=queue1 --env=worker_env --delay=3 --timeout=600
. Same with queue2, 3, 4. So the Log::info()
in the schedule()
will executed four times every 3 seconds.
Above make me thought the schedule works well. Actually, the schedule is not executed. I found /etc/cron.minutely/printer-task-minute
have no x
privilege, so I add the x
privilege for it. And I found I didn't config the crontab file, so I add * * * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely )
to /etc/crontab
(I'm not familar with cron).
Solution:
Config the cron like above, or refer to the manual.
Change queue:listen
to queue:work
(When changed to queue:work
, schedule()
will not executed by queue periodically, I also don't know the reason here.).
这篇关于laravel 无法运行预定命令,而 queue:listen 定期运行 schedule()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!