Files

152 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2017-09-16 23:10:00 -05:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2017-09-16 23:10:00 -05:00
namespace Pterodactyl\Jobs\Schedule;
2020-10-05 20:46:41 -07:00
use Carbon\CarbonImmutable;
use Illuminate\Bus\Queueable;
2017-09-16 23:10:00 -05:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\DispatchesJobs;
2026-10-05 18:10:18 -04:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use InvalidArgumentException;
use Pterodactyl\Contracts\Backups\InitiatesBackups;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Daemon;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Task;
use Throwable;
2017-09-16 23:10:00 -05:00
class RunTaskJob implements ShouldQueue
2017-09-16 23:10:00 -05:00
{
use DispatchesJobs;
2026-10-05 18:10:18 -04:00
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2017-09-16 23:10:00 -05:00
2026-10-05 18:10:18 -04:00
public int $timeout = 30;
/** @var list<int> */
public array $backoff = [10, 30, 60];
public int $tries = 3;
2017-09-16 23:10:00 -05:00
/**
* RunTaskJob constructor.
*/
2022-10-14 10:59:20 -06:00
public function __construct(public Task $task, public bool $manualRun = false)
2017-09-16 23:10:00 -05:00
{
2022-10-14 10:59:20 -06:00
$this->queue = 'standard';
2017-09-16 23:10:00 -05:00
}
/**
* Run the job and send actions to the daemon running the server.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
2017-09-16 23:10:00 -05:00
*/
public function handle(
2026-10-05 18:10:18 -04:00
InitiatesBackups $backupService,
): void {
2021-04-24 18:18:29 -07:00
// Do not process a task that is not set to active, unless it's been manually triggered.
2026-10-05 18:10:18 -04:00
if (! $this->task->schedule->is_active && ! $this->manualRun) {
$this->markTaskNotQueued();
$this->markScheduleComplete();
return;
}
2020-10-05 20:46:41 -07:00
$server = $this->task->server;
// If we made it to this point and the server status is not null it means the
// server was likely suspended or marked as reinstalling after the schedule
2026-10-05 18:10:18 -04:00
// was queued up. Just end the task right now - this should be a very rare
// condition.
2026-10-05 18:10:18 -04:00
if (($server->status) !== null) {
$this->failed();
return;
}
2018-05-13 10:50:56 -04:00
// Perform the provided task against the daemon.
try {
2026-10-05 18:10:18 -04:00
match ($this->task->action) {
Task::ACTION_POWER => Daemon::server($server)->power($this->task->payload),
Task::ACTION_COMMAND => Daemon::server($server)->commands($this->task->payload),
Task::ACTION_BACKUP => $backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->initiate($server, null, true),
default => throw new InvalidArgumentException('Invalid task action provided: '.$this->task->action),
};
} catch (Throwable $throwable) {
// If this isn't a DaemonConnectionException on a task that allows for failures
// throw the exception back up the chain so that the task is stopped.
2026-10-05 18:10:18 -04:00
throw_if(! $this->task->continue_on_failure || ! $throwable instanceof DaemonConnectionException, $throwable);
2017-09-16 23:10:00 -05:00
}
$this->markTaskNotQueued();
2020-10-05 20:46:41 -07:00
$this->queueNextTask();
2017-09-16 23:10:00 -05:00
}
/**
* Handle a failure while sending the action to the daemon or otherwise processing the job.
*/
2026-10-05 18:10:18 -04:00
public function failed(?Throwable $exception = null): void
2017-09-16 23:10:00 -05:00
{
2026-10-05 18:10:18 -04:00
$this->task->getConnection()->transaction(function (): void {
$schedule = Schedule::query()->without('tasks')->lockForUpdate()->findOrFail($this->task->schedule_id);
$schedule->tasks()->where('is_queued', true)->update(['is_queued' => false]);
$schedule->forceFill(['is_processing' => false, 'last_run_at' => CarbonImmutable::now()])->saveOrFail();
});
2017-09-16 23:10:00 -05:00
}
/**
* Get the next task in the schedule and queue it for running after the defined period of wait time.
*/
2026-10-05 18:10:18 -04:00
private function queueNextTask(): void
2017-09-16 23:10:00 -05:00
{
2020-10-05 20:46:41 -07:00
$nextTask = Task::query()->where('schedule_id', $this->task->schedule_id)
2022-03-28 21:31:35 +02:00
->orderBy('sequence_id', 'asc')
->where('sequence_id', '>', $this->task->sequence_id)
2020-10-05 20:46:41 -07:00
->first();
2026-10-05 18:10:18 -04:00
if (($nextTask) === null) {
2017-09-16 23:10:00 -05:00
$this->markScheduleComplete();
return;
}
2020-10-05 20:46:41 -07:00
$nextTask->update(['is_queued' => true]);
2026-10-05 18:10:18 -04:00
try {
$this->dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset));
} catch (Throwable $throwable) {
rescue(fn () => $this->failed($throwable));
if ($this->job !== null) {
$this->fail($throwable);
return;
}
throw $throwable;
}
2017-09-16 23:10:00 -05:00
}
/**
* Marks the parent schedule as being complete.
*/
2026-10-05 18:10:18 -04:00
private function markScheduleComplete(): void
2017-09-16 23:10:00 -05:00
{
2020-10-05 20:46:41 -07:00
$this->task->schedule()->update([
'is_processing' => false,
'last_run_at' => CarbonImmutable::now()->toDateTimeString(),
]);
2017-09-16 23:10:00 -05:00
}
/**
* Mark a specific task as no longer being queued.
*/
2026-10-05 18:10:18 -04:00
private function markTaskNotQueued(): void
2017-09-16 23:10:00 -05:00
{
2020-10-05 20:46:41 -07:00
$this->task->update(['is_queued' => false]);
2017-09-16 23:10:00 -05:00
}
}