There is no problem not worth solving, and no technology not worth learning!

In Laravel 6, after setting the static variable, it is not cleared when it is obtained in the next queue task, and will be used as a solution

参考:https://www.shuijingwanwq.com/2022/07/15/6796/ 。在 Laravel 6 的队列任务中,当任务失败时的处理(设置与获取静态变量)。但是,其存在一个 Bug,在下一队列任务中如果未被重新设置,则会被沿用。分别打印2个任务中的静态变量:self::$destination。预期 755 的 destination 为 null,但是其沿用了 754 的值

1. Reference:https://www.shuijingwanwq.com/2022/07/15/6796/. In Laravel 6’s queue task, processing when the task fails (set and get static variables). However, there is a bug that will be used if it is not reset in the next queue task. Print static variables in 2 tasks separately: self::$destination. It is expected that the destination of 755 is null, but it uses the value of 754. as shown in Figure 1

参考:https://www.shuijingwanwq.com/2022/07/15/6796/ 。在 Laravel 6 的队列任务中,当任务失败时的处理(设置与获取静态变量)。但是,其存在一个 Bug,在下一队列任务中如果未被重新设置,则会被沿用。分别打印2个任务中的静态变量:self::$destination。预期 755 的 destination 为 null,但是其沿用了 754 的值
Figure 1

    private static ?string $destination = null; // 解压缩 ZIP 文件后的目录

	Log::info(
		'destination',
		[
			'id' => $this->themeInstallationTask->id,
			'destination' => self::$destination
		]
	);



[2023-04-14 13:37:18] local.INFO: uploadThemeAssetToCdn {
    "id": 754,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/14/1681450622.2291.583840996"
} 
[2023-04-14 13:38:06] local.INFO: uploadThemeAssetToCdn {
    "id": 755,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/14/1681450622.2291.583840996"
} 


2. If in the next queue task, if there is a reset static variable to null, it will be overwritten, and print the static variables in 2 tasks: self::$destination. in line with expectations. as shown in Figure 2

如果在下一个队列任务中,有重新设置 静态变量为 null,则会被覆盖,分别打印2个任务中的静态变量:self::$destination。符合预期
Figure 2

    private static ?string $destination = null; // 解压缩 ZIP 文件后的目录

	self::$destination = null;

	Log::info(
		'uploadThemeAssetToCdn',
		[
			'id' => $this->themeInstallationTask->id,
			'destination' => self::$destination
		]
	);



[2023-04-14 13:44:46] local.INFO: uploadThemeAssetToCdn {
    "id": 756,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/14/1681451070.5331.338264899"
} 
[2023-04-14 13:45:34] local.INFO: uploadThemeAssetToCdn {
    "id": 757,
    "destination": null
} 


3. However, reference: In Laravel 6, in the queue task, the implementation of the exponential backoff on retry. When you want to pass static variables to the next queue task, it still causes problems with each other. In the handle method, the expected value of 760 is not null , but it has now been overwritten by 761’s null . as shown in Figure 3

