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;
|
2026-03-27 07:21:35 +08:00
|
|
|
use Illuminate\Bus\Queueable;
|
2017-09-16 23:10:00 -05:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2026-03-27 07:21:35 +08:00
|
|
|
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;
|
2021-05-01 10:44:40 -07:00
|
|
|
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
|
|
|
|
2026-02-14 10:51:26 -08:00
|
|
|
class RunTaskJob implements ShouldQueue
|
2017-09-16 23:10:00 -05:00
|
|
|
{
|
2026-03-27 07:21:35 +08:00
|
|
|
use DispatchesJobs;
|
2026-10-05 18:10:18 -04:00
|
|
|
use InteractsWithQueue;
|
|
|
|
|
use Queueable;
|
2026-03-27 07:21:35 +08:00
|
|
|
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) {
|
2018-01-08 21:43:10 -06:00
|
|
|
$this->markTaskNotQueued();
|
|
|
|
|
$this->markScheduleComplete();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 20:46:41 -07:00
|
|
|
$server = $this->task->server;
|
2022-05-04 20:41:53 -04:00
|
|
|
// 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
|
2022-05-04 20:41:53 -04:00
|
|
|
// condition.
|
2026-10-05 18:10:18 -04:00
|
|
|
if (($server->status) !== null) {
|
2022-05-04 20:41:53 -04:00
|
|
|
$this->failed();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 10:50:56 -04:00
|
|
|
// Perform the provided task against the daemon.
|
2021-05-01 10:44:40 -07:00
|
|
|
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) {
|
2021-05-01 11:52:02 -07:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
}
|