如果我在 Controller 队列中使用此代码,效果很好
$job=(new ReProcessShipment($single_data->request_data))->delay(2);
$this->dispatch($job);
但在 crontab 中使用相同的代码会抛出错误
Method App\Console\Commands\AddPreProcess::dispatch does not exist. {"exception":"[object] (BadMethodCallException(code: 0):
Method App\Console\Commands\AddPreProcess::dispatch does not exist.
尝试像这样使用它
$job=(new ReProcessShipment($single_data->request_data))->delay(2);
ReProcessShipment::dispatch($job);
然后报错
Object of class App\Jobs\ReProcessShipment could not be converted to string {"exception":"[object] (ErrorException(code: 0): Object of class App\Jobs\ReProcessShipment could not be converted to string at
我无法处理来自 cronjob 的作业队列,任何建议都很好。
最佳答案
您可以 dispatch通过在作业类上调用静态 dispatch
方法并将作业的构造函数参数传递给 dispatch
方法来创建作业,如下所示:
ReProcessShipment::dispatch($single_data->request_data)->delay(2);
确保您使用的是 Illuminate\Foundation\Bus\Dispatchable
特性,以便能够在作业类上调用 dispatch
,例如:
use Illuminate\Foundation\Bus\Dispatchable;
class ProcessPodcast implements ShouldQueue
{
use Dispatchable, ...
如果您查看 source您会看到静态 dispatch
函数使用作业的参数为您创建作业,因此您无需在调度作业之前创建作业。这是 dispatch
函数的源代码:
public static function dispatch()
{
return new PendingDispatch(new static(...func_get_args()));
}
所以它本质上改变了这个:
ReProcessShipment::dispatch($single_data->request_data);
进入这个:
new PendingDispatch(new ReProcessShipment($single_data->request_data));
关于php - Laravel 无法从 crontab 调度作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57990680/