但是,参考:在 Laravel 6 中,在队列任务中,重试时的指数回退(exponential backoff)的实现。当想将静态变量传递给下一个队列任务时,仍然会导致相互覆盖的问题。在 handle 方法中,预期 760 的值不为 null ,但是现在已经被 761 的 null 给覆盖
Figure 3

    private static ?string $absolutePath; // 待解压缩的 ZIP 文件的绝对路径
    private static ?string $destination; // 解压缩 ZIP 文件后的目录

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(ThemeInstallationTask $themeInstallationTask, ?string $absolutePath, ?string $destination)
    {
        // 队列任务构造器中接收了 Eloquent 模型,将会只序列化模型的 ID
        $this->themeInstallationTask = $themeInstallationTask;
        self::$absolutePath = $absolutePath;
        self::$destination = $destination;
        Log::info(
            'construct',
            [
                'id' => $this->themeInstallationTask->id,
                'destination' => self::$destination
            ]
        );
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws OverflowException
     */
    public function handle()
    {
        Log::info(
            'handle',
            [
                'id' => $this->themeInstallationTask->id,
                'destination' => self::$destination
            ]
        );
		
	}



PS E:\wwwroot\object> php artisan queue:work
[2023-04-14 14:06:03][ACuiiTG0Ptuo5Tl3iMCPnREKYZhGTva3] Processing: Jobs\ThemeInstalling
[2023-04-14 14:06:50][ACuiiTG0Ptuo5Tl3iMCPnREKYZhGTva3] Processed:  Jobs\ThemeInstalling
[2023-04-14 14:06:50][xcL1KIALWlYy9edrKD3GpqkWOzCbudSu] Processing: Jobs\ThemeInstalling
[2023-04-14 14:07:22][xcL1KIALWlYy9edrKD3GpqkWOzCbudSu] Processed:  Jobs\ThemeInstalling
[2023-04-14 14:07:22][Ff7Pcd8szupQYVAO2BkQhGJxyoQKrpo8] Processing: Jobs\ThemeAssetUploading
[2023-04-14 14:07:42][Ff7Pcd8szupQYVAO2BkQhGJxyoQKrpo8] Processed:  Jobs\ThemeAssetUploading
[2023-04-14 14:07:42][Nyc3IPP2mXHz3Hl2tT4RADsKNTAhxf1l] Processing: Jobs\ThemeAssetUploading
[2023-04-14 14:07:45][Nyc3IPP2mXHz3Hl2tT4RADsKNTAhxf1l] Processed:  Jobs\ThemeAssetUploading
[2023-04-14 14:07:45][gaMvLrJttc4Ws1qu2rAAsORFGAfPNZmq] Processing: Jobs\ThemeInstalled
[2023-04-14 14:07:45][gaMvLrJttc4Ws1qu2rAAsORFGAfPNZmq] Processed:  Jobs\ThemeInstalled
[2023-04-14 14:07:45][QALQV7djT9caKVH50HHZLe0x3OScalBP] Processing: Jobs\ThemeInstalled
[2023-04-14 14:07:45][QALQV7djT9caKVH50HHZLe0x3OScalBP] Processed:  Jobs\ThemeInstalled


[2023-04-14 14:06:19] local.INFO: uploadThemeAssetToCdn {
    "id": 760,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/14/1681452363.3555.1473533151"
} 
[2023-04-14 14:06:19] local.INFO: construct {
    "id": 760,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/14/1681452363.3555.1473533151"
} 
[2023-04-14 14:06:53] local.INFO: uploadThemeAssetToCdn {
    "id": 761,
    "destination": null
} 
[2023-04-14 14:06:53] local.INFO: construct {
    "id": 761,
    "destination": null
} 
[2023-04-14 14:07:22] local.INFO: handle {
    "id": 760,
    "destination": null
} 
[2023-04-14 14:07:22] local.INFO: destination {
    "id": 760,
    "destination": null
} 
[2023-04-14 14:07:42] local.INFO: destination {
    "id": 760,
    "destination": null
} 
[2023-04-14 14:07:42] local.INFO: handle {
    "id": 761,
    "destination": null
} 
[2023-04-14 14:07:42] local.INFO: destination {
    "id": 761,
    "destination": null
} 
[2023-04-14 14:07:45] local.INFO: destination {
    "id": 761,
    "destination": null
} 


4. Finally decide to give up the use of static variables, and store the corresponding values in the MySQL table record related to the queue task. Queue tasks no longer affect each other, in line with expectations. as shown in Figure 4

最终决定放弃静态变量的使用,将相应的值存储在队列任务相关的 MySQL 表记录中。队列任务之间不再相互影响,符合预期
Figure 4

{
  "theme_download_paths": {
    "zip_path": "/theme_downloads/2023/04/17/1681696283.1861.462360447.zip",
    "destination": "/theme_downloads/2023/04/17/1681696283.1861.462360447"
  }
}



PS E:\wwwroot\object> php artisan queue:work
[2023-04-17 09:51:22][0Ta6TjtzacnMmI50uh9Ti3s9qfh5c5H1] Processing: Jobs\ThemeInstalling
[2023-04-17 09:52:21][0Ta6TjtzacnMmI50uh9Ti3s9qfh5c5H1] Processed:  Jobs\ThemeInstalling
[2023-04-17 09:52:21][dcjXRXkV2XgHdhQrGkYy3Yyq9vy7iiDh] Processing: Jobs\ThemeInstalling
[2023-04-17 09:52:59][dcjXRXkV2XgHdhQrGkYy3Yyq9vy7iiDh] Processed:  Jobs\ThemeInstalling
[2023-04-17 09:52:59][v8MkfSZlf6PtoXi8tqNcDb4SXCp6KTGk] Processing: Jobs\ThemeAssetUploading
[2023-04-17 09:53:02][v8MkfSZlf6PtoXi8tqNcDb4SXCp6KTGk] Processed:  Jobs\ThemeAssetUploading
[2023-04-17 09:53:02][Zv1ts0pDpCSiTVoWc9WqmszXj5QUYSF6] Processing: Jobs\ThemeAssetUploading
[2023-04-17 09:53:07][Zv1ts0pDpCSiTVoWc9WqmszXj5QUYSF6] Processed:  Jobs\ThemeAssetUploading
[2023-04-17 09:53:07][K69thLwVkwnkOccdR778hDdMBAL7U4pD] Processing: Jobs\ThemeInstalled
[2023-04-17 09:53:07][K69thLwVkwnkOccdR778hDdMBAL7U4pD] Processed:  Jobs\ThemeInstalled
[2023-04-17 09:53:07][SJPEBLJozJygXiIW9zfJNU1FSGtBmWek] Processing: Jobs\ThemeInstalled
[2023-04-17 09:53:07][SJPEBLJozJygXiIW9zfJNU1FSGtBmWek] Processed:  Jobs\ThemeInstalled

[2023-04-17 14:02:50] local.INFO: destination {
    "id": 799,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/17/1681711238.5778.1671140645"
} 
[2023-04-17 14:02:54] local.INFO: destination {
    "id": 799,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/17/1681711238.5778.1671140645"
} 
[2023-04-17 14:02:56] local.INFO: destination {
    "id": 800,
    "destination": null
} 
[2023-04-17 14:02:57] local.INFO: destination {
    "id": 800,
    "destination": null
} 
[2023-04-17 14:02:57] local.INFO: destination {
    "id": 801,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/17/1681711324.896.2108597779"
} 
[2023-04-17 14:02:59] local.INFO: destination {
    "id": 801,
    "destination": "E:\\wwwroot\\object\\storage\\app/theme_downloads/2023/04/17/1681711324.896.2108597779"
} 


 

 

PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.