没有不值得去解决的问题,也没有不值得去学习的技术!

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

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

    }


   

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理