Processing when the task fails in Laravel 6’s queue task (set and get static variables)
1. In a queue task, you need to download the zip file first, and then decompress it. Now, after the decompression is successful, you need to delete the downloaded zip file. Now implemented.
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// 下载 ZIP
$downloadZipRes = $this->downloadZip();
// 解压缩 ZIP
$this->unzip($downloadZipRes['destination'], $downloadZipRes['absolutePath']);
}
/**
* 解压缩 ZIP
*
* @param string $destination 解压缩至的本地目标路径
* @param string $absolutePath 待解压缩的文件的绝对路径
* @return void
*/
private function unzip($destination, $absolutePath)
{
$this->themeInstallationTask->step = ThemeInstallationTask::STEP_UNZIP;
$this->themeInstallationTask->save();
app(ZipHandler::class)->unzip($destination, $absolutePath);
// 删除 ZIP
Storage::disk('local')->delete(substr($absolutePath, strlen(storage_path('app') . '/')));
}
/**
* 解压缩 ZIP
* @param string $destination 解压缩至的本地目标路径
* @param string $entrie 待解压缩的文件的绝对路径
* @return void
*/
public function unzip($destination, $entrie)
{
checkhere();
if ($this->zipArchive->open($entrie) === TRUE) {
$this->zipArchive->extractTo($destination);
$this->zipArchive->close();
} else {
abort(500, 'Unzip failed.');
}
}
2. When the decompression fails: call to undefined function modules\themestoredb\handlers\checkhere(), you also need to delete the corresponding zip file, you need to Failed(Exception $Exception) method is implemented. However, you need to get the absolute path to the zip file. Not yet realized. View undeleted zip files. as shown in Figure 1
3. In the table: failed_jobs, check the value of the field: payload
{
"displayName": "Modules\\ThemeStoreDB\\Jobs\\ThemeInstallationJob",
"job": "Illuminate\\Queue\\CallQueuedHandler@call",
"maxTries": 1,
"delay": null,
"timeout": 600,
"timeoutAt": null,
"data": {
"commandName": "Modules\\ThemeStoreDB\\Jobs\\ThemeInstallationJob",
"command": "O:46:\"Modules\\ThemeStoreDB\\Jobs\\ThemeInstallationJob\":11:{s:5:\"tries\";i:1;s:7:\"timeout\";i:600;s:24:\"\u0000*\u0000themeInstallationTask\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:51:\"Modules\\ThemeStoreDB\\Entities\\ThemeInstallationTask\";s:2:\"id\";i:22;s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"mysql\";}s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"
},
"telescope_uuid": "96a60a99-dbb8-40a4-8054-2694435b46be",
"tenant_name": "chengdu-wangqiang-object-local",
"id": "lX7QOFkBy5VwjNBSx5OekUISzvXU7ess",
"attempts": 0
}
4. To obtain the absolute path of the zip file, one solution is to store it in the MySQL table, and the other solution is to assign a value to the Job object attribute and decide to use the second scheme.
private $absolutePath; // 待解压缩的 ZIP 文件的绝对路径
private $destination; // 解压缩 ZIP 文件后的目录
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// 下载 ZIP
$downloadZipRes = $this->downloadZip();
// 解压缩 ZIP
$this->absolutePath = $downloadZipRes['absolutePath'];
$this->unzip($downloadZipRes['destination']);
}
/**
* 任务失败的处理过程
*
* @param Exception $exception
* @return void
*/
public function failed(Exception $exception)
{
if (isset($this->absolutePath)) {
// 删除 ZIP
Storage::disk('local')->delete(substr($this->absolutePath, strlen(storage_path('app') . '/')));
}
}
5. But after the decompression fails, the zip file is not deleted because $this->ABSOLutePath is null . There are values in the method handle() , but null in failed() .
6. Decide to adjust the AbsolutePath to a static variable. in failed() The value is: E:\wwwroot\object\storage\app/theme_downl OADS/2022/06/29/1656468032.6555.1798260415.zip
private static $absolutePath; // 待解压缩的 ZIP 文件的绝对路径
private static $destination; // 解压缩 ZIP 文件后的目录
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// 下载 ZIP
$downloadZipRes = $this->downloadZip();
// 解压缩 ZIP
self::$absolutePath = $downloadZipRes['absolutePath'];
$this->unzip($downloadZipRes['destination']);
}
/**
* 任务失败的处理过程
*
* @param Exception $exception
* @return void
*/
public function failed(Exception $exception)
{
if (isset(self::$absolutePath)) {
// 删除 ZIP
Storage::disk('local')->delete(substr(self::$absolutePath, strlen(storage_path('app') . '/')));
}
}
