mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-10-31 18:16:55 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			138 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			138 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Jobs\Schedule;
 | |
| 
 | |
| use App\Jobs\Job;
 | |
| use App\Models\Task;
 | |
| use App\Repositories\Daemon\DaemonServerRepository;
 | |
| use App\Services\Backups\InitiateBackupService;
 | |
| use App\Services\Files\DeleteFilesService;
 | |
| use Carbon\CarbonImmutable;
 | |
| use Exception;
 | |
| use Illuminate\Contracts\Queue\ShouldQueue;
 | |
| use Illuminate\Http\Client\ConnectionException;
 | |
| use Illuminate\Queue\InteractsWithQueue;
 | |
| use Illuminate\Queue\SerializesModels;
 | |
| use InvalidArgumentException;
 | |
| use Throwable;
 | |
| 
 | |
| class RunTaskJob extends Job implements ShouldQueue
 | |
| {
 | |
|     use InteractsWithQueue;
 | |
|     use SerializesModels;
 | |
| 
 | |
|     /**
 | |
|      * RunTaskJob constructor.
 | |
|      */
 | |
|     public function __construct(public Task $task, public bool $manualRun = false) {}
 | |
| 
 | |
|     /**
 | |
|      * Run the job and send actions to the daemon running the server.
 | |
|      *
 | |
|      * @throws Throwable
 | |
|      */
 | |
|     public function handle(
 | |
|         InitiateBackupService $backupService,
 | |
|         DaemonServerRepository $serverRepository,
 | |
|         DeleteFilesService $deleteFilesService
 | |
|     ): void {
 | |
|         // Do not process a task that is not set to active, unless it's been manually triggered.
 | |
|         if (!$this->task->schedule->is_active && !$this->manualRun) {
 | |
|             $this->markTaskNotQueued();
 | |
|             $this->markScheduleComplete();
 | |
| 
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         $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
 | |
|         // was queued up. Just end the task right now — this should be a very rare
 | |
|         // condition.
 | |
|         if (!is_null($server->status)) {
 | |
|             $this->failed();
 | |
| 
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         // Perform the provided task against the daemon.
 | |
|         try {
 | |
|             switch ($this->task->action) {
 | |
|                 case Task::ACTION_POWER:
 | |
|                     $serverRepository->setServer($server)->power($this->task->payload);
 | |
|                     break;
 | |
|                 case Task::ACTION_COMMAND:
 | |
|                     $server->send($this->task->payload);
 | |
|                     break;
 | |
|                 case Task::ACTION_BACKUP:
 | |
|                     $backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
 | |
|                     break;
 | |
|                 case Task::ACTION_DELETE_FILES:
 | |
|                     $deleteFilesService->handle($server, explode(PHP_EOL, $this->task->payload));
 | |
|                     break;
 | |
|                 default:
 | |
|                     throw new InvalidArgumentException('Invalid task action provided: ' . $this->task->action);
 | |
|             }
 | |
|         } catch (Exception $exception) {
 | |
|             // If this isn't a ConnectionException on a task that allows for failures
 | |
|             // throw the exception back up the chain so that the task is stopped.
 | |
|             if (!($this->task->continue_on_failure && $exception instanceof ConnectionException)) {
 | |
|                 throw $exception;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         $this->markTaskNotQueued();
 | |
|         $this->queueNextTask();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Handle a failure while sending the action to the daemon or otherwise processing the job.
 | |
|      */
 | |
|     public function failed(): void
 | |
|     {
 | |
|         $this->markTaskNotQueued();
 | |
|         $this->markScheduleComplete();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Get the next task in the schedule and queue it for running after the defined period of wait time.
 | |
|      */
 | |
|     private function queueNextTask(): void
 | |
|     {
 | |
|         /** @var Task|null $nextTask */
 | |
|         $nextTask = Task::query()->where('schedule_id', $this->task->schedule_id)
 | |
|             ->orderBy('sequence_id', 'asc')
 | |
|             ->where('sequence_id', '>', $this->task->sequence_id)
 | |
|             ->first();
 | |
| 
 | |
|         if (is_null($nextTask)) {
 | |
|             $this->markScheduleComplete();
 | |
| 
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         $nextTask->update(['is_queued' => true]);
 | |
| 
 | |
|         dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset));
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Marks the parent schedule as being complete.
 | |
|      */
 | |
|     private function markScheduleComplete(): void
 | |
|     {
 | |
|         $this->task->schedule()->update([
 | |
|             'is_processing' => false,
 | |
|             'last_run_at' => CarbonImmutable::now()->toDateTimeString(),
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Mark a specific task as no longer being queued.
 | |
|      */
 | |
|     private function markTaskNotQueued(): void
 | |
|     {
 | |
|         $this->task->update(['is_queued' => false]);
 | |
|     }
 | |
| }
 | 
