没有不值得去解决的问题,也没有不值得去学习的技术!

Validating Max Tries for Queue Jobs in Laravel 9: What Is the Default Value?

After the first 4 failures (FAIL in orange), it continues to retry until the 5th attempt (FAIL in red), at which point it truly fails.

1. In Laravel 9, what is the default value for the maximum number of attempts for a queued job? I decided to verify this. The implementation in the job class is as follows:

PHP


	public $timeout = 10;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        sleep(2);
        Log::info(
            'handle',
            [
                date('Y-m-d H:i:s')
            ]
        );
        sss();
    }


2. The printed logs are as follows:

Plaintext

[2024-05-11 05:56:47] local.INFO: handle ["2024-05-11 05:56:47"] 

3. I defined the maximum number of attempts for the job class itself as 1. The implementation in the job class is as follows. The final printed logs show the same result as when $tries is not set. Conclusion: the default maximum number of attempts is 1.

PHP


	public $tries = 1;
	public $timeout = 10;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        sleep(2);
        Log::info(
            'handle',
            [
                date('Y-m-d H:i:s')
            ]
        );
        sss();
    }


Plaintext

[2024-05-11 06:04:02] local.INFO: handle ["2024-05-11 06:04:02"] 

4. I defined the maximum number of attempts for the job class itself as 0. The implementation in the job class is as follows. The final printed logs contain over 40 entries, indicating that when set to 0, the maximum number of attempts is unlimited. See Figure 1 and Figure 2.

I defined the maximum number of attempts for the job class itself as 0. The implementation in the job class is as follows. The final printed logs contain over 40 entries, indicating that when set to 0, the maximum number of attempts is unlimited.
Figure 1

 

I defined the maximum number of attempts for the job class itself as 0. The implementation in the job class is as follows. The final printed logs contain over 40 entries, indicating that when set to 0, the maximum number of attempts is unlimited.
Figure 2
PHP


	public $tries = 0;
	public $timeout = 10;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        sleep(2);
        Log::info(
            'handle0',
            [
                date('Y-m-d H:i:s')
            ]
        );
        sss();
    }


5. I defined the maximum number of attempts for the job class itself as 5. The implementation in the job class is as follows. The final printed logs contain 5 entries, indicating that when set to 5, the maximum number of attempts is 5. After the first 4 failures (FAIL in orange), it continues to retry until the 5th attempt (FAIL in red), at which point it truly fails. See Figure 3 and Figure 4.

I defined the maximum number of attempts for the job class itself as 5. The implementation in the job class is as follows. The final printed logs contain 5 entries, indicating that when set to 5, the maximum number of attempts is 5.
Figure 3

 

After the first 4 failures (FAIL in orange), it continues to retry until the 5th attempt (FAIL in red), at which point it truly fails.
Figure 4
PHP


	public $tries = 5;
	public $timeout = 10;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        sleep(2);
        Log::info(
            'handle5',
            [
                date('Y-m-d H:i:s')
            ]
        );
        sss();
    }


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系