2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Jobs\Schedule;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Pterodactyl\Jobs\Job;
|
2020-10-06 03:46:41 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use Pterodactyl\Models\Task;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2020-04-20 02:43:41 +00:00
|
|
|
use Pterodactyl\Services\Backups\InitiateBackupService;
|
2020-04-11 22:35:32 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
|
2021-05-01 17:44:40 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
class RunTaskJob extends Job implements ShouldQueue
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
use DispatchesJobs;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use SerializesModels;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RunTaskJob constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(public Task $task, public bool $manualRun = false)
|
2017-09-17 04:10:00 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->queue = 'standard';
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the job and send actions to the daemon running the server.
|
|
|
|
*
|
2020-04-20 02:43:41 +00:00
|
|
|
* @throws \Throwable
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
|
|
|
public function handle(
|
2020-04-11 22:35:32 +00:00
|
|
|
DaemonCommandRepository $commandRepository,
|
2020-04-20 02:43:41 +00:00
|
|
|
InitiateBackupService $backupService,
|
2020-10-15 03:38:59 +00:00
|
|
|
DaemonPowerRepository $powerRepository
|
2017-09-17 04:10:00 +00:00
|
|
|
) {
|
2021-04-25 01:18:29 +00:00
|
|
|
// 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) {
|
2018-01-09 03:43:10 +00:00
|
|
|
$this->markTaskNotQueued();
|
|
|
|
$this->markScheduleComplete();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-06 03:46:41 +00:00
|
|
|
$server = $this->task->server;
|
2022-05-05 00:41:53 +00: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
|
|
|
|
// was queued up. Just end the task right now — this should be a very rare
|
|
|
|
// condition.
|
|
|
|
if (!is_null($server->status)) {
|
|
|
|
$this->failed();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-13 14:50:56 +00:00
|
|
|
// Perform the provided task against the daemon.
|
2021-05-01 17:44:40 +00:00
|
|
|
try {
|
|
|
|
switch ($this->task->action) {
|
|
|
|
case Task::ACTION_POWER:
|
|
|
|
$powerRepository->setServer($server)->send($this->task->payload);
|
|
|
|
break;
|
|
|
|
case Task::ACTION_COMMAND:
|
|
|
|
$commandRepository->setServer($server)->send($this->task->payload);
|
|
|
|
break;
|
|
|
|
case Task::ACTION_BACKUP:
|
|
|
|
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
|
|
|
|
break;
|
|
|
|
default:
|
2022-11-29 17:53:59 +00:00
|
|
|
throw new \InvalidArgumentException('Invalid task action provided: ' . $this->task->action);
|
2021-05-01 17:44:40 +00:00
|
|
|
}
|
2022-11-29 17:53:59 +00:00
|
|
|
} catch (\Exception $exception) {
|
2021-05-01 18:52:02 +00: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.
|
|
|
|
if (!($this->task->continue_on_failure && $exception instanceof DaemonConnectionException)) {
|
|
|
|
throw $exception;
|
2021-05-01 17:44:40 +00:00
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->markTaskNotQueued();
|
2020-10-06 03:46:41 +00:00
|
|
|
$this->queueNextTask();
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a failure while sending the action to the daemon or otherwise processing the job.
|
|
|
|
*/
|
2022-11-29 17:53:59 +00:00
|
|
|
public function failed(\Exception $exception = null)
|
2017-09-17 04:10:00 +00:00
|
|
|
{
|
|
|
|
$this->markTaskNotQueued();
|
|
|
|
$this->markScheduleComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next task in the schedule and queue it for running after the defined period of wait time.
|
|
|
|
*/
|
2020-10-06 03:46:41 +00:00
|
|
|
private function queueNextTask()
|
2017-09-17 04:10:00 +00:00
|
|
|
{
|
2020-10-06 03:46:41 +00:00
|
|
|
/** @var \Pterodactyl\Models\Task|null $nextTask */
|
|
|
|
$nextTask = Task::query()->where('schedule_id', $this->task->schedule_id)
|
2022-03-28 19:31:35 +00:00
|
|
|
->orderBy('sequence_id', 'asc')
|
|
|
|
->where('sequence_id', '>', $this->task->sequence_id)
|
2020-10-06 03:46:41 +00:00
|
|
|
->first();
|
|
|
|
|
2017-09-17 04:10:00 +00:00
|
|
|
if (is_null($nextTask)) {
|
|
|
|
$this->markScheduleComplete();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-06 03:46:41 +00:00
|
|
|
$nextTask->update(['is_queued' => true]);
|
|
|
|
|
2021-04-25 01:18:29 +00:00
|
|
|
$this->dispatch((new self($nextTask, $this->manualRun))->delay($nextTask->time_offset));
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the parent schedule as being complete.
|
|
|
|
*/
|
|
|
|
private function markScheduleComplete()
|
|
|
|
{
|
2020-10-06 03:46:41 +00:00
|
|
|
$this->task->schedule()->update([
|
|
|
|
'is_processing' => false,
|
|
|
|
'last_run_at' => CarbonImmutable::now()->toDateTimeString(),
|
|
|
|
]);
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a specific task as no longer being queued.
|
|
|
|
*/
|
|
|
|
private function markTaskNotQueued()
|
|
|
|
{
|
2020-10-06 03:46:41 +00:00
|
|
|
$this->task->update(['is_queued' => false]);
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
}
|