Update schedule process to allow toggling/triggering via UI
This commit is contained in:
parent
02fe49892d
commit
036bea2b94
18 changed files with 280 additions and 221 deletions
|
@ -15,6 +15,16 @@ interface ScheduleRepositoryInterface extends RepositoryInterface
|
|||
*/
|
||||
public function findServerSchedules(int $server): Collection;
|
||||
|
||||
/**
|
||||
* Load the tasks relationship onto the Schedule module if they are not
|
||||
* already present.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Schedule $schedule
|
||||
* @param bool $refresh
|
||||
* @return \Pterodactyl\Models\Schedule
|
||||
*/
|
||||
public function loadTasks(Schedule $schedule, bool $refresh = false): Schedule;
|
||||
|
||||
/**
|
||||
* Return a schedule model with all of the associated tasks as a relationship.
|
||||
*
|
||||
|
|
|
@ -14,7 +14,7 @@ interface TaskRepositoryInterface extends RepositoryInterface
|
|||
*
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function getTaskWithServer(int $id): Task;
|
||||
public function getTaskForJobProcess(int $id): Task;
|
||||
|
||||
/**
|
||||
* Returns the next task in a schedule.
|
||||
|
|
70
app/Http/Controllers/Server/Tasks/ActionController.php
Normal file
70
app/Http/Controllers/Server/Tasks/ActionController.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Server\Tasks;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Services\Schedules\ProcessScheduleService;
|
||||
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
||||
|
||||
class ActionController extends Controller
|
||||
{
|
||||
private $processScheduleService;
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
public function __construct(ProcessScheduleService $processScheduleService, ScheduleRepositoryInterface $repository)
|
||||
{
|
||||
$this->processScheduleService = $processScheduleService;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle a task to be active or inactive for a given server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function toggle(Request $request): Response
|
||||
{
|
||||
$server = $request->attributes->get('server');
|
||||
$schedule = $request->attributes->get('schedule');
|
||||
$this->authorize('toggle-schedule', $server);
|
||||
|
||||
$this->repository->update($schedule->id, [
|
||||
'is_active' => ! $schedule->is_active,
|
||||
]);
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger a schedule to run now.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function trigger(Request $request): Response
|
||||
{
|
||||
$server = $request->attributes->get('server');
|
||||
$this->authorize('toggle-schedule', $server);
|
||||
|
||||
$this->processScheduleService->setRunTimeOverride(Carbon::now())->handle(
|
||||
$request->attributes->get('schedule')
|
||||
);
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
}
|
|
@ -112,7 +112,7 @@ class TaskManagementController extends Controller
|
|||
$server = $request->attributes->get('server');
|
||||
|
||||
$schedule = $this->creationService->handle($server, $request->normalize(), $request->getTasks());
|
||||
$this->alert->success(trans('server.schedules.task_created'))->flash();
|
||||
$this->alert->success(trans('server.schedule.task_created'))->flash();
|
||||
|
||||
return redirect()->route('server.schedules.view', [
|
||||
'server' => $server->uuidShort,
|
||||
|
|
|
@ -1,18 +1,10 @@
|
|||
<?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\Jobs\Schedule;
|
||||
|
||||
use Exception;
|
||||
use Carbon\Carbon;
|
||||
use Pterodactyl\Jobs\Job;
|
||||
use Webmozart\Assert\Assert;
|
||||
use InvalidArgumentException;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
|
@ -59,12 +51,9 @@ class RunTaskJob extends Job implements ShouldQueue
|
|||
* @param int $task
|
||||
* @param int $schedule
|
||||
*/
|
||||
public function __construct($task, $schedule)
|
||||
public function __construct(int $task, int $schedule)
|
||||
{
|
||||
Assert::integerish($task, 'First argument passed to constructor must be integer, received %s.');
|
||||
Assert::integerish($schedule, 'Second argument passed to constructor must be integer, received %s.');
|
||||
|
||||
$this->queue = app()->make('config')->get('pterodactyl.queues.standard');
|
||||
$this->queue = config('pterodactyl.queues.standard');
|
||||
$this->task = $task;
|
||||
$this->schedule = $schedule;
|
||||
}
|
||||
|
@ -91,10 +80,18 @@ class RunTaskJob extends Job implements ShouldQueue
|
|||
$this->powerRepository = $powerRepository;
|
||||
$this->taskRepository = $taskRepository;
|
||||
|
||||
$task = $this->taskRepository->getTaskWithServer($this->task);
|
||||
$task = $this->taskRepository->getTaskForJobProcess($this->task);
|
||||
$server = $task->getRelation('server');
|
||||
$user = $server->getRelation('user');
|
||||
|
||||
// Do not process a task that is not set to active.
|
||||
if (! $task->getRelation('schedule')->is_active) {
|
||||
$this->markTaskNotQueued();
|
||||
$this->markScheduleComplete();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Perform the provided task aganist the daemon.
|
||||
switch ($task->action) {
|
||||
case 'power':
|
||||
|
@ -108,7 +105,7 @@ class RunTaskJob extends Job implements ShouldQueue
|
|||
->send($task->payload);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException('Cannot run a task that points to a non-existant action.');
|
||||
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');
|
||||
}
|
||||
|
||||
$this->markTaskNotQueued();
|
||||
|
|
|
@ -31,6 +31,23 @@ class ScheduleRepository extends EloquentRepository implements ScheduleRepositor
|
|||
return $this->getBuilder()->withCount('tasks')->where('server_id', '=', $server)->get($this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the tasks relationship onto the Schedule module if they are not
|
||||
* already present.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Schedule $schedule
|
||||
* @param bool $refresh
|
||||
* @return \Pterodactyl\Models\Schedule
|
||||
*/
|
||||
public function loadTasks(Schedule $schedule, bool $refresh = false): Schedule
|
||||
{
|
||||
if (! $schedule->relationLoaded('tasks') || $refresh) {
|
||||
$schedule->load('tasks');
|
||||
}
|
||||
|
||||
return $schedule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a schedule model with all of the associated tasks as a relationship.
|
||||
*
|
||||
|
|
|
@ -27,10 +27,10 @@ class TaskRepository extends EloquentRepository implements TaskRepositoryInterfa
|
|||
*
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function getTaskWithServer(int $id): Task
|
||||
public function getTaskForJobProcess(int $id): Task
|
||||
{
|
||||
try {
|
||||
return $this->getBuilder()->with('server.user')->findOrFail($id, $this->getColumns());
|
||||
return $this->getBuilder()->with('server.user', 'schedule')->findOrFail($id, $this->getColumns());
|
||||
} catch (ModelNotFoundException $exception) {
|
||||
throw new RecordNotFoundException;
|
||||
}
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
<?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;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Cron\CronExpression;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Models\Schedule;
|
||||
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
|
||||
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
||||
|
@ -20,12 +13,17 @@ class ProcessScheduleService
|
|||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
|
||||
*/
|
||||
protected $runnerService;
|
||||
private $runnerService;
|
||||
|
||||
/**
|
||||
* @var \Carbon\Carbon|null
|
||||
*/
|
||||
private $runTimeOverride;
|
||||
|
||||
/**
|
||||
* ProcessScheduleService constructor.
|
||||
|
@ -39,23 +37,31 @@ class ProcessScheduleService
|
|||
$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.
|
||||
*
|
||||
* @param \Carbon\Carbon $time
|
||||
* @return $this
|
||||
*/
|
||||
public function setRunTimeOverride(Carbon $time)
|
||||
{
|
||||
$this->runTimeOverride = $time;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a schedule and push the first task onto the queue worker.
|
||||
*
|
||||
* @param int|\Pterodactyl\Models\Schedule $schedule
|
||||
* @param \Pterodactyl\Models\Schedule $schedule
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function handle($schedule)
|
||||
public function handle(Schedule $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);
|
||||
}
|
||||
$this->repository->loadTasks($schedule);
|
||||
|
||||
$formattedCron = sprintf('%s %s %s * %s *',
|
||||
$schedule->cron_minute,
|
||||
|
@ -66,10 +72,25 @@ class ProcessScheduleService
|
|||
|
||||
$this->repository->update($schedule->id, [
|
||||
'is_processing' => true,
|
||||
'next_run_at' => CronExpression::factory($formattedCron)->getNextRunDate(),
|
||||
'next_run_at' => $this->getRunAtTime($formattedCron),
|
||||
]);
|
||||
|
||||
$task = $schedule->tasks->where('sequence_id', 1)->first();
|
||||
$task = $schedule->getRelation('tasks')->where('sequence_id', 1)->first();
|
||||
$this->runnerService->handle($task);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,8 @@
|
|||
<?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;
|
||||
|
||||
use Cron\CronExpression;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
|
||||
|
@ -53,37 +45,32 @@ class ScheduleCreationService
|
|||
/**
|
||||
* Create a new schedule for a specific server.
|
||||
*
|
||||
* @param int|\Pterodactyl\Models\Server $server
|
||||
* @param array $data
|
||||
* @param array $tasks
|
||||
* @param \Pterodactyl\Models\Server $server
|
||||
* @param array $data
|
||||
* @param array $tasks
|
||||
* @return \Pterodactyl\Models\Schedule
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
|
||||
*/
|
||||
public function handle($server, array $data, array $tasks = [])
|
||||
public function handle(Server $server, array $data, array $tasks = [])
|
||||
{
|
||||
Assert::true(($server instanceof Server || is_digit($server)),
|
||||
'First argument passed to handle must be numeric or instance of \Pterodactyl\Models\Server, received %s.'
|
||||
);
|
||||
|
||||
$server = ($server instanceof Server) ? $server->id : $server;
|
||||
$data['server_id'] = $server;
|
||||
$data['next_run_at'] = $this->getCronTimestamp($data);
|
||||
$data = array_merge($data, [
|
||||
'server_id' => $server->id,
|
||||
'next_run_at' => $this->getCronTimestamp($data),
|
||||
]);
|
||||
|
||||
$this->connection->beginTransaction();
|
||||
$schedule = $this->repository->create($data);
|
||||
|
||||
if (! empty($tasks)) {
|
||||
foreach ($tasks as $index => $task) {
|
||||
$this->taskCreationService->handle($schedule, [
|
||||
'time_interval' => array_get($task, 'time_interval'),
|
||||
'time_value' => array_get($task, 'time_value'),
|
||||
'sequence_id' => $index + 1,
|
||||
'action' => array_get($task, 'action'),
|
||||
'payload' => array_get($task, 'payload'),
|
||||
], false);
|
||||
}
|
||||
foreach ($tasks as $index => $task) {
|
||||
$this->taskCreationService->handle($schedule, [
|
||||
'time_interval' => array_get($task, 'time_interval'),
|
||||
'time_value' => array_get($task, 'time_value'),
|
||||
'sequence_id' => $index + 1,
|
||||
'action' => array_get($task, 'action'),
|
||||
'payload' => array_get($task, 'payload'),
|
||||
], false);
|
||||
}
|
||||
|
||||
$this->connection->commit();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue