In Laravel 6, when the maximum number of times a task can be tried is not supported for a task greater than 1 in a queue task, split the queue task
1. In Laravel 6, in the queue task, when the maximum number of tasks that the task can try is greater than 1, it fails on the second run: Must be an instance of app\models\theme, null given. The reason is that the queue task does not support trying again. as shown in Figure 1
/**
* 任务可以尝试的最大次数。
*
* @var int
*/
public $tries = 2;
Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Modules\ThemeStoreDb\ThemeSetting\ThemeSettingMigration::migrateBladeSettingsData() must be an instance of App\Models\Theme, null given, called in E:\wwwroot\object\Modules\ThemeStoreDb\Handlers\ThemeHandler.php on line 129 in E:\wwwroot\object\Modules\ThemeStoreDb\ThemeSetting\ThemeSettingMigration.php:42
Stack trace:
#0 E:\wwwroot\object\Modules\ThemeStoreDb\Handlers\ThemeHandler.php(129): Modules\ThemeStoreDb\ThemeSetting\ThemeSettingMigration->migrateBladeSettingsData(NULL, Object(Modules\ThemeStoreDb\Entities\ThemeInstallationTask))
#1 E:\wwwroot\object\Modules\ThemeStoreDb\Handlers\ThemeHandler.php(64): Modules\ThemeStoreDb\Handlers\ThemeHandler->migrateSettingsData(Object(Modules\ThemeStoreDb\Entities\ThemeInstallation), Object(Modules\ThemeStoreDb\Entities\ThemeInstallationTask), Array)
#2 E:\wwwroot\object\Modules\ThemeStoreDb\Jobs\ThemeInstallationJob.php(203): Modules\ThemeStoreDb\Handlers\ThemeHandler->migrateThemeSettings(Object(Modules\ThemeStoreDb\Entities\ThemeInstallation), Object(Modules\ThemeStoreDb\Entities\ThemeInstallationTask))
#3 E:\wwwroot\object\Modules\ThemeStoreDb\Jobs\ThemeInstallationJob.php(90): Modules\ThemeStoreDb\Jobs\ThemeInstallationJob->migrateThemeSettings()
#4 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(36): Modules\ThemeStoreDb\Jobs\ThemeInstallationJob->handle()
#5 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\Util.php(37): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#7 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#8 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\Container.php(590): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#9 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(94): Illuminate\Container\Container->call(Array)
#10 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(130): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Modules\ThemeStoreDb\Jobs\ThemeInstallationJob))
#11 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\ThemeStoreDb\Jobs\ThemeInstallationJob))
#12 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#13 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(83): Illuminate\Bus\Dispatcher->dispatchNow(Object(Modules\ThemeStoreDb\Jobs\ThemeInstallationJob), false)
#14 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(130): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue\{closure}(Object(Modules\ThemeStoreDb\Jobs\ThemeInstallationJob))
#15 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\ThemeStoreDb\Jobs\ThemeInstallationJob))
#16 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(85): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(59): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware(Object(Illuminate\Queue\Jobs\RedisJob), Object(Modules\ThemeStoreDb\Jobs\ThemeInstallationJob))
#18 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(88): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#19 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(368): Illuminate\Queue\Jobs\Job->fire()
#20 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(314): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))
#21 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(134): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\RedisJob), 'redis', Object(Illuminate\Queue\WorkerOptions))
#22 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(112): Illuminate\Queue\Worker->daemon('redis', 'default', Object(Illuminate\Queue\WorkerOptions))
#23 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(96): Illuminate\Queue\Console\WorkCommand->runWorker('redis', 'default')
#24 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle()
#25 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\Util.php(37): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#26 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#27 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#28 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Container\Container.php(590): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#29 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Console\Command.php(134): Illuminate\Container\Container->call(Array)
#30 E:\wwwroot\object\vendor\symfony\console\Command\Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#31 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Console\Command.php(121): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#32 E:\wwwroot\object\vendor\symfony\console\Application.php(1009): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 E:\wwwroot\object\vendor\symfony\console\Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 E:\wwwroot\object\vendor\symfony\console\Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#35 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Console\Application.php(93): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#36 E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(131): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#37 E:\wwwroot\object\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#38 {main}
2. When the queue task is run for the first time, some records in the data table are deleted. When the second run, the records that have been deleted need to be used again, resulting in failure on the second run. The compatibility here is not easy to handle, and finally decided to split the queue task.
3. Split the task link that needs to be tried again into a separate queue task, and the maximum number of times the task can be tried is greater than 1. For the original queue task, the maximum number of times you can try is 1 to avoid errors when running again.
/**
* 任务可以尝试的最大次数。
*
* @var int
*/
public $tries = 1;
// 推送任务到队列
UploadThemeAssetToCdnJob::dispatch($this->themeInstallationTask, $this->absolutePath, $this->destination);
/**
* 任务可以尝试的最大次数。
*
* @var int
*/
public $tries = 3;
4. When the new queue task failed, the original queue task has been tried 1 time, and the new queue task has been tried 3 times. in line with expectations. as shown in Figure 2
PS E:\wwwroot\object> php artisan queue:work
[2023-04-06 17:01:50][FSn8E0VIfENgDzP2CydEHcP4ZKdub5vT] Processing: Modules\ThemeStoreDb\Jobs\ThemeInstallationJob
[2023-04-06 17:02:39][FSn8E0VIfENgDzP2CydEHcP4ZKdub5vT] Processed: Modules\ThemeStoreDb\Jobs\ThemeInstallationJob
[2023-04-06 17:02:39][Tf9DYc8hDoW6f3GVH2889yb9ZA1jSyWX] Processing: Modules\ThemeStoreDb\Jobs\UploadThemeAssetToCdnJob
[2023-04-06 17:02:50][Tf9DYc8hDoW6f3GVH2889yb9ZA1jSyWX] Processing: Modules\ThemeStoreDb\Jobs\UploadThemeAssetToCdnJob
[2023-04-06 17:02:50][Tf9DYc8hDoW6f3GVH2889yb9ZA1jSyWX] Processing: Modules\ThemeStoreDb\Jobs\UploadThemeAssetToCdnJob
[2023-04-06 17:02:50][Tf9DYc8hDoW6f3GVH2889yb9ZA1jSyWX] Failed: Modules\ThemeStoreDb\Jobs\UploadThemeAssetToCdnJob
5. When the 3 consecutive queue tasks are successful, each queue task is tried once. in line with expectations. as shown in Figure 3
PS E:\wwwroot\object> php artisan queue:work
[2023-04-06 17:21:13][mJUCYgune7X37F4pBxbBTfqvKzOTi1I4] Processing: Modules\ThemeStoreDb\Jobs\ThemeInstallationJob
[2023-04-06 17:21:56][mJUCYgune7X37F4pBxbBTfqvKzOTi1I4] Processed: Modules\ThemeStoreDb\Jobs\ThemeInstallationJob
[2023-04-06 17:21:56][245oFV2O4NuYfD81b9gwKE4rnG6iuSCa] Processing: Modules\ThemeStoreDb\Jobs\UploadThemeAssetToCdnJob
[2023-04-06 17:22:02][245oFV2O4NuYfD81b9gwKE4rnG6iuSCa] Processed: Modules\ThemeStoreDb\Jobs\UploadThemeAssetToCdnJob
[2023-04-06 17:22:02][htBRuBPcfiKFXebjeCLuk8Vf6Lx55I5I] Processing: Modules\ThemeStoreDb\Jobs\ThemeInstallationedJob
[2023-04-06 17:22:04][htBRuBPcfiKFXebjeCLuk8Vf6Lx55I5I] Processed: Modules\ThemeStoreDb\Jobs\ThemeInstallationedJob


