2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Schedules;
|
|
|
|
|
2018-09-01 04:00:13 +00:00
|
|
|
use DateTimeInterface;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Cron\CronExpression;
|
2018-09-01 04:00:13 +00:00
|
|
|
use Cake\Chronos\Chronos;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Pterodactyl\Models\Schedule;
|
2018-09-01 04:12:10 +00:00
|
|
|
use Cake\Chronos\ChronosInterface;
|
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
|
|
|
|
2017-09-27 03:16:26 +00:00
|
|
|
/**
|
2018-09-03 21:04:25 +00:00
|
|
|
* @var \DateTimeInterface|null
|
2017-09-27 03:16:26 +00:00
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
private $runTimeOverride;
|
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
|
|
|
}
|
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
/**
|
|
|
|
* Set the time that this schedule should be run at. This will override the time
|
|
|
|
* defined on the schedule itself. Useful for triggering one-off task runs.
|
|
|
|
*
|
2018-09-01 04:00:13 +00:00
|
|
|
* @param \DateTimeInterface $time
|
2018-01-09 03:43:10 +00:00
|
|
|
* @return $this
|
|
|
|
*/
|
2018-09-01 04:00:13 +00:00
|
|
|
public function setRunTimeOverride(DateTimeInterface $time)
|
2018-01-09 03:43:10 +00:00
|
|
|
{
|
|
|
|
$this->runTimeOverride = $time;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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-01-09 03:43:10 +00:00
|
|
|
'next_run_at' => $this->getRunAtTime($formattedCron),
|
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
|
|
|
}
|
2018-01-09 03:43:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timestamp to store in the database as the next_run time for a schedule.
|
|
|
|
*
|
|
|
|
* @param string $formatted
|
2018-09-01 04:12:10 +00:00
|
|
|
* @return \Cake\Chronos\ChronosInterface
|
2018-01-09 03:43:10 +00:00
|
|
|
*/
|
2018-09-01 04:12:10 +00:00
|
|
|
private function getRunAtTime(string $formatted): ChronosInterface
|
2018-01-09 03:43:10 +00:00
|
|
|
{
|
2018-09-01 04:00:13 +00:00
|
|
|
if (! is_null($this->runTimeOverride)) {
|
2018-09-01 04:12:10 +00:00
|
|
|
return $this->runTimeOverride instanceof ChronosInterface ? $this->runTimeOverride : Chronos::instance($this->runTimeOverride);
|
2018-01-09 03:43:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 04:12:10 +00:00
|
|
|
return Chronos::instance(CronExpression::factory($formatted)->getNextRunDate());
|
2018-01-09 03:43:10 +00:00
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|