如何解决为 Laravel 排队命令设置自定义超时
我有一个这样的命令类。
class MyCommand extends Command {
protected $signature = 'mycommand';
}
然后通过队列运行这个
Artisan::queue('mycommand');
一个工人正在运行多个作业 php artisan queue:work --timeout=120
通常,我们可以为这样的作业类设置自定义超时
class MyJob implements ShouldQueue {
public $timeout = 240; // custom timeout
}
但是如何在 Command 类中正确实现这一点?
解决方法
to_remove
不适合您的原因是因为实际分派的工作是:Artisan::queue
;
查看 Illuminate\Foundation\Console\QueuedCommand
类上的 queue
方法
相反,您应该使用 \Illuminate\Foundation\Console\Kernel
trait 并在使用 Dispatchable
方法的同时实现 ShouldQueue
。
这是一个简单的命令:
::dispatch()
发送测试:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class TestCommand extends Command implements ShouldQueue
{
use Dispatchable;
protected $signature = 'test:command';
protected $description = 'Command description';
public $timeout = 3;
public function handle()
{
$count = 0;
while (true) {
sleep(1);
dump($count++);
}
}
}
和worker的输出(使用Horizon)
/** @test */ // test
public function test(){
// given
TestCommand::dispatch();
// when
// then
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。