laravel 队列执行完毕自动断开了

1. 运行环境

  • php 版本: 8.2
  • laravel 版本:11

2. 问题描述

GenerateVersion队列执行成功后,php artisan queue:work自动断开了

  1. 运行位置:

    GradeType::dispatch();
    foreach ($category as $cate){
     VirtualJob::dispatch($cate['id']);
     SingJob::dispatch($cate['id']);
     SentenceJob::dispatch($cate['id']);
     VocabularyJob::dispatch($cate['id']);
     WordJob::dispatch($cate['id']);
    }
    $directory =  Str::beforeLast(base_path(),'/').'/rjyst_module/'.$version_name;
    $version = $version_name;
    $fileExtension = 'm3u8';
    GenerateVersion::dispatch(version_name:$version_name,directory:$directory,version:$version,fileExtension:$fileExtension,isOpen: true);
  2. GenerateVersion 代码

    <?php
    namespace App\Jobs;
    use Carbon\Carbon;
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Support\Facades\File;
    use Illuminate\Support\Facades\Log;
    use Illuminate\Support\Str;
    class GenerateVersion implements ShouldQueue
    {
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
     protected string $version_name;
     protected string $directory;
     protected string $version;
     protected string $fileExtension;
     protected bool $isOpen;
     /**
      * Create a new job instance.
      */
     public function __construct(string $version_name, string $directory, string $version, string $fileExtension, bool $isOpen)
     {
         $this->version_name=$version_name;
         $this->directory=$directory;
         $this->version=$version;
         $this->fileExtension=$fileExtension;
         $this->isOpen=$isOpen;
     }
    
     /**
      * Execute the job.
      */
     public function handle(): void
     {
         Log::info("生成版本任务开始", ['version_name' => $this->version_name, 'version' => $this->version]);
         $outputFile = Str::beforeLast(base_path(),'/').'/rjyst_module/'.$this->version_name.'/'. "version_{$this->version}.json";
         $versionFile = Str::beforeLast(base_path(),'/').'/rjyst_module/'.$this->version_name.'/'. 'audio_version.json';
         File::put($outputFile, '');
         File::put($versionFile, '');
         $this->writeJson($versionFile, [
             'open' => $this->isOpen,
             'version' => $this->version
         ]);
         if (!File::exists($this->directory)) {
             Log::error("目录不存在: {$this->directory}");
             return;
         }
         $files = File::allFiles($this->directory);
         $fileExtension = $this->fileExtension;
         $filteredFiles = collect($files)->filter(function ($file) use ($fileExtension) {
             return $file->getExtension() === $fileExtension;
         });
         if ($filteredFiles->isEmpty()) {
             return;
         }
         $fileData = [];
         $directory = $this->directory;
         $filteredFiles->each(function ($file) use (&$fileData, $directory) {
             $relativePath = str_replace($directory, '', $file->getRealPath());
             $relativePath = str_replace('\\', '/', $relativePath);
             $modificationTime = $file->getMTime();
             $formattedTime = Carbon::createFromTimestamp($modificationTime)->toDateTimeString();
             $fileData[$relativePath] = $formattedTime;
         });
         $this->writeJson($outputFile, $fileData);
         Log::info("生成版本任务结束", ['version_name' => $this->version_name, 'version' => $this->version]);
     }
     private function writeJson($file, $data): void
     {
         $jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
         File::append($file, $jsonData . "\n");
     }
    }
  3. 执行结果:

laravel 队列执行完毕自动断开了

[2025-02-06 14:56:24] local.INFO: 生成版本任务开始 {"version_name":"1.0.1","version":"1.0.1"} 
[2025-02-06 14:56:59] local.INFO: 生成版本任务结束 {"version_name":"1.0.1","version":"1.0.1"} 

3. 期望结果

GenerateVersion 队列执行完毕后,应该在后台永久运行,而不是自动关闭。

laravel 队列执行完毕自动断开了

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
2周前 评论
sunny123456 2周前
Shine-x (楼主) 2周前
讨论数量: 7

你需要的是command吧,改造下使用守护进程拉起

2周前 评论

没太看懂 你不是自己ctrl+c 把队列进程终结了吗, 应该参照文档设置后台运行,或者用supervisor监控启动

2周前 评论
2周前 评论
sunny123456 2周前
Shine-x (楼主) 2周前

应该是进程达到了指定的内存--memory=128 这个是默认的值 128MB 就会被终止 建议使用守护进程

2周前 评论

可以先了解下什么是守护进程 或者 执行 nohup php ./artisan queue:work 2>&1 &

2周前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!