在 Laravel 6 中,设置静态变量后,在下一队列任务中获取时未被清除,会被沿用的解决
1、参考:https://www.shuijingwanwq.com/2022/07/15/6796/ 。在 Laravel 6 的队列任务中,当任务失败时的处理(设置与获取静态变量)。但是,其存在一个 Bug,在下一队列任务中如果未被重新设置,则会被沿用。分别打印2个任务中的静态变量:self::$destination。预期 755 的 destination 为 null,但是其沿用了 754 的值。如图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、如果在下一个队列任务中,有重新设置 静态变量为 null,则会被覆盖,分别打印2个任务中的静态变量:self::$destination。符合预期。如图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、但是,参考:在 Laravel 6 中,在队列任务中,重试时的指数回退(exponential backoff)的实现。当想将静态变量传递给下一个队列任务时,仍然会导致相互覆盖的问题。在 handle 方法中,预期 760 的值不为 null ,但是现在已经被 761 的 null 给覆盖。如图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、最终决定放弃静态变量的使用,将相应的值存储在队列任务相关的 MySQL 表记录中。队列任务之间不再相互影响,符合预期。如图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" }
近期评论