2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Jobs\Schedule;
|
|
|
|
|
|
|
|
use Exception;
|
2020-04-11 22:35:32 +00:00
|
|
|
use Carbon\Carbon;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Pterodactyl\Jobs\Job;
|
|
|
|
use InvalidArgumentException;
|
2020-04-11 22:35:32 +00:00
|
|
|
use Illuminate\Container\Container;
|
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-11 22:35:32 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\TaskRepository;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
|
2017-09-30 16:45:24 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
|
|
|
|
|
|
|
class RunTaskJob extends Job implements ShouldQueue
|
|
|
|
{
|
|
|
|
use DispatchesJobs, InteractsWithQueue, SerializesModels;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2017-09-27 03:16:26 +00:00
|
|
|
public $schedule;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $task;
|
|
|
|
|
|
|
|
/**
|
2020-04-11 22:35:32 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\TaskRepository
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
|
|
|
protected $taskRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RunTaskJob constructor.
|
|
|
|
*
|
|
|
|
* @param int $task
|
|
|
|
* @param int $schedule
|
|
|
|
*/
|
2018-01-09 03:43:10 +00:00
|
|
|
public function __construct(int $task, int $schedule)
|
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;
|
|
|
|
$this->schedule = $schedule;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
|
2020-04-11 22:35:32 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonPowerRepository $powerRepository
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\TaskRepository $taskRepository
|
2017-09-17 04:10:00 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function handle(
|
2020-04-11 22:35:32 +00:00
|
|
|
DaemonCommandRepository $commandRepository,
|
2017-09-30 16:45:24 +00:00
|
|
|
DaemonKeyProviderService $keyProviderService,
|
2020-04-11 22:35:32 +00:00
|
|
|
DaemonPowerRepository $powerRepository,
|
|
|
|
TaskRepository $taskRepository
|
2017-09-17 04:10:00 +00:00
|
|
|
) {
|
|
|
|
$this->taskRepository = $taskRepository;
|
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
$task = $this->taskRepository->getTaskForJobProcess($this->task);
|
2017-11-05 18:38:39 +00:00
|
|
|
$server = $task->getRelation('server');
|
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.
|
|
|
|
if (! $task->getRelation('schedule')->is_active) {
|
|
|
|
$this->markTaskNotQueued();
|
|
|
|
$this->markScheduleComplete();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-13 14:50:56 +00:00
|
|
|
// Perform the provided task against the daemon.
|
2017-09-17 04:10:00 +00:00
|
|
|
switch ($task->action) {
|
|
|
|
case 'power':
|
2020-04-11 22:35:32 +00:00
|
|
|
$powerRepository->setServer($server)->send($task->payload);
|
2017-09-17 04:10:00 +00:00
|
|
|
break;
|
|
|
|
case 'command':
|
2020-04-11 22:35:32 +00:00
|
|
|
$commandRepository->setServer($server)->send($task->payload);
|
2017-09-17 04:10:00 +00:00
|
|
|
break;
|
|
|
|
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();
|
|
|
|
$this->queueNextTask($task->sequence_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param int $sequence
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
private function queueNextTask($sequence)
|
|
|
|
{
|
|
|
|
$nextTask = $this->taskRepository->getNextTask($this->schedule, $sequence);
|
|
|
|
if (is_null($nextTask)) {
|
|
|
|
$this->markScheduleComplete();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->taskRepository->update($nextTask->id, ['is_queued' => true]);
|
|
|
|
$this->dispatch((new self($nextTask->id, $this->schedule))->delay($nextTask->time_offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the parent schedule as being complete.
|
|
|
|
*/
|
|
|
|
private function markScheduleComplete()
|
|
|
|
{
|
2020-04-11 22:35:32 +00:00
|
|
|
Container::getInstance()
|
|
|
|
->make(ScheduleRepositoryInterface::class)
|
|
|
|
->withoutFreshModel()
|
|
|
|
->update($this->schedule, [
|
|
|
|
'is_processing' => false,
|
|
|
|
'last_run_at' => Carbon::now()->toDateTimeString(),
|
|
|
|
]);
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a specific task as no longer being queued.
|
|
|
|
*/
|
|
|
|
private function markTaskNotQueued()
|
|
|
|
{
|
2020-04-11 22:35:32 +00:00
|
|
|
Container::getInstance()
|
|
|
|
->make(TaskRepositoryInterface::class)
|
|
|
|
->update($this->task, ['is_queued' => false]);
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
}
|