Skip to content

Commit 2d01c7b

Browse files
committed
Reset is_processing state of a schedule when toggling active/inactive; closes pterodactyl#2425
1 parent 57457f0 commit 2d01c7b

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

app/Http/Controllers/Api/Client/Servers/ScheduleController.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,27 @@ public function view(ViewScheduleRequest $request, Server $server, Schedule $sch
120120
*/
121121
public function update(UpdateScheduleRequest $request, Server $server, Schedule $schedule)
122122
{
123-
$this->repository->update($schedule->id, [
123+
$active = (bool) $request->input('is_active');
124+
125+
$data = [
124126
'name' => $request->input('name'),
125127
'cron_day_of_week' => $request->input('day_of_week'),
126128
'cron_day_of_month' => $request->input('day_of_month'),
127129
'cron_hour' => $request->input('hour'),
128130
'cron_minute' => $request->input('minute'),
129-
'is_active' => (bool) $request->input('is_active'),
131+
'is_active' => $active,
130132
'next_run_at' => $this->getNextRunAt($request),
131-
]);
133+
];
134+
135+
// Toggle the processing state of the scheduled task when it is enabled or disabled so that an
136+
// invalid state can be reset without manual database intervention.
137+
//
138+
// @see https://github.com/pterodactyl/panel/issues/2425
139+
if ($schedule->is_active !== $active) {
140+
$data['is_processing'] = false;
141+
}
142+
143+
$this->repository->update($schedule->id, $data);
132144

133145
return $this->fractal->item($schedule->refresh())
134146
->transformWith($this->getTransformer(ScheduleTransformer::class))

tests/Integration/Api/Client/Server/Schedule/UpdateServerScheduleTest.php

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99

1010
class UpdateServerScheduleTest extends ClientApiIntegrationTestCase
1111
{
12+
/**
13+
* The data to use when updating a schedule.
14+
*
15+
* @var array
16+
*/
17+
private $updateData = [
18+
'name' => 'Updated Schedule Name',
19+
'minute' => '5',
20+
'hour' => '*',
21+
'day_of_week' => '*',
22+
'day_of_month' => '*',
23+
'is_active' => false,
24+
];
25+
1226
/**
1327
* Test that a schedule can be updated.
1428
*
@@ -24,14 +38,7 @@ public function testScheduleCanBeUpdated($permissions)
2438
$expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*');
2539

2640
$response = $this->actingAs($user)
27-
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", [
28-
'name' => 'Updated Schedule Name',
29-
'minute' => '5',
30-
'hour' => '*',
31-
'day_of_week' => '*',
32-
'day_of_month' => '*',
33-
'is_active' => false,
34-
]);
41+
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
3542

3643
$schedule = $schedule->refresh();
3744

@@ -74,6 +81,36 @@ public function testErrorIsReturnedIfSubuserDoesNotHavePermissionToModifySchedul
7481
->assertForbidden();
7582
}
7683

84+
/**
85+
* Test that the "is_processing" field gets reset to false when the schedule is enabled
86+
* or disabled so that an invalid state can be more easily fixed.
87+
*
88+
* @see https://github.com/pterodactyl/panel/issues/2425
89+
*/
90+
public function testScheduleIsProcessingIsSetToFalseWhenActiveStateChanges()
91+
{
92+
[$user, $server] = $this->generateTestAccount();
93+
94+
/** @var \Pterodactyl\Models\Schedule $schedule */
95+
$schedule = factory(Schedule::class)->create([
96+
'server_id' => $server->id,
97+
'is_active' => true,
98+
'is_processing' => true,
99+
]);
100+
101+
$this->assertTrue($schedule->is_active);
102+
$this->assertTrue($schedule->is_processing);
103+
104+
$response = $this->actingAs($user)
105+
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
106+
107+
$schedule = $schedule->refresh();
108+
109+
$response->assertOk();
110+
$this->assertFalse($schedule->is_active);
111+
$this->assertFalse($schedule->is_processing);
112+
}
113+
77114
/**
78115
* @return array
79116
*/

0 commit comments

Comments
 (0)