Path: blob/1.0-develop/app/Jobs/Schedule/RunTaskJob.php
14044 views
<?php12namespace Pterodactyl\Jobs\Schedule;34use Carbon\CarbonImmutable;5use Pterodactyl\Models\Task;6use Illuminate\Bus\Queueable;7use Illuminate\Queue\SerializesModels;8use Illuminate\Contracts\Queue\ShouldQueue;9use Illuminate\Foundation\Bus\DispatchesJobs;10use Pterodactyl\Services\Backups\InitiateBackupService;11use Pterodactyl\Repositories\Wings\DaemonPowerRepository;12use Pterodactyl\Repositories\Wings\DaemonCommandRepository;13use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;1415class RunTaskJob implements ShouldQueue16{17use Queueable;18use DispatchesJobs;19use SerializesModels;2021/**22* RunTaskJob constructor.23*/24public function __construct(public Task $task, public bool $manualRun = false)25{26$this->queue = 'standard';27}2829/**30* Run the job and send actions to the daemon running the server.31*32* @throws \Throwable33*/34public function handle(35DaemonCommandRepository $commandRepository,36InitiateBackupService $backupService,37DaemonPowerRepository $powerRepository,38) {39// Do not process a task that is not set to active, unless it's been manually triggered.40if (!$this->task->schedule->is_active && !$this->manualRun) {41$this->markTaskNotQueued();42$this->markScheduleComplete();4344return;45}4647$server = $this->task->server;48// If we made it to this point and the server status is not null it means the49// server was likely suspended or marked as reinstalling after the schedule50// was queued up. Just end the task right now — this should be a very rare51// condition.52if (!is_null($server->status)) {53$this->failed();5455return;56}5758// Perform the provided task against the daemon.59try {60switch ($this->task->action) {61case Task::ACTION_POWER:62$powerRepository->setServer($server)->send($this->task->payload);63break;64case Task::ACTION_COMMAND:65$commandRepository->setServer($server)->send($this->task->payload);66break;67case Task::ACTION_BACKUP:68$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);69break;70default:71throw new \InvalidArgumentException('Invalid task action provided: ' . $this->task->action);72}73} catch (\Exception $exception) {74// If this isn't a DaemonConnectionException on a task that allows for failures75// throw the exception back up the chain so that the task is stopped.76if (!($this->task->continue_on_failure && $exception instanceof DaemonConnectionException)) {77throw $exception;78}79}8081$this->markTaskNotQueued();82$this->queueNextTask();83}8485/**86* Handle a failure while sending the action to the daemon or otherwise processing the job.87*/88public function failed(?\Exception $exception = null)89{90$this->markTaskNotQueued();91$this->markScheduleComplete();92}9394/**95* Get the next task in the schedule and queue it for running after the defined period of wait time.96*/97private function queueNextTask()98{99/** @var Task|null $nextTask */100$nextTask = Task::query()->where('schedule_id', $this->task->schedule_id)101->orderBy('sequence_id', 'asc')102->where('sequence_id', '>', $this->task->sequence_id)103->first();104105if (is_null($nextTask)) {106$this->markScheduleComplete();107108return;109}110111$nextTask->update(['is_queued' => true]);112113$this->dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset));114}115116/**117* Marks the parent schedule as being complete.118*/119private function markScheduleComplete()120{121$this->task->schedule()->update([122'is_processing' => false,123'last_run_at' => CarbonImmutable::now()->toDateTimeString(),124]);125}126127/**128* Mark a specific task as no longer being queued.129*/130private function markTaskNotQueued()131{132$this->task->update(['is_queued' => false]);133}134}135136137