2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Schedules;
|
|
|
|
|
|
|
|
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;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
class ProcessScheduleService
|
|
|
|
{
|
2017-09-27 03:16:26 +00:00
|
|
|
/**
|
2018-09-03 21:04:25 +00:00
|
|
|
* @var \Illuminate\Contracts\Bus\Dispatcher
|
2017-09-27 03:16:26 +00:00
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
private $dispatcher;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
/**
|
2020-10-15 03:38:59 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2018-01-09 03:43:10 +00:00
|
|
|
*/
|
2020-10-15 03:38:59 +00:00
|
|
|
private $connection;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2017-09-27 03:16:26 +00:00
|
|
|
/**
|
|
|
|
* ProcessScheduleService constructor.
|
|
|
|
*
|
2020-10-15 03:38:59 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
|
2017-09-27 03:16:26 +00:00
|
|
|
*/
|
2020-10-15 03:38:59 +00:00
|
|
|
public function __construct(ConnectionInterface $connection, Dispatcher $dispatcher)
|
|
|
|
{
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->dispatcher = $dispatcher;
|
2020-10-15 03:38:59 +00:00
|
|
|
$this->connection = $connection;
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a schedule and push the first task onto the queue worker.
|
|
|
|
*
|
2018-01-09 03:43:10 +00:00
|
|
|
* @param \Pterodactyl\Models\Schedule $schedule
|
2020-10-15 03:38:59 +00:00
|
|
|
* @param bool $now
|
2017-09-17 04:10:00 +00:00
|
|
|
*
|
2020-10-15 03:38:59 +00:00
|
|
|
* @throws \Throwable
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
2020-10-15 03:38:59 +00:00
|
|
|
public function handle(Schedule $schedule, bool $now = false)
|
2017-09-17 04:10:00 +00:00
|
|
|
{
|
2018-09-03 21:04:25 +00:00
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
2020-10-15 04:06:27 +00:00
|
|
|
$task = $schedule->tasks()->where('sequence_id', 1)->first();
|
|
|
|
|
|
|
|
if (is_null($task)) {
|
|
|
|
throw new DisplayException(
|
|
|
|
'Cannot process schedule for task execution: no tasks are registered.'
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
|
2020-10-15 03:38:59 +00:00
|
|
|
$this->dispatcher->{$now ? 'dispatchNow' : 'dispatch'}(
|
2020-10-06 03:46:41 +00:00
|
|
|
(new RunTaskJob($task))->delay($task->time_offset)
|
2018-09-03 21:04:25 +00:00
|
|
|
);
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
}
|