在 Laravel 6 的队列任务中,当任务失败时的处理(设置与获取静态变量)

1、在一个队列任务中,需要先下载 zip 文件,然后解压缩,现在当解压缩成功后,需要删除已下载的 zip 文件。现已实现。

    /**
     * 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、当解压缩失败:Call to undefined function Modules\ThemeStoreDB\Handlers\checkhere() 时,也需要删除掉对应的 zip 文件,就需要在 failed(Exception $exception) 方法中实现。但是,需要获取到 zip 文件的绝对路径。 暂未实现。查看未删除的 zip 文件。如图1

图1

3、在表:failed_jobs 中,查看字段: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、zip 文件的绝对路径的获取,一种方案是将其存入至 MySQL 表中,另一种方案就是赋值给 Job 对象属性,决定采用第二种方案。


    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、但是解压缩失败后,并未删除掉 zip 文件,原因在于 $this->absolutePath 为 null 。在方法 handle() 中是有值的,但是在 failed() 中为 null。

6、决定将 absolutePath 调整为静态变量。在 failed() 中其值为:E:\wwwroot\object\storage\app/theme_downloads/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') . '/')));
        }

    }

 

 

永夜