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

76 lines
2.4 KiB
PHP
Raw Normal View History

2017-09-17 04:10:00 +00:00
<?php
2017-09-26 02:43:01 +00:00
/**
2017-09-17 04:10:00 +00:00
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-09-17 04:10:00 +00:00
*/
namespace Pterodactyl\Services\Schedules;
use Cron\CronExpression;
use Webmozart\Assert\Assert;
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
*/
2017-09-17 04:10:00 +00:00
protected $repository;
2017-09-27 03:16:26 +00:00
/**
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
*/
2017-09-17 04:10:00 +00:00
protected $runnerService;
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;
}
/**
* Process a schedule and push the first task onto the queue worker.
*
* @param int|\Pterodactyl\Models\Schedule $schedule
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($schedule)
{
Assert::true(($schedule instanceof Schedule || is_digit($schedule)),
'First argument passed to handle must be instance of \Pterodactyl\Models\Schedule or an integer, received %s.'
);
if (($schedule instanceof Schedule && ! $schedule->relationLoaded('tasks')) || ! $schedule instanceof Schedule) {
$schedule = $this->repository->getScheduleWithTasks(is_digit($schedule) ? $schedule : $schedule->id);
}
$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,
'next_run_at' => CronExpression::factory($formattedCron)->getNextRunDate(),
]);
$task = $schedule->tasks->where('sequence_id', 1)->first();
$this->runnerService->handle($task);
}
}