From 2d01c7b9887a4d8cf5b65bf6ecf0d302de2b5265 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 3 Oct 2020 19:47:52 -0700 Subject: [PATCH] Reset is_processing state of a schedule when toggling active/inactive; closes #2425 --- .../Api/Client/Servers/ScheduleController.php | 18 +++++-- .../Schedule/UpdateServerScheduleTest.php | 53 ++++++++++++++++--- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/ScheduleController.php b/app/Http/Controllers/Api/Client/Servers/ScheduleController.php index 593bbc259..d35b597ed 100644 --- a/app/Http/Controllers/Api/Client/Servers/ScheduleController.php +++ b/app/Http/Controllers/Api/Client/Servers/ScheduleController.php @@ -120,15 +120,27 @@ class ScheduleController extends ClientApiController */ public function update(UpdateScheduleRequest $request, Server $server, Schedule $schedule) { - $this->repository->update($schedule->id, [ + $active = (bool) $request->input('is_active'); + + $data = [ 'name' => $request->input('name'), 'cron_day_of_week' => $request->input('day_of_week'), 'cron_day_of_month' => $request->input('day_of_month'), 'cron_hour' => $request->input('hour'), 'cron_minute' => $request->input('minute'), - 'is_active' => (bool) $request->input('is_active'), + 'is_active' => $active, 'next_run_at' => $this->getNextRunAt($request), - ]); + ]; + + // Toggle the processing state of the scheduled task when it is enabled or disabled so that an + // invalid state can be reset without manual database intervention. + // + // @see https://github.com/pterodactyl/panel/issues/2425 + if ($schedule->is_active !== $active) { + $data['is_processing'] = false; + } + + $this->repository->update($schedule->id, $data); return $this->fractal->item($schedule->refresh()) ->transformWith($this->getTransformer(ScheduleTransformer::class)) diff --git a/tests/Integration/Api/Client/Server/Schedule/UpdateServerScheduleTest.php b/tests/Integration/Api/Client/Server/Schedule/UpdateServerScheduleTest.php index d2d3132ff..ec60a4137 100644 --- a/tests/Integration/Api/Client/Server/Schedule/UpdateServerScheduleTest.php +++ b/tests/Integration/Api/Client/Server/Schedule/UpdateServerScheduleTest.php @@ -9,6 +9,20 @@ use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase; class UpdateServerScheduleTest extends ClientApiIntegrationTestCase { + /** + * The data to use when updating a schedule. + * + * @var array + */ + private $updateData = [ + 'name' => 'Updated Schedule Name', + 'minute' => '5', + 'hour' => '*', + 'day_of_week' => '*', + 'day_of_month' => '*', + 'is_active' => false, + ]; + /** * Test that a schedule can be updated. * @@ -24,14 +38,7 @@ class UpdateServerScheduleTest extends ClientApiIntegrationTestCase $expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*'); $response = $this->actingAs($user) - ->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", [ - 'name' => 'Updated Schedule Name', - 'minute' => '5', - 'hour' => '*', - 'day_of_week' => '*', - 'day_of_month' => '*', - 'is_active' => false, - ]); + ->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData); $schedule = $schedule->refresh(); @@ -74,6 +81,36 @@ class UpdateServerScheduleTest extends ClientApiIntegrationTestCase ->assertForbidden(); } + /** + * Test that the "is_processing" field gets reset to false when the schedule is enabled + * or disabled so that an invalid state can be more easily fixed. + * + * @see https://github.com/pterodactyl/panel/issues/2425 + */ + public function testScheduleIsProcessingIsSetToFalseWhenActiveStateChanges() + { + [$user, $server] = $this->generateTestAccount(); + + /** @var \Pterodactyl\Models\Schedule $schedule */ + $schedule = factory(Schedule::class)->create([ + 'server_id' => $server->id, + 'is_active' => true, + 'is_processing' => true, + ]); + + $this->assertTrue($schedule->is_active); + $this->assertTrue($schedule->is_processing); + + $response = $this->actingAs($user) + ->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData); + + $schedule = $schedule->refresh(); + + $response->assertOk(); + $this->assertFalse($schedule->is_active); + $this->assertFalse($schedule->is_processing); + } + /** * @return array */