Reset is_processing state of a schedule when toggling active/inactive; closes #2425

This commit is contained in:
Dane Everitt 2020-10-03 19:47:52 -07:00
parent 57457f0e9c
commit 2d01c7b988
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 60 additions and 11 deletions

View file

@ -120,15 +120,27 @@ class ScheduleController extends ClientApiController
*/ */
public function update(UpdateScheduleRequest $request, Server $server, Schedule $schedule) 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'), 'name' => $request->input('name'),
'cron_day_of_week' => $request->input('day_of_week'), 'cron_day_of_week' => $request->input('day_of_week'),
'cron_day_of_month' => $request->input('day_of_month'), 'cron_day_of_month' => $request->input('day_of_month'),
'cron_hour' => $request->input('hour'), 'cron_hour' => $request->input('hour'),
'cron_minute' => $request->input('minute'), 'cron_minute' => $request->input('minute'),
'is_active' => (bool) $request->input('is_active'), 'is_active' => $active,
'next_run_at' => $this->getNextRunAt($request), '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()) return $this->fractal->item($schedule->refresh())
->transformWith($this->getTransformer(ScheduleTransformer::class)) ->transformWith($this->getTransformer(ScheduleTransformer::class))

View file

@ -9,6 +9,20 @@ use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
class UpdateServerScheduleTest extends 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. * Test that a schedule can be updated.
* *
@ -24,14 +38,7 @@ class UpdateServerScheduleTest extends ClientApiIntegrationTestCase
$expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*'); $expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*');
$response = $this->actingAs($user) $response = $this->actingAs($user)
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", [ ->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
'name' => 'Updated Schedule Name',
'minute' => '5',
'hour' => '*',
'day_of_week' => '*',
'day_of_month' => '*',
'is_active' => false,
]);
$schedule = $schedule->refresh(); $schedule = $schedule->refresh();
@ -74,6 +81,36 @@ class UpdateServerScheduleTest extends ClientApiIntegrationTestCase
->assertForbidden(); ->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 * @return array
*/ */