Changes to job running to clean up code

This commit is contained in:
Dane Everitt 2018-09-03 14:04:25 -07:00
parent a7943553d3
commit 413a22a3d5
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 85 additions and 192 deletions

View file

@ -7,36 +7,48 @@ use Cron\CronExpression;
use Cake\Chronos\Chronos;
use Pterodactyl\Models\Schedule;
use Cake\Chronos\ChronosInterface;
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
use Illuminate\Contracts\Bus\Dispatcher;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ProcessScheduleService
{
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
* @var \Illuminate\Contracts\Bus\Dispatcher
*/
private $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
*/
private $runnerService;
private $dispatcher;
/**
* @var \DateTimeInterface|null
*/
private $runTimeOverride;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
private $scheduleRepository;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
*/
private $taskRepository;
/**
* ProcessScheduleService constructor.
*
* @param \Pterodactyl\Services\Schedules\Tasks\RunTaskService $runnerService
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
* @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $scheduleRepository
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $taskRepository
*/
public function __construct(RunTaskService $runnerService, ScheduleRepositoryInterface $repository)
{
$this->repository = $repository;
$this->runnerService = $runnerService;
public function __construct(
Dispatcher $dispatcher,
ScheduleRepositoryInterface $scheduleRepository,
TaskRepositoryInterface $taskRepository
) {
$this->dispatcher = $dispatcher;
$this->scheduleRepository = $scheduleRepository;
$this->taskRepository = $taskRepository;
}
/**
@ -63,7 +75,10 @@ class ProcessScheduleService
*/
public function handle(Schedule $schedule)
{
$this->repository->loadTasks($schedule);
$this->scheduleRepository->loadTasks($schedule);
/** @var \Pterodactyl\Models\Task $task */
$task = $schedule->getRelation('tasks')->where('sequence_id', 1)->first();
$formattedCron = sprintf('%s %s %s * %s',
$schedule->cron_minute,
@ -72,13 +87,16 @@ class ProcessScheduleService
$schedule->cron_day_of_week
);
$this->repository->update($schedule->id, [
$this->scheduleRepository->update($schedule->id, [
'is_processing' => true,
'next_run_at' => $this->getRunAtTime($formattedCron),
]);
$task = $schedule->getRelation('tasks')->where('sequence_id', 1)->first();
$this->runnerService->handle($task);
$this->taskRepository->update($task->id, ['is_queued' => true]);
$this->dispatcher->dispatch(
(new RunTaskJob($task->id, $schedule->id))->delay($task->time_offset)
);
}
/**

View file

@ -1,53 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Schedules\Tasks;
use Pterodactyl\Models\Task;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
class RunTaskService
{
use DispatchesJobs;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
*/
protected $repository;
/**
* RunTaskService constructor.
*
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository
*/
public function __construct(TaskRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* Push a single task onto the queue.
*
* @param int|\Pterodactyl\Models\Task $task
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($task)
{
if (! $task instanceof Task) {
$task = $this->repository->find($task);
}
$this->repository->update($task->id, ['is_queued' => true]);
$this->dispatch((new RunTaskJob($task->id, $task->schedule_id))->delay($task->time_offset));
}
}