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

98 lines
2.8 KiB
PHP
Raw Normal View History

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;
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ProcessScheduleService
{
2017-09-27 03:16:26 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
private $repository;
2017-09-17 04:10:00 +00:00
2017-09-27 03:16:26 +00:00
/**
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
*/
private $runnerService;
/**
2018-09-01 04:00:13 +00:00
* @var \DateTimeInterface|null
*/
private $runTimeOverride;
2017-09-17 04:10:00 +00:00
2017-09-27 03:16:26 +00:00
/**
* ProcessScheduleService constructor.
*
* @param \Pterodactyl\Services\Schedules\Tasks\RunTaskService $runnerService
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
*/
public function __construct(RunTaskService $runnerService, ScheduleRepositoryInterface $repository)
{
2017-09-17 04:10:00 +00:00
$this->repository = $repository;
$this->runnerService = $runnerService;
}
/**
* 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
* @return $this
*/
2018-09-01 04:00:13 +00:00
public function setRunTimeOverride(DateTimeInterface $time)
{
$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.
*
* @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->repository->loadTasks($schedule);
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->repository->update($schedule->id, [
'is_processing' => true,
'next_run_at' => $this->getRunAtTime($formattedCron),
2017-09-17 04:10:00 +00:00
]);
$task = $schedule->getRelation('tasks')->where('sequence_id', 1)->first();
2017-09-17 04:10:00 +00:00
$this->runnerService->handle($task);
}
/**
* Get the timestamp to store in the database as the next_run time for a schedule.
*
* @param string $formatted
2018-09-01 04:00:13 +00:00
* @return string
*/
private function getRunAtTime(string $formatted)
{
2018-09-01 04:00:13 +00:00
if (! is_null($this->runTimeOverride)) {
return $this->runTimeOverride->format(Chronos::ATOM);
}
2018-09-01 04:00:13 +00:00
return CronExpression::factory($formatted)->getNextRunDate()->format(Chronos::ATOM);
}
2017-09-17 04:10:00 +00:00
}