laravel 队列执行完毕自动断开了
1. 运行环境
- php 版本: 8.2
- laravel 版本:11
2. 问题描述
在
GenerateVersion
队列执行成功后,php artisan queue:work
自动断开了
运行位置:
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);
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"); } }
执行结果:
[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 10 中文文档》