2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Schedules;
|
|
|
|
|
2020-10-27 03:54:15 +00:00
|
|
|
use Exception;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Pterodactyl\Models\Schedule;
|
2018-09-03 21:04:25 +00:00
|
|
|
use Illuminate\Contracts\Bus\Dispatcher;
|
|
|
|
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
2020-10-15 03:38:59 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-10-15 04:06:27 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2021-05-01 18:52:02 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
class ProcessScheduleService
|
|
|
|
{
|
2017-09-27 03:16:26 +00:00
|
|
|
/**
|
|
|
|
* ProcessScheduleService constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private ConnectionInterface $connection, private Dispatcher $dispatcher, private DaemonServerRepository $serverRepository)
|
2020-10-15 03:38:59 +00:00
|
|
|
{
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a schedule and push the first task onto the queue worker.
|
|
|
|
*
|
2020-10-15 03:38:59 +00:00
|
|
|
* @throws \Throwable
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(Schedule $schedule, bool $now = false): void
|
2017-09-17 04:10:00 +00:00
|
|
|
{
|
2018-09-03 21:04:25 +00:00
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
2021-05-01 18:52:02 +00:00
|
|
|
$task = $schedule->tasks()->orderBy('sequence_id')->first();
|
2020-10-15 04:06:27 +00:00
|
|
|
|
|
|
|
if (is_null($task)) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new DisplayException('Cannot process schedule for task execution: no tasks are registered.');
|
2020-10-15 04:06:27 +00:00
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2020-10-15 03:38:59 +00:00
|
|
|
$this->connection->transaction(function () use ($schedule, $task) {
|
|
|
|
$schedule->forceFill([
|
|
|
|
'is_processing' => true,
|
|
|
|
'next_run_at' => $schedule->getNextRunDate(),
|
|
|
|
])->saveOrFail();
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2020-10-15 03:38:59 +00:00
|
|
|
$task->update(['is_queued' => true]);
|
|
|
|
});
|
2018-09-03 21:04:25 +00:00
|
|
|
|
2021-04-24 22:06:21 +00:00
|
|
|
$job = new RunTaskJob($task, $now);
|
2021-05-01 18:52:02 +00:00
|
|
|
if ($schedule->only_when_online) {
|
|
|
|
// Check that the server is currently in a starting or running state before executing
|
|
|
|
// this schedule if this option has been set.
|
|
|
|
try {
|
|
|
|
$details = $this->serverRepository->setServer($schedule->server)->getDetails();
|
|
|
|
$state = $details['state'] ?? 'offline';
|
|
|
|
// If the server is stopping or offline just do nothing with this task.
|
|
|
|
if (in_array($state, ['offline', 'stopping'])) {
|
|
|
|
$job->failed();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-02-23 19:30:16 +00:00
|
|
|
} catch (\Exception $exception) {
|
2021-05-01 18:52:02 +00:00
|
|
|
if (!$exception instanceof DaemonConnectionException) {
|
|
|
|
// If we encountered some exception during this process that wasn't just an
|
|
|
|
// issue connecting to Wings run the failed sequence for a job. Otherwise we
|
|
|
|
// can just quietly mark the task as completed without actually running anything.
|
|
|
|
$job->failed($exception);
|
|
|
|
}
|
|
|
|
$job->failed();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 03:54:15 +00:00
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$now) {
|
2020-10-27 03:54:15 +00:00
|
|
|
$this->dispatcher->dispatch($job->delay($task->time_offset));
|
|
|
|
} else {
|
|
|
|
// When using dispatchNow the RunTaskJob::failed() function is not called automatically
|
|
|
|
// so we need to manually trigger it and then continue with the exception throw.
|
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2550
|
|
|
|
try {
|
|
|
|
$this->dispatcher->dispatchNow($job);
|
2023-02-23 19:30:16 +00:00
|
|
|
} catch (\Exception $exception) {
|
2020-10-27 03:54:15 +00:00
|
|
|
$job->failed($exception);
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
}
|