Skip to content

Commit b07fdc1

Browse files
committed
Don't run schedules when a server is suspended or installing; closes pterodactyl#4008
1 parent ab37ee8 commit b07fdc1

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

app/Console/Commands/Schedule/ProcessRunnableCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Console\Command;
88
use Pterodactyl\Models\Schedule;
99
use Illuminate\Support\Facades\Log;
10+
use Illuminate\Database\Eloquent\Builder;
1011
use Pterodactyl\Services\Schedules\ProcessScheduleService;
1112

1213
class ProcessRunnableCommand extends Command
@@ -26,7 +27,9 @@ class ProcessRunnableCommand extends Command
2627
*/
2728
public function handle()
2829
{
29-
$schedules = Schedule::query()->with('tasks')
30+
$schedules = Schedule::query()
31+
->with('tasks')
32+
->whereRelation('server', fn (Builder $builder) => $builder->whereNull('status'))
3033
->where('is_active', true)
3134
->where('is_processing', false)
3235
->whereRaw('next_run_at <= NOW()')

app/Jobs/Schedule/RunTaskJob.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public function handle(
6161
}
6262

6363
$server = $this->task->server;
64+
// If we made it to this point and the server status is not null it means the
65+
// server was likely suspended or marked as reinstalling after the schedule
66+
// was queued up. Just end the task right now — this should be a very rare
67+
// condition.
68+
if (!is_null($server->status)) {
69+
$this->failed();
70+
71+
return;
72+
}
73+
6474
// Perform the provided task against the daemon.
6575
try {
6676
switch ($this->task->action) {

tests/Integration/Jobs/Schedule/RunTaskJobTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Pterodactyl\Tests\Integration\Jobs\Schedule;
44

55
use Mockery;
6+
use Carbon\Carbon;
7+
use DateTimeInterface;
68
use Carbon\CarbonImmutable;
79
use GuzzleHttp\Psr7\Request;
810
use Pterodactyl\Models\Task;
@@ -146,6 +148,36 @@ public function testExceptionDuringRunIsHandledCorrectly(bool $continueOnFailure
146148
}
147149
}
148150

151+
/**
152+
* Test that a schedule is not executed if the server is suspended.
153+
*
154+
* @see https://github.com/pterodactyl/panel/issues/4008
155+
*/
156+
public function testTaskIsNotRunIfServerIsSuspended()
157+
{
158+
$server = $this->createServerModel([
159+
'status' => Server::STATUS_SUSPENDED,
160+
]);
161+
162+
$schedule = Schedule::factory()->for($server)->create([
163+
'last_run_at' => Carbon::now()->subHour(),
164+
]);
165+
166+
$task = Task::factory()->for($schedule)->create([
167+
'action' => Task::ACTION_POWER,
168+
'payload' => 'start',
169+
]);
170+
171+
Bus::dispatchNow(new RunTaskJob($task));
172+
173+
$task->refresh();
174+
$schedule->refresh();
175+
176+
$this->assertFalse($task->is_queued);
177+
$this->assertFalse($schedule->is_processing);
178+
$this->assertTrue(Carbon::now()->isSameAs(DateTimeInterface::ATOM, $schedule->last_run_at));
179+
}
180+
149181
/**
150182
* @return array
151183
*/

0 commit comments

Comments
 (0)