Laravel 在设定的时间调度队列

Laravel dispatching Queues at set time

我目前正在调度排队的作业以立即发送 API 事件,在繁忙的时候,这些排队的作业需要等待到 API 不太忙的晚上,我如何才能保持这些排队作业或将它们安排在第二天 01:00am 后仅 运行。

排队的作业调用当前看起来像:

EliQueueIdentity::dispatch($EliIdentity->id)->onQueue('eli');

同一个队列中还有其他作业,都需要在繁忙时间进行

您可以使用延迟调度(参见https://laravel.com/docs/6.x/queues#delayed-dispatching):

// Run it 10 minutes later:
EliQueueIdentity::dispatch($EliIdentity->id)->onQueue('eli')->delay(
    now()->addMinutes(10)
);

或传递另一个碳实例,如:

// Run it at the end of the current week (i believe this is sunday 23:59, havent checked).
->delay(Carbon::now()->endOfWeek());
// Or run it at february second 2020 at 00:00.
->delay(Carbon::createFromFormat('Y-m-d', '2020-02-02'));

你明白了。

在特定时间使用延迟 运行 作业。

EliQueueIdentity::dispatch($EliIdentity->id)
    ->onQueue('eli')
    ->delay($this->scheduleDate());

计算时间的助手,处理 00:00 到 01:00 之间的边缘情况,它会延迟一整天。虽然没有指定如何处理繁忙,但提供了一个您可以实现的伪示例。

private function scheduleDate()
{
    $now = Carbon::now();

    if (! $this->busy()) {
        return $now;
    }

    // check for edge case of 00:00:00 to 01
    if ($now->hour <= 1) {
        $now->setTime(1, 0, 0);
        return $now;
    }

    return Carbon::tomorrow()->addHour();
}