misc_pterodactyl-panel/app/Services/Schedules/ProcessScheduleService.php

75 lines
2.1 KiB
PHP
Raw Normal View History

2017-09-17 04:10:00 +00:00
<?php
namespace Pterodactyl\Services\Schedules;
use Exception;
2017-09-17 04:10:00 +00:00
use Pterodactyl\Models\Schedule;
use Illuminate\Contracts\Bus\Dispatcher;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
2017-09-17 04:10:00 +00:00
class ProcessScheduleService
{
2017-09-27 03:16:26 +00:00
/**
* @var \Illuminate\Contracts\Bus\Dispatcher
2017-09-27 03:16:26 +00:00
*/
private $dispatcher;
2017-09-17 04:10:00 +00:00
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
2017-09-17 04:10:00 +00:00
2017-09-27 03:16:26 +00:00
/**
* ProcessScheduleService constructor.
*/
public function __construct(ConnectionInterface $connection, Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
$this->connection = $connection;
2017-09-17 04:10:00 +00:00
}
/**
* Process a schedule and push the first task onto the queue worker.
*
* @throws \Throwable
2017-09-17 04:10:00 +00:00
*/
public function handle(Schedule $schedule, bool $now = false)
2017-09-17 04:10:00 +00:00
{
/** @var \Pterodactyl\Models\Task $task */
$task = $schedule->tasks()->orderBy('sequence_id', 'asc')->first();
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.');
}
2017-09-17 04:10:00 +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
$task->update(['is_queued' => true]);
});
2021-04-24 22:06:21 +00:00
$job = new RunTaskJob($task, $now);
2021-01-23 20:33:34 +00:00
if (!$now) {
$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);
} catch (Exception $exception) {
$job->failed($exception);
throw $exception;
}
}
2017-09-17 04:10:00 +00:00
}
}