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

80 lines
2.4 KiB
PHP
Raw Normal View History

2017-09-17 04:10:00 +00:00
<?php
namespace Pterodactyl\Services\Schedules;
use Cron\CronExpression;
use Pterodactyl\Models\Schedule;
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
/**
* @var \Illuminate\Contracts\Bus\Dispatcher
2017-09-27 03:16:26 +00:00
*/
private $dispatcher;
2017-09-17 04:10:00 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
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.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $scheduleRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $taskRepository
2017-09-27 03:16:26 +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.
*
* @param \Pterodactyl\Models\Schedule $schedule
2017-09-17 04:10:00 +00:00
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle(Schedule $schedule)
2017-09-17 04:10:00 +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
$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
);
$this->scheduleRepository->update($schedule->id, [
2017-09-17 04:10:00 +00:00
'is_processing' => true,
'next_run_at' => CronExpression::factory($formattedCron)->getNextRunDate(),
2017-09-17 04:10:00 +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
}
}