Path: blob/1.0-develop/app/Jobs/Schedule/RunTaskJob.php
10279 views
<?php12namespace Pterodactyl\Jobs\Schedule;34use Exception;5use Pterodactyl\Jobs\Job;6use Carbon\CarbonImmutable;7use Pterodactyl\Models\Task;8use Illuminate\Queue\SerializesModels;9use Illuminate\Queue\InteractsWithQueue;10use Illuminate\Contracts\Queue\ShouldQueue;11use Illuminate\Foundation\Bus\DispatchesJobs;12use Pterodactyl\Services\Backups\InitiateBackupService;13use Pterodactyl\Repositories\Wings\DaemonPowerRepository;14use Pterodactyl\Repositories\Wings\DaemonCommandRepository;15use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;1617class RunTaskJob extends Job implements ShouldQueue18{19use DispatchesJobs;20use InteractsWithQueue;21use SerializesModels;2223/**24* RunTaskJob constructor.25*/26public function __construct(public Task $task, public bool $manualRun = false)27{28$this->queue = 'standard';29}3031/**32* Run the job and send actions to the daemon running the server.33*34* @throws \Throwable35*/36public function handle(37DaemonCommandRepository $commandRepository,38InitiateBackupService $backupService,39DaemonPowerRepository $powerRepository,40) {41// Do not process a task that is not set to active, unless it's been manually triggered.42if (!$this->task->schedule->is_active && !$this->manualRun) {43$this->markTaskNotQueued();44$this->markScheduleComplete();4546return;47}4849$server = $this->task->server;50// If we made it to this point and the server status is not null it means the51// server was likely suspended or marked as reinstalling after the schedule52// was queued up. Just end the task right now — this should be a very rare53// condition.54if (!is_null($server->status)) {55$this->failed();5657return;58}5960// Perform the provided task against the daemon.61try {62switch ($this->task->action) {63case Task::ACTION_POWER:64$powerRepository->setServer($server)->send($this->task->payload);65break;66case Task::ACTION_COMMAND:67$commandRepository->setServer($server)->send($this->task->payload);68break;69case Task::ACTION_BACKUP:70$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);71break;72default:73throw new \InvalidArgumentException('Invalid task action provided: ' . $this->task->action);74}75} catch (\Exception $exception) {76// If this isn't a DaemonConnectionException on a task that allows for failures77// throw the exception back up the chain so that the task is stopped.78if (!($this->task->continue_on_failure && $exception instanceof DaemonConnectionException)) {79throw $exception;80}81}8283$this->markTaskNotQueued();84$this->queueNextTask();85}8687/**88* Handle a failure while sending the action to the daemon or otherwise processing the job.89*/90public function failed(?\Exception $exception = null)91{92$this->markTaskNotQueued();93$this->markScheduleComplete();94}9596/**97* Get the next task in the schedule and queue it for running after the defined period of wait time.98*/99private function queueNextTask()100{101/** @var Task|null $nextTask */102$nextTask = Task::query()->where('schedule_id', $this->task->schedule_id)103->orderBy('sequence_id', 'asc')104->where('sequence_id', '>', $this->task->sequence_id)105->first();106107if (is_null($nextTask)) {108$this->markScheduleComplete();109110return;111}112113$nextTask->update(['is_queued' => true]);114115$this->dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset));116}117118/**119* Marks the parent schedule as being complete.120*/121private function markScheduleComplete()122{123$this->task->schedule()->update([124'is_processing' => false,125'last_run_at' => CarbonImmutable::now()->toDateTimeString(),126]);127}128129/**130* Mark a specific task as no longer being queued.131*/132private function markTaskNotQueued()133{134$this->task->update(['is_queued' => false]);135}136}137138139