2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Schedules;
|
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
use Carbon\Carbon;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Cron\CronExpression;
|
|
|
|
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
|
|
|
|
*/
|
2018-01-09 03:43:10 +00:00
|
|
|
private $repository;
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2017-09-27 03:16:26 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
|
|
|
|
*/
|
2018-01-09 03:43:10 +00:00
|
|
|
private $runnerService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Carbon\Carbon|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;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param \Carbon\Carbon $time
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setRunTimeOverride(Carbon $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.
|
|
|
|
*
|
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-01-09 03:43:10 +00:00
|
|
|
$this->repository->loadTasks($schedule);
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
$formattedCron = sprintf('%s %s %s * %s *',
|
|
|
|
$schedule->cron_minute,
|
|
|
|
$schedule->cron_hour,
|
|
|
|
$schedule->cron_day_of_month,
|
|
|
|
$schedule->cron_day_of_week
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->repository->update($schedule->id, [
|
|
|
|
'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-01-09 03:43:10 +00:00
|
|
|
$task = $schedule->getRelation('tasks')->where('sequence_id', 1)->first();
|
2017-09-17 04:10:00 +00:00
|
|
|
$this->runnerService->handle($task);
|
|
|
|
}
|
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
|
|
|
|
* @return \DateTime|string
|
|
|
|
*/
|
|
|
|
private function getRunAtTime(string $formatted)
|
|
|
|
{
|
|
|
|
if ($this->runTimeOverride instanceof Carbon) {
|
|
|
|
return $this->runTimeOverride->toDateTimeString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return CronExpression::factory($formatted)->getNextRunDate();
|
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|