【发布时间】:2020-01-03 12:52:50
【问题描述】:
我的工作是测试失败是如何发生的:
<?php
namespace App\Jobs;
use App\ApiUser;
use App\Helpers\Helper;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class FakeJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $apiUser;
public function __construct(ApiUser $apiUser) {
$this->apiUser = $apiUser;
}
public function handle() {
throw new Exception('time is even');
}
public function failed(Exception $exception) {
// Send user notification of failure, etc...
$path = storage_path('logs/s3_logs/FakeJob_failed_' . Helper::generateRandomString(5) . '.txt');
file_put_contents($path, $exception->getMessage());
}
}
当我正常调度时,它会转到failed 函数并完全按照预期写入文件。
但是,当我像 FakeJob::dispatchNow($apiUser); 那样做时,它根本不会这样做......
有没有办法让dispatchNow 与请求在同一个进程上运行,但像正常排队一样失败?
因为目前看来,我唯一的方法是:
$fakeJob = new FakeJob($apiUser);
try {
$fakeJob->handle();
} catch(Exception $e) {
$fakeJob->failed($e);
}
我想这……很好,但并不理想。
【问题讨论】: