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 InvalidArgumentException;
|
2020-10-27 03:54:15 +00:00
|
|
|
use Pterodactyl\Models\Schedule;
|
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;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
class RunTaskJob extends Job implements ShouldQueue
|
|
|
|
{
|
|
|
|
use DispatchesJobs, InteractsWithQueue, SerializesModels;
|
|
|
|
|
|
|
|
/**
|
2020-10-06 03:46:41 +00:00
|
|
|
* @var \Pterodactyl\Models\Task
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
|
|
|
public $task;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RunTaskJob constructor.
|
|
|
|
*
|
2020-10-06 03:46:41 +00:00
|
|
|
* @param \Pterodactyl\Models\Task $task
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
2020-10-06 03:46:41 +00:00
|
|
|
public function __construct(Task $task)
|
2017-09-17 04:10:00 +00:00
|
|
|
{
|
2018-01-09 03:43:10 +00:00
|
|
|
$this->queue = config('pterodactyl.queues.standard');
|
2017-09-17 04:10:00 +00:00
|
|
|
$this->task = $task;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the job and send actions to the daemon running the server.
|
|
|
|
*
|
2020-04-11 22:35:32 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonCommandRepository $commandRepository
|
2020-04-20 02:43:41 +00:00
|
|
|
* @param \Pterodactyl\Services\Backups\InitiateBackupService $backupService
|
2020-04-11 22:35:32 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $powerRepository
|
2017-09-17 04:10:00 +00:00
|
|
|
*
|
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
|
|
|
) {
|
2018-01-09 03:43:10 +00:00
|
|
|
// Do not process a task that is not set to active.
|
2020-10-06 03:46:41 +00:00
|
|
|
if (! $this->task->schedule->is_active) {
|
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;
|
2018-05-13 14:50:56 +00:00
|
|
|
// Perform the provided task against the daemon.
|
2020-10-06 03:46:41 +00:00
|
|
|
switch ($this->task->action) {
|
2017-09-17 04:10:00 +00:00
|
|
|
case 'power':
|
2020-10-06 03:46:41 +00:00
|
|
|
$powerRepository->setServer($server)->send($this->task->payload);
|
2017-09-17 04:10:00 +00:00
|
|
|
break;
|
|
|
|
case 'command':
|
2020-10-06 03:46:41 +00:00
|
|
|
$commandRepository->setServer($server)->send($this->task->payload);
|
2017-09-17 04:10:00 +00:00
|
|
|
break;
|
2020-04-20 02:43:41 +00:00
|
|
|
case 'backup':
|
2020-11-09 23:35:57 +00:00
|
|
|
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
|
2020-04-20 02:43:41 +00:00
|
|
|
break;
|
2017-09-17 04:10:00 +00:00
|
|
|
default:
|
2018-01-09 03:43:10 +00:00
|
|
|
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');
|
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.
|
|
|
|
*
|
2020-04-11 22:35:32 +00:00
|
|
|
* @param \Exception|null $exception
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
|
|
|
public function failed(Exception $exception = null)
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
->where('sequence_id', $this->task->sequence_id + 1)
|
|
|
|
->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]);
|
|
|
|
|
|
|
|
$this->dispatch((new self($nextTask))->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
|
|
|
}
|
|
|
|
}
|