2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Schedules;
|
|
|
|
|
|
|
|
use Cron\CronExpression;
|
|
|
|
use Pterodactyl\Models\Schedule;
|
2018-09-03 21:04:25 +00:00
|
|
|
use Illuminate\Contracts\Bus\Dispatcher;
|
|
|
|
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
|
|
|
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
2018-09-03 21:04:25 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
|
2018-01-09 03:43:10 +00:00
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
private $scheduleRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $taskRepository;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2017-09-27 03:16:26 +00:00
|
|
|
/**
|
|
|
|
* ProcessScheduleService constructor.
|
|
|
|
*
|
2018-09-03 21:04:25 +00:00
|
|
|
* @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $scheduleRepository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $taskRepository
|
2017-09-27 03:16:26 +00:00
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
public function __construct(
|
|
|
|
Dispatcher $dispatcher,
|
|
|
|
ScheduleRepositoryInterface $scheduleRepository,
|
|
|
|
TaskRepositoryInterface $taskRepository
|
|
|
|
) {
|
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
$this->scheduleRepository = $scheduleRepository;
|
|
|
|
$this->taskRepository = $taskRepository;
|
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
|
2017-09-17 04:10:00 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2018-01-09 03:43:10 +00:00
|
|
|
public function handle(Schedule $schedule)
|
2017-09-17 04:10:00 +00:00
|
|
|
{
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->scheduleRepository->loadTasks($schedule);
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
|
|
|
$task = $schedule->getRelation('tasks')->where('sequence_id', 1)->first();
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2018-05-13 04:41:56 +00:00
|
|
|
$formattedCron = sprintf('%s %s %s * %s',
|
2017-09-17 04:10:00 +00:00
|
|
|
$schedule->cron_minute,
|
|
|
|
$schedule->cron_hour,
|
|
|
|
$schedule->cron_day_of_month,
|
|
|
|
$schedule->cron_day_of_week
|
|
|
|
);
|
|
|
|
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->scheduleRepository->update($schedule->id, [
|
2017-09-17 04:10:00 +00:00
|
|
|
'is_processing' => true,
|
2018-09-03 21:32:33 +00:00
|
|
|
'next_run_at' => CronExpression::factory($formattedCron)->getNextRunDate(),
|
2017-09-17 04:10:00 +00:00
|
|
|
]);
|
|
|
|
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->taskRepository->update($task->id, ['is_queued' => true]);
|
|
|
|
|
|
|
|
$this->dispatcher->dispatch(
|
|
|
|
(new RunTaskJob($task->id, $schedule->id))->delay($task->time_offset)
|
|
|
|
);
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|
|
|
|
}
|