Laravel 将作业设置为失败

Laravel setting Job as failed

如何 flag/set 作业失败?

我正在使用闭包函数来分派调用外部 API 的作业,我想根据 API 响应手动设置作业是成功还是失败。

这是我的代码,我使用的是一个简单的示例,而不是 API 调用。

public function sendSMS( $numbers ) {

    dispatch(function () use ( $numbers ) {            
        $this->smsProcess($numbers, $this->note->content);
    });

}

public function smsProcess( $numbers, $message ) {

    $int = random_int( 1, 10 ) * random_int( 1, 10 );

    if ( $int < 50  ) {
        throw ValidationException::withMessages('Less than 50')->status(403);
    } else {
        throw ValidationException::withMessages('Greater than 50')->status(403);
    }
}

sendSMS 函数 运行 时,我可以看到 smsProcess

的待处理作业

但是当我 运行 queue:work 作业正在 Processed 并且没有失败时,

那么如何手动触发失败的作业?

如果我在 smsProcess 上放置一些错误代码,我可以看到作业被标记为 Failed 由于 php 错误

您应该使用 Exception class 而不是 ValidationException

public function smsProcess( $numbers, $message ) {

    $int = random_int( 1, 10 ) * random_int( 1, 10 );

    if ( $int < 50  ) {
        throw new \Exception('Less than 50');
    } else {
        throw new \Exception('Greater than 50');
    }
}