Skip to content

Commit 7a85c31

Browse files
committed
Add internal code support for stopping tasks if server is not running or continuing through on task error
1 parent 84483d3 commit 7a85c31

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

app/Jobs/Schedule/RunTaskJob.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use Carbon\CarbonImmutable;
88
use Pterodactyl\Models\Task;
99
use InvalidArgumentException;
10-
use Illuminate\Http\Response;
11-
use Pterodactyl\Models\Schedule;
1210
use Illuminate\Queue\SerializesModels;
1311
use Illuminate\Queue\InteractsWithQueue;
1412
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -79,17 +77,11 @@ public function handle(
7977
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');
8078
}
8179
} catch (Exception $exception) {
82-
if ($exception instanceof DaemonConnectionException) {
83-
// If the task "failed" because the server is offline and it was sending a command or
84-
// executing a power action (which shouldn't happen?) then just stop trying to process
85-
// the schedule, but don't actually log the failure.
86-
if ($this->task->action === Task::ACTION_POWER || $this->task->action === Task::ACTION_COMMAND) {
87-
// Do the thing
88-
if ($exception->getStatusCode() === Response::HTTP_CONFLICT) {
89-
}
90-
}
80+
// If this isn't a DaemonConnectionException on a task that allows for failures
81+
// throw the exception back up the chain so that the task is stopped.
82+
if (!($this->task->continue_on_failure && $exception instanceof DaemonConnectionException)) {
83+
throw $exception;
9184
}
92-
throw $exception;
9385
}
9486

9587
$this->markTaskNotQueued();

app/Services/Schedules/ProcessScheduleService.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Pterodactyl\Jobs\Schedule\RunTaskJob;
99
use Illuminate\Database\ConnectionInterface;
1010
use Pterodactyl\Exceptions\DisplayException;
11+
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
12+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
1113

1214
class ProcessScheduleService
1315
{
@@ -21,13 +23,19 @@ class ProcessScheduleService
2123
*/
2224
private $connection;
2325

26+
/**
27+
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
28+
*/
29+
private $serverRepository;
30+
2431
/**
2532
* ProcessScheduleService constructor.
2633
*/
27-
public function __construct(ConnectionInterface $connection, Dispatcher $dispatcher)
34+
public function __construct(ConnectionInterface $connection, DaemonServerRepository $serverRepository, Dispatcher $dispatcher)
2835
{
2936
$this->dispatcher = $dispatcher;
3037
$this->connection = $connection;
38+
$this->serverRepository = $serverRepository;
3139
}
3240

3341
/**
@@ -38,7 +46,7 @@ public function __construct(ConnectionInterface $connection, Dispatcher $dispatc
3846
public function handle(Schedule $schedule, bool $now = false)
3947
{
4048
/** @var \Pterodactyl\Models\Task $task */
41-
$task = $schedule->tasks()->orderBy('sequence_id', 'asc')->first();
49+
$task = $schedule->tasks()->orderBy('sequence_id')->first();
4250

4351
if (is_null($task)) {
4452
throw new DisplayException('Cannot process schedule for task execution: no tasks are registered.');
@@ -54,6 +62,30 @@ public function handle(Schedule $schedule, bool $now = false)
5462
});
5563

5664
$job = new RunTaskJob($task, $now);
65+
if ($schedule->only_when_online) {
66+
// Check that the server is currently in a starting or running state before executing
67+
// this schedule if this option has been set.
68+
try {
69+
$details = $this->serverRepository->setServer($schedule->server)->getDetails();
70+
$state = $details['state'] ?? 'offline';
71+
// If the server is stopping or offline just do nothing with this task.
72+
if (in_array($state, ['offline', 'stopping'])) {
73+
$job->failed();
74+
75+
return;
76+
}
77+
} catch (Exception $exception) {
78+
if (!$exception instanceof DaemonConnectionException) {
79+
// If we encountered some exception during this process that wasn't just an
80+
// issue connecting to Wings run the failed sequence for a job. Otherwise we
81+
// can just quietly mark the task as completed without actually running anything.
82+
$job->failed($exception);
83+
}
84+
$job->failed();
85+
86+
return;
87+
}
88+
}
5789

5890
if (!$now) {
5991
$this->dispatcher->dispatch($job->delay($task->time_offset));

0 commit comments

Comments
 (0)