In Laravel 9, the validation of the timeout of the queue task (sleep() cannot be used, it will cause the related settings to not work)
1. In Laravel 9, the queue task timeout. Decided to try to verify, the implementation in the task class is as follows. Finally found that it was not in effect on Windows systems. The reason is that the PCNTL PHP extension must be installed to specify the task timeout. But this extension is not available on the Windows platform. as shown in Figure 1
public $timeout = 2;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
sleep(10);
Log::info(
'handle5',
[
date('Y-m-d H:i:s')
]
);
}
2. Decide to perform queue tasks in the Linux system. The timeout setting has taken effect. Queue job execution failed. The generated logs are as expected. as shown in Figure 2
public $timeout = 300;
public function handle()
{
Log::info(
'handle-timeout20-before',
[
date('Y-m-d H:i:s')
]
);
sleep(600);
Log::info(
'handle-timeout20-after',
[
date('Y-m-d H:i:s')
]
);
// 其他流程实现
Log::info(
'handle-timeout20-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
}
[2024-05-13 02:13:58] feature.INFO: handle-timeout20-before ["2024-05-13 02:13:58"]
[2024-05-13 02:18:58] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
3. In the table failed_jobs, search: `Exception` like%OrdershippingLogExportJob%, you can search for the corresponding records. View the value of the field exception as shown below. as shown in Figure 3
Illuminate\Queue\MaxAttemptsExceededException: Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out. in /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:755
Stack trace:
#0 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(214): Illuminate\Queue\Worker->maxAttemptsExceededException(Object(Illuminate\Queue\Jobs\RedisJob))
#1 /var/www/backend/Modules/Order/Jobs/OrderShippingLogExportJob.php(65): Illuminate\Queue\Worker->Illuminate\Queue\{closure}(14, Array)
#2 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Modules\Order\Jobs\OrderShippingLogExportJob->handle()
#3 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#4 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#5 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#6 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#7 /var/www/backend/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\Container\Container->call(Array)
#8 /var/www/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Modules\Order\Jobs\OrderShippingLogExportJob))
#9 /var/www/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\Order\Jobs\OrderShippingLogExportJob))
#10 /var/www/backend/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#11 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(124): Illuminate\Bus\Dispatcher->dispatchNow(Object(Modules\Order\Jobs\OrderShippingLogExportJob), false)
#12 /var/www/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue\{closure}(Object(Modules\Order\Jobs\OrderShippingLogExportJob))
#13 /var/www/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\Order\Jobs\OrderShippingLogExportJob))
#14 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(126): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#15 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(70): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware(Object(Illuminate\Queue\Jobs\RedisJob), Object(Modules\Order\Jobs\OrderShippingLogExportJob))
#16 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(98): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#17 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(425): Illuminate\Queue\Jobs\Job->fire()
#18 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(375): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))
#19 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(173): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\RedisJob), 'redis', Object(Illuminate\Queue\WorkerOptions))
#20 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(150): Illuminate\Queue\Worker->daemon('redis', 'export_queue', Object(Illuminate\Queue\WorkerOptions))
#21 /var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(134): Illuminate\Queue\Console\WorkCommand->runWorker('redis', 'export_queue')
#22 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle()
#23 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#24 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#25 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#26 /var/www/backend/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#27 /var/www/backend/vendor/laravel/framework/src/Illuminate/Console/Command.php(144): Illuminate\Container\Container->call(Array)
#28 /var/www/backend/vendor/symfony/console/Command/Command.php(312): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#29 /var/www/backend/vendor/laravel/framework/src/Illuminate/Console/Command.php(126): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#30 /var/www/backend/vendor/symfony/console/Application.php(1022): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#31 /var/www/backend/vendor/symfony/console/Application.php(314): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#32 /var/www/backend/vendor/symfony/console/Application.php(168): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 /var/www/backend/vendor/laravel/framework/src/Illuminate/Console/Application.php(102): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 /var/www/backend/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(151): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#35 /var/www/backend/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#36 {main}
4. The maximum number of attempts to define the task class itself is 2, and the implementation in the task class is as follows. After the first verification, it does not meet the expectations. Since the timeout is set to 300 seconds, and the queue Sleep (600), in theory, no matter what, the queue task should not be executed successfully. However, there is a log information related to the successful execution. The second and third verifications are not quite as expected. There is no execution success, but no execution fails. Instead, there is no time-out exception message (by searching in the table failed_jobs, search: `exception` like%OrdershippingLogExportJob%, no corresponding records were found). The expectation is: During the execution of a queue task, handle-timeout20-$tries2-before should appear 2 times, and the last queue task execution fails, and the method failed will be run.
/**
* 在超时之前任务可以运行的秒数.
*
* @var int
*/
public $timeout = 300;
public $tries = 2;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private JobStatus $jobStatus;
private array $params;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(JobStatus $jobStatus, array $params)
{
$this->jobStatus = $jobStatus;
$this->params = $params;
$this->onQueue(OrderConstants::EXPORT_QUEUE);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$orderShippingLogService = app(OrderShippingLogService::class);
Log::info(
'handle-timeout20-$tries2-before',
[
date('Y-m-d H:i:s')
]
);
sleep(600);
Log::info(
'handle-timeout20-$tries2-after',
[
date('Y-m-d H:i:s')
]
);
//
Log::info(
'handle-timeout20-$tries2-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
}
[2024-05-13 06:51:06] feature.INFO: handle-timeout20-$tries2-before ["2024-05-13 06:51:06"]
[2024-05-13 06:56:01] feature.INFO: handle-timeout20-$tries2-after ["2024-05-13 06:56:01"]
[2024-05-13 06:56:01] feature.INFO: handle-timeout20-$tries2-updateJobStatus-after ["2024-05-13 06:56:01"]
[2024-05-13 07:12:46] feature.INFO: handle-timeout20-$tries2-before ["2024-05-13 07:12:46"]
[2024-05-13 07:24:49] feature.INFO: handle-timeout20-$tries2-before ["2024-05-13 07:24:49"]
5. Decide to add a timeout failure, if you want to mark the task as failed on the timeout, you can define the $failOnTimeout property on the task class. There is no execution success, but no execution fails. Instead, there is no time-out exception message (by searching in the table failed_jobs, search: `exception` like%OrdershippingLogExportJob%, no corresponding records were found). The expected is: during the execution of the queue task, handle-timeout20-$tries2-$failontimeout-before should appear 2 times, and the last queue task execution fails, the method failed will be run. The preliminary conclusion is that when $tries is greater than 1, the setting of $timeout does not seem to take effect.
/**
* 在超时之前任务可以运行的秒数.
*
* @var int
*/
public $timeout = 300;
public $tries = 2;
public $failOnTimeout = true;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private JobStatus $jobStatus;
private array $params;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(JobStatus $jobStatus, array $params)
{
$this->jobStatus = $jobStatus;
$this->params = $params;
$this->onQueue(OrderConstants::EXPORT_QUEUE);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$orderShippingLogService = app(OrderShippingLogService::class);
Log::info(
'handle-timeout20-$tries2-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
sleep(600);
Log::info(
'handle-timeout20-$tries2-$failOnTimeout-after',
[
date('Y-m-d H:i:s')
]
);
//
Log::info(
'handle-timeout20-$tries2-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
}
[2024-05-13 07:50:20] feature.INFO: handle-timeout20-$tries2-$failOnTimeout-before ["2024-05-13 07:50:20"]
6. Verify that when $tries is equal to 1, the setting of $timeout is the same as $failontimeout = true. to compare with step 2. Confirm that $failOnTimeout = true is valid. The final finding is no different from step 2. It feels like $failOnTimeout = true is no longer useful.
/**
* 在超时之前任务可以运行的秒数.
*
* @var int
*/
public $timeout = 300;
public $tries = 1;
public $failOnTimeout = true;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private JobStatus $jobStatus;
private array $params;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(JobStatus $jobStatus, array $params)
{
$this->jobStatus = $jobStatus;
$this->params = $params;
$this->onQueue(OrderConstants::EXPORT_QUEUE);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$orderShippingLogService = app(OrderShippingLogService::class);
Log::info(
'handle-timeout20-$tries1-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
sleep(600);
Log::info(
'handle-timeout20-$tries1-$failOnTimeout-after',
[
date('Y-m-d H:i:s')
]
);
//
Log::info(
'handle-timeout20-$tries1-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
}
[2024-05-13 09:38:29] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 09:38:29"]
[2024-05-13 09:38:29] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 09:38:29"]
[2024-05-13 09:40:29] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
7. Re-validate step 4, remove $failOnTimeout = true, remove sleep(), but use other solutions to make the job execution timeout. The queue is all lost, and the failed method is executed, and the andle-timeout120-$tries2-$failOnTimeout-updateJobStatus-after has not been executed. Basically in line with expectations (expect handle-timeout120-$tries2-$failontimeout-before will execute 2 times). Conclusion: The confirmation of the Sleep method will affect the number of attempts.
/**
* 在超时之前任务可以运行的秒数.
*
* @var int
*/
public $timeout = 120;
public $tries = 2;
// public $failOnTimeout = true;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info(
'handle-timeout120-$tries2-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
// sleep(600);
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-after',
[
date('Y-m-d H:i:s')
]
);
// 此环节超时
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
$this->updateJobStatus(JobStatusConstant::STATUS_FAIL);
}
[2024-05-13 08:56:25] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 08:56:25"]
[2024-05-13 08:56:25] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 08:56:25"]
[2024-05-13 08:58:25] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
[2024-05-13 09:19:27] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 09:19:27"]
[2024-05-13 09:19:27] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 09:19:27"]
[2024-05-13 09:21:27] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
[2024-05-13 09:22:43] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 09:22:43"]
[2024-05-13 09:22:43] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 09:22:43"]
[2024-05-13 09:24:43] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
8. Revalidate step 5, decide to remove sleep(), but use other schemes to make the job execution timeout. After verifying 3 times, the queues all failed, and all execute the failed method, and andle-timeout120-$tries2-$failontimeout-updateJobStatus-after None of them have been executed. Basically in line with expectations (expect handle-timeout120-$tries2-$failontimeout-before will execute 2 times). Conclusion: The confirmation of the Sleep method will affect the number of attempts. as shown in Figure 4
/**
* 在超时之前任务可以运行的秒数.
*
* @var int
*/
public $timeout = 120;
public $tries = 2;
public $failOnTimeout = true;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info(
'handle-timeout120-$tries2-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
// sleep(600);
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-after',
[
date('Y-m-d H:i:s')
]
);
// 此环节超时
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
$this->updateJobStatus(JobStatusConstant::STATUS_FAIL);
}
[2024-05-13 08:56:25] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 08:56:25"]
[2024-05-13 08:56:25] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 08:56:25"]
[2024-05-13 08:58:25] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
[2024-05-13 09:19:27] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 09:19:27"]
[2024-05-13 09:19:27] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 09:19:27"]
[2024-05-13 09:21:27] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
[2024-05-13 09:22:43] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 09:22:43"]
[2024-05-13 09:22:43] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 09:22:43"]
[2024-05-13 09:24:43] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
9. Set the number of attempts to 4 times, cancel $failOnTimeout = true. Not as expected, expected to execute 4 times for handle-timeout120-$tries2-$failontimeout-before. In fact, there is only 1 time, and there is neither success nor failure, but has been stuck.
/**
* 在超时之前任务可以运行的秒数.
*
* @var int
*/
public $timeout = 120;
public $tries = 4;
// public $failOnTimeout = true;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info(
'handle-timeout120-$tries2-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
// sleep(600);
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-after',
[
date('Y-m-d H:i:s')
]
);
// 此环节超时
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
$this->updateJobStatus(JobStatusConstant::STATUS_FAIL);
}
[2024-05-13 09:51:02] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 09:51:02"]
[2024-05-13 09:51:02] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 09:51:02"]
[2024-05-13 10:03:33] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 10:03:33"]
[2024-05-13 10:03:33] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 10:03:33"]
10. Set the number of attempts to 4 times, and set $failOnTimeout = true. Basically in line with expectations, it is expected that handle-timeout120-$tries2-$failontimeout-before will be executed 4 times. Actually only 1 time, but the execution failed (in line with expectations).
/**
* 在超时之前任务可以运行的秒数.
*
* @var int
*/
public $timeout = 120;
public $tries = 4;
public $failOnTimeout = true;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info(
'handle-timeout120-$tries2-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
// sleep(600);
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-after',
[
date('Y-m-d H:i:s')
]
);
// 此环节超时
Log::info(
'andle-timeout120-$tries2-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
$this->updateJobStatus(JobStatusConstant::STATUS_FAIL);
}
[2024-05-13 11:13:54] feature.INFO: handle-timeout120-$tries2-$failOnTimeout-before ["2024-05-13 11:13:54"]
[2024-05-13 11:13:54] feature.INFO: andle-timeout120-$tries2-$failOnTimeout-after ["2024-05-13 11:13:54"]
[2024-05-13 11:15:54] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
11. Set the number of attempts to 3 times, and set the number of seconds to wait before the retry task $backoff = 6. Verified 2 times, the result is the same. Basically in line with expectations, it is expected that handle-timeout120-$backoff6-$tries3-$failontimeout-before will be executed 3 times, in fact there are only 2 time, but the execution was successful (not as expected).
// 在超时之前任务可以运行的秒数
public int $timeout = 120;
// 重试任务前等待的秒数
public int $backoff = 6;
// 任务可尝试次数
public int $tries = 3;
// 标示是否应在超时时标记为失败
// public bool $failOnTimeout = true;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$orderShippingLogService = app(OrderShippingLogService::class);
Log::info(
'handle-timeout120-$backoff6-$tries3-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
// sleep(600);
// Log::info(
// 'handle-timeout120-$backoff6-$tries3-$failOnTimeout-after',
// [
// date('Y-m-d H:i:s')
// ]
// );
// 执行超时
Log::info(
'handle-timeout120-$backoff6-$tries3-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
$this->updateJobStatus(JobStatusConstant::STATUS_FAIL);
}
[2024-05-14 07:19:22] feature.INFO: handle-timeout120-$backoff6-$tries3-$failOnTimeout-before ["2024-05-14 07:19:22"]
[2024-05-14 07:24:49] feature.INFO: handle-timeout120-$backoff6-$tries3-$failOnTimeout-before ["2024-05-14 07:24:49"]
[2024-05-14 07:25:21] feature.INFO: handle-timeout120-$backoff6-$tries3-$failOnTimeout-updateJobStatus-after ["2024-05-14 07:25:21"]
[2024-05-14 07:41:38] feature.INFO: handle-timeout120-$backoff6-$tries3-$failOnTimeout-before ["2024-05-14 07:41:38"]
[2024-05-14 07:50:20] feature.INFO: handle-timeout120-$backoff6-$tries3-$failOnTimeout-before ["2024-05-14 07:50:20"]
[2024-05-14 07:50:52] feature.INFO: handle-timeout120-$backoff6-$tries3-$failOnTimeout-updateJobStatus-after ["2024-05-14 07:50:52"]
12. Set the number of attempts to 3 times, set the number of seconds to wait before the retry task $backoff = 6, and set $failOnTimeout = true. Basically in line with expectations, it is expected that handle-timeout120-$backoff6-$tries3-$failontimeout-before will be executed 3 times, in fact there is only 1 times, but the execution failed (as expected).
// 在超时之前任务可以运行的秒数
public int $timeout = 120;
// 重试任务前等待的秒数
public int $backoff = 6;
// 任务可尝试次数
public int $tries = 3;
// 标示是否应在超时时标记为失败
// public bool $failOnTimeout = true;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$orderShippingLogService = app(OrderShippingLogService::class);
Log::info(
'handle-timeout120-$backoff6-$tries3-$failOnTimeout-before',
[
date('Y-m-d H:i:s')
]
);
// sleep(600);
// Log::info(
// 'handle-timeout120-$backoff6-$tries3-$failOnTimeout-after',
// [
// date('Y-m-d H:i:s')
// ]
// );
// 执行超时
Log::info(
'handle-timeout120-$backoff6-$tries3-$failOnTimeout-updateJobStatus-after',
[
date('Y-m-d H:i:s')
]
);
}
/**
* @param \Throwable $e
* @return void
*/
public function failed(\Throwable $e)
{
Log::error('导出订单交运日志信息异常:' . $e->getFile() . $e->getLine() . $e->getMessage());
$this->updateJobStatus(JobStatusConstant::STATUS_FAIL);
}
[2024-05-14 10:09:55] feature.INFO: handle-timeout120-$backoff6-$tries3-$failOnTimeout-before ["2024-05-14 10:09:55"]
[2024-05-14 10:11:55] feature.ERROR: 导出订单交运日志信息异常:/var/www/backend/vendor/laravel/framework/src/Illuminate/Queue/Worker.php755Modules\Order\Jobs\OrderShippingLogExportJob has been attempted too many times or run too long. The job may have previously timed out.
13. Final decision, the configuration is as follows
// 在超时之前任务可以运行的秒数
public int $timeout = 120;
// 任务可尝试次数
public int $tries = 1;
// 标示是否应在超时时标记为失败
public bool $failOnTimeout = true;



