From 36b80c4b6fedfbf152e29492a0736cbb35c73d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Thu, 1 Dec 2022 00:16:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=B9=E6=80=A7=EF=BC=9A=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E9=98=9F=E5=88=97=E4=B8=8E=E5=8F=96=E6=B6=88?= =?UTF-8?q?API=E9=99=90=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Panel.php | 13 +++++++++++++ app/Http/Kernel.php | 1 - app/Jobs/ProcessShell.php | 13 +++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/Panel.php b/app/Console/Commands/Panel.php index 84d2b43e..29726594 100644 --- a/app/Console/Commands/Panel.php +++ b/app/Console/Commands/Panel.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use App\Models\Plugin; use App\Models\Setting; +use App\Models\Task; use App\Models\User; use Illuminate\Console\Command; use Illuminate\Support\Facades\Hash; @@ -52,6 +53,9 @@ class Panel extends Command case 'writeMysqlPassword': $this->writeMysqlPassword(); break; + case 'cleanRunningTask': + $this->cleanRunningTask(); + break; default: $this->error('错误的操作'); break; @@ -192,4 +196,13 @@ class Panel extends Command Setting::query()->where('name', 'mysql_root_password')->update(['value' => $password]); $this->info('成功'); } + + /** + * 清理所有运行中的任务 + */ + private function cleanRunningTask(): void + { + Task::query()->where('status', 'running')->update(['status' => 'finished']); + $this->info('成功'); + } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index aa1eb38f..f826828a 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -41,7 +41,6 @@ class Kernel extends HttpKernel 'api' => [ \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, - 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], ]; diff --git a/app/Jobs/ProcessShell.php b/app/Jobs/ProcessShell.php index 046ba09b..5eb2a815 100644 --- a/app/Jobs/ProcessShell.php +++ b/app/Jobs/ProcessShell.php @@ -3,14 +3,13 @@ namespace App\Jobs; use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use App\Models\Task; -class ProcessShell implements ShouldQueue, ShouldBeUnique +class ProcessShell implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -52,9 +51,15 @@ class ProcessShell implements ShouldQueue, ShouldBeUnique */ public function handle(): void { + // 检查当前是否有任务正在运行 + $taskCheck = Task::query()->where('status', 'running')->get(); + if ($taskCheck->isNotEmpty()) { + $this->release(10); + return; + } // 查询任务 $task = Task::query()->where('id', $this->task_id)->first(); - echo $task->name . "开始执行".PHP_EOL; + echo $task->name."开始执行".PHP_EOL; // 更新任务状态为running $task->job_id = $this->job->getJobId(); $task->status = 'running'; @@ -63,6 +68,6 @@ class ProcessShell implements ShouldQueue, ShouldBeUnique // 更新任务状态 $task->status = 'finished'; $task->save(); - echo $task->name . "执行完毕".PHP_EOL; + echo $task->name."执行完毕".PHP_EOL; } }