From 552b9d3c33d9528f02b25d23bd2fc5a2f86bc08e Mon Sep 17 00:00:00 2001 From: Julien Tant Date: Sat, 24 Apr 2021 15:06:21 -0700 Subject: [PATCH 1/3] Add possibility to run disabled cron --- .../Api/Client/Servers/ScheduleController.php | 4 ---- app/Jobs/Schedule/RunTaskJob.php | 14 ++++++++++---- app/Services/Schedules/ProcessScheduleService.php | 2 +- .../server/schedules/ScheduleEditContainer.tsx | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/ScheduleController.php b/app/Http/Controllers/Api/Client/Servers/ScheduleController.php index 320133595..5b2609e58 100644 --- a/app/Http/Controllers/Api/Client/Servers/ScheduleController.php +++ b/app/Http/Controllers/Api/Client/Servers/ScheduleController.php @@ -156,10 +156,6 @@ class ScheduleController extends ClientApiController */ public function execute(TriggerScheduleRequest $request, Server $server, Schedule $schedule) { - if (!$schedule->is_active) { - throw new BadRequestHttpException('Cannot trigger schedule exection for a schedule that is not currently active.'); - } - $this->service->handle($schedule, true); return new JsonResponse([], JsonResponse::HTTP_ACCEPTED); diff --git a/app/Jobs/Schedule/RunTaskJob.php b/app/Jobs/Schedule/RunTaskJob.php index 3dd8e920d..b2b51578c 100644 --- a/app/Jobs/Schedule/RunTaskJob.php +++ b/app/Jobs/Schedule/RunTaskJob.php @@ -27,13 +27,19 @@ class RunTaskJob extends Job implements ShouldQueue */ public $task; + /** + * @var bool + */ + public $now; + /** * RunTaskJob constructor. */ - public function __construct(Task $task) + public function __construct(Task $task, $now = false) { $this->queue = config('pterodactyl.queues.standard'); $this->task = $task; + $this->now = $now; } /** @@ -46,8 +52,8 @@ class RunTaskJob extends Job implements ShouldQueue InitiateBackupService $backupService, DaemonPowerRepository $powerRepository ) { - // Do not process a task that is not set to active. - if (!$this->task->schedule->is_active) { + // Do not process a task that is not set to active, unless it's been trigger by the run now API. + if ($this->task->schedule->is_active && !$this->now) { $this->markTaskNotQueued(); $this->markScheduleComplete(); @@ -101,7 +107,7 @@ class RunTaskJob extends Job implements ShouldQueue $nextTask->update(['is_queued' => true]); - $this->dispatch((new self($nextTask))->delay($nextTask->time_offset)); + $this->dispatch((new self($nextTask, $this->now))->delay($nextTask->time_offset)); } /** diff --git a/app/Services/Schedules/ProcessScheduleService.php b/app/Services/Schedules/ProcessScheduleService.php index 23c9ea5e8..9e62d45cb 100644 --- a/app/Services/Schedules/ProcessScheduleService.php +++ b/app/Services/Schedules/ProcessScheduleService.php @@ -53,7 +53,7 @@ class ProcessScheduleService $task->update(['is_queued' => true]); }); - $job = new RunTaskJob($task); + $job = new RunTaskJob($task, $now); if (!$now) { $this->dispatcher->dispatch($job->delay($task->time_offset)); diff --git a/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx b/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx index df0af5b99..60d7ca943 100644 --- a/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx +++ b/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx @@ -161,7 +161,7 @@ export default () => { onDeleted={() => history.push(`/server/${id}/schedules`)} /> - {schedule.isActive && schedule.tasks.length > 0 && + {schedule.tasks.length > 0 && From f7f972b33dac2885821ac5d551a0d0617df13f75 Mon Sep 17 00:00:00 2001 From: Julien Tant Date: Sat, 24 Apr 2021 18:18:29 -0700 Subject: [PATCH 2/3] rename now variable & fix condition --- app/Jobs/Schedule/RunTaskJob.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Jobs/Schedule/RunTaskJob.php b/app/Jobs/Schedule/RunTaskJob.php index b2b51578c..f60711f43 100644 --- a/app/Jobs/Schedule/RunTaskJob.php +++ b/app/Jobs/Schedule/RunTaskJob.php @@ -30,16 +30,16 @@ class RunTaskJob extends Job implements ShouldQueue /** * @var bool */ - public $now; + public $manualRun; /** * RunTaskJob constructor. */ - public function __construct(Task $task, $now = false) + public function __construct(Task $task, $manualRun = false) { $this->queue = config('pterodactyl.queues.standard'); $this->task = $task; - $this->now = $now; + $this->manualRun = $manualRun; } /** @@ -52,8 +52,8 @@ class RunTaskJob extends Job implements ShouldQueue InitiateBackupService $backupService, DaemonPowerRepository $powerRepository ) { - // Do not process a task that is not set to active, unless it's been trigger by the run now API. - if ($this->task->schedule->is_active && !$this->now) { + // Do not process a task that is not set to active, unless it's been manually triggered. + if (!$this->task->schedule->is_active && !$this->manualRun) { $this->markTaskNotQueued(); $this->markScheduleComplete(); @@ -107,7 +107,7 @@ class RunTaskJob extends Job implements ShouldQueue $nextTask->update(['is_queued' => true]); - $this->dispatch((new self($nextTask, $this->now))->delay($nextTask->time_offset)); + $this->dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset)); } /** From b10f6184e043a5deb3a45bc7b607c149a728f3f5 Mon Sep 17 00:00:00 2001 From: Julien Tant Date: Sat, 24 Apr 2021 18:30:48 -0700 Subject: [PATCH 3/3] remove the test preventing disabled schedule to be manually executed --- .../Server/Schedule/ExecuteScheduleTest.php | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php b/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php index 7c9d34d18..9964691aa 100644 --- a/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php +++ b/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php @@ -51,26 +51,6 @@ class ExecuteScheduleTest extends ClientApiIntegrationTestCase }); } - /** - * Test that the schedule is not executed if it is not currently active. - */ - public function testScheduleIsNotExecutedIfNotActive() - { - [$user, $server] = $this->generateTestAccount(); - - /** @var \Pterodactyl\Models\Schedule $schedule */ - $schedule = Schedule::factory()->create([ - 'server_id' => $server->id, - 'is_active' => false, - ]); - - $response = $this->actingAs($user)->postJson($this->link($schedule, '/execute')); - - $response->assertStatus(Response::HTTP_BAD_REQUEST); - $response->assertJsonPath('errors.0.code', 'BadRequestHttpException'); - $response->assertJsonPath('errors.0.detail', 'Cannot trigger schedule exection for a schedule that is not currently active.'); - } - /** * Test that a user without the schedule update permission cannot execute it. */