Update schedule process to allow toggling/triggering via UI

This commit is contained in:
Dane Everitt 2018-01-08 21:43:10 -06:00
parent 02fe49892d
commit 036bea2b94
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
18 changed files with 280 additions and 221 deletions

View file

@ -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.
*

View file

@ -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.

View 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);
}
}

View file

@ -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,

View file

@ -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();

View file

@ -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.
*

View file

@ -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;
}

View file

@ -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();
}
}

View file

@ -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();

File diff suppressed because one or more lines are too long

View file

@ -19,6 +19,7 @@
// SOFTWARE.
$(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip();
$('[data-action="delete-schedule"]').click(function () {
var self = $(this);
swal({
@ -59,6 +60,45 @@ $(document).ready(function () {
});
});
$('[data-action="trigger-schedule"]').click(function (event) {
event.preventDefault();
var self = $(this);
swal({
type: 'info',
title: 'Trigger Schedule',
text: 'This will run the selected schedule now.',
showCancelButton: true,
allowOutsideClick: true,
closeOnConfirm: false,
confirmButtonText: 'Continue',
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'POST',
url: Router.route('server.schedules.trigger', {
server: Pterodactyl.server.uuidShort,
schedule: self.data('schedule-id'),
}),
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'),
},
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Schedule has been added to the next-run queue.'
});
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: 'An error occured while attempting to trigger this schedule.'
});
});
});
});
$('[data-action="toggle-schedule"]').click(function (event) {
var self = $(this);
swal({

View file

@ -30,6 +30,9 @@ return [
'command' => 'Send Command',
'power' => 'Power Action',
],
'toggle' => 'Toggle Status',
'run_now' => 'Trigger Schedule',
'schedule_created' => 'Successfully created a new schedule for this server.',
'unnamed' => 'Unnamed Schedule',
'setup' => 'Schedule Setup',
'day_of_week' => 'Day of Week',

View file

@ -6,7 +6,7 @@
@extends('layouts.master')
@section('title')
@lang('server.schedules.header')
@lang('server.schedule.header')
@endsection
@section('content-header')
@ -38,7 +38,6 @@
<th>@lang('strings.last_run')</th>
<th>@lang('strings.next_run')</th>
<th></th>
<th></th>
</tr>
@foreach($schedules as $schedule)
<tr @if(! $schedule->is_active)class="muted muted-hover"@endif>
@ -66,21 +65,20 @@
</td>
<td class="middle">
@if($schedule->is_active)
@if($schedule->last_run_at)
{{ Carbon::parse($schedule->next_run_at)->toDayDateTimeString() }}<br /><span class="text-muted small">({{ Carbon::parse($schedule->next_run_at)->diffForHumans() }})</span>
@else
<em class="text-muted">@lang('strings.not_run_yet')</em>
@endif
{{ Carbon::parse($schedule->next_run_at)->toDayDateTimeString() }}<br /><span class="text-muted small">({{ Carbon::parse($schedule->next_run_at)->diffForHumans() }})</span>
@else
<em>n/a</em>
@endif
</td>
@can('delete-schedule', $server)
<td class="text-center middle"><a href="#" data-action="delete-schedule" data-schedule-id="{{ $schedule->hashid }}"><i class="fa fa-fw fa-trash-o text-danger" data-toggle="tooltip" data-placement="top" title="@lang('strings.delete')"></i></a></td>
@endcan
@can('toggle-schedule', $server)
<td class="text-center middle"><a href="#" data-action="toggle-schedule" data-active="{{ $schedule->active }}" data-schedule-id="{{ $schedule->hashid }}"><i class="fa fa-fw fa-eye-slash text-primary" data-toggle="tooltip" data-placement="top" title="@lang('server.schedules.toggle')"></i></a></td>
@endcan
<td class="middle">
@can('delete-schedule', $server)
<a class="btn btn-xs btn-danger" href="#" data-action="delete-schedule" data-schedule-id="{{ $schedule->hashid }}" data-toggle="tooltip" data-placement="top" title="@lang('strings.delete')"><i class="fa fa-fw fa-trash-o"></i></a>
@endcan
@can('toggle-schedule', $server)
<a class="btn btn-xs btn-default" href="#" data-action="toggle-schedule" data-active="{{ $schedule->active }}" data-schedule-id="{{ $schedule->hashid }}" data-toggle="tooltip" data-placement="top" title="@lang('server.schedule.toggle')"><i class="fa fa-fw fa-eye-slash"></i></a>
<a class="btn btn-xs btn-default" href="#" data-action="trigger-schedule" data-schedule-id="{{ $schedule->hashid }}" data-toggle="tooltip" data-placement="top" title="@lang('server.schedule.run_now')"><i class="fa fa-fw fa-refresh"></i></a>
@endcan
</td>
</tr>
@endforeach
</tbody>

View file

@ -96,7 +96,8 @@ Route::group(['prefix' => 'schedules'], function () {
Route::get('/view/{schedule}', 'Tasks\TaskManagementController@view')->name('server.schedules.view');
Route::patch('/view/{schedule}', 'Tasks\TaskManagementController@update');
Route::patch('/view/{schedule}/toggle', 'Tasks\TaskToggleController@index')->name('server.schedules.toggle');
Route::post('/view/{schedule}/toggle', 'Tasks\ActionController@toggle')->name('server.schedules.toggle');
Route::post('/view/{schedule}/trigger', 'Tasks\ActionController@trigger')->name('server.schedules.trigger');
Route::delete('/view/{schedule}', 'Tasks\TaskManagementController@delete');
});

View file

@ -34,7 +34,7 @@ class CleanServiceBackupFilesCommandTest extends CommandTestCase
{
parent::setUp();
Carbon::setTestNow();
Carbon::setTestNow(Carbon::now());
$this->disk = m::mock(Filesystem::class);
$this->filesystem = m::mock(Factory::class);
$this->filesystem->shouldReceive('disk')->withNoArgs()->once()->andReturn($this->disk);

View file

@ -1,11 +1,4 @@
<?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 Tests\Unit\Jobs\Schedule;
@ -87,9 +80,10 @@ class RunTaskJobTest extends TestCase
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'power', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->powerRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
@ -116,9 +110,10 @@ class RunTaskJobTest extends TestCase
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'command', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
@ -145,9 +140,10 @@ class RunTaskJobTest extends TestCase
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'command', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
@ -176,19 +172,45 @@ class RunTaskJobTest extends TestCase
* Test that an exception is thrown if an invalid task action is supplied.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot run a task that points to a non-existant action.
* @expectedExceptionMessage Cannot run a task that points to a non-existent action.
*/
public function testInvalidActionPassedToJob()
{
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->getJobInstance($task->id, 1234);
}
/**
* Test that a schedule marked as disabled does not get processed.
*/
public function testScheduleMarkedAsDisabledDoesNotProcess()
{
$schedule = factory(Schedule::class)->make(['is_active' => false]);
$task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false,
'last_run_at' => Carbon::now()->toDateTimeString(),
])->once()->andReturn(1);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturn(1);
$this->getJobInstance($task->id, $schedule->id);
$this->assertTrue(true);
}
/**
* Run the job using the mocks provided.
*

View file

@ -1,15 +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 Tests\Unit\Services\Schedules;
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Cron\CronExpression;
use Pterodactyl\Models\Task;
@ -46,8 +40,8 @@ class ProcessScheduleServiceTest extends TestCase
public function setUp()
{
parent::setUp();
Carbon::setTestNow(Carbon::now());
$this->cron = m::mock('overload:' . CronExpression::class);
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->runnerService = m::mock(RunTaskService::class);
@ -64,14 +58,12 @@ class ProcessScheduleServiceTest extends TestCase
'sequence_id' => 1,
])]));
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => '00:00:00',
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
@ -80,58 +72,23 @@ class ProcessScheduleServiceTest extends TestCase
$this->assertTrue(true);
}
/**
* Test that passing a schedule model without a tasks relation is handled.
*/
public function testScheduleModelWithoutTasksIsHandled()
{
$nonRelationModel = factory(Schedule::class)->make();
$model = clone $nonRelationModel;
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
'sequence_id' => 1,
])]));
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->repository->shouldReceive('getScheduleWithTasks')->with($nonRelationModel->id)->once()->andReturn($model);
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => '00:00:00',
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->service->handle($nonRelationModel);
$this->assertTrue(true);
}
/**
* Test that a task ID can be passed in place of the task model.
*/
public function testPassingScheduleIdInPlaceOfModelIsHandled()
public function testScheduleRunTimeCanBeOverridden()
{
$model = factory(Schedule::class)->make();
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
'sequence_id' => 1,
])]));
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->repository->shouldReceive('getScheduleWithTasks')->with($model->id)->once()->andReturn($model);
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => '00:00:00',
'next_run_at' => Carbon::now()->addSeconds(15)->toDateTimeString(),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->service->handle($model->id);
$this->service->setRunTimeOverride(Carbon::now()->addSeconds(15))->handle($model);
$this->assertTrue(true);
}
}

View file

@ -1,17 +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 Tests\Unit\Services\Schedules;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Cron\CronExpression;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\ConnectionInterface;
@ -54,7 +47,6 @@ class ScheduleCreationServiceTest extends TestCase
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->cron = m::mock('overload:\Cron\CronExpression');
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->taskCreationService = m::mock(TaskCreationService::class);
@ -69,18 +61,15 @@ class ScheduleCreationServiceTest extends TestCase
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => 'nextDate',
'test_data' => 'test_value',
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_data' => 'test_value']);
$this->assertNotEmpty($response);
$response = $this->service->handle($server, ['test_key' => 'value', 'server_id' => '123abc']);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
@ -93,14 +82,13 @@ class ScheduleCreationServiceTest extends TestCase
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'next_run_at' => 'nextDate',
'server_id' => $server->id,
'test_data' => 'test_value',
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->taskCreationService->shouldReceive('handle')->with($schedule, [
'time_interval' => 'm',
'time_value' => 10,
@ -111,62 +99,10 @@ class ScheduleCreationServiceTest extends TestCase
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_data' => 'test_value'], [
$response = $this->service->handle($server, ['test_key' => 'value'], [
['time_interval' => 'm', 'time_value' => 10, 'action' => 'test', 'payload' => 'testpayload'],
]);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
/**
* Test that an ID can be passed in place of the server model.
*/
public function testIdCanBePassedInPlaceOfServerModel()
{
$schedule = factory(Schedule::class)->make();
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'next_run_at' => 'nextDate',
'server_id' => 1234,
'test_data' => 'test_value',
])->once()->andReturn($schedule);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle(1234, ['test_data' => 'test_value']);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
/**
* Test that an exception is raised if invalid data is passed.
*
* @dataProvider invalidServerArgumentProvider
* @expectedException \InvalidArgumentException
*/
public function testExceptionIsThrownIfServerIsInvalid($attribute)
{
$this->service->handle($attribute, []);
}
/**
* Return an array of invalid server data to test aganist.
*
* @return array
*/
public function invalidServerArgumentProvider()
{
return [
[123.456],
['server'],
['abc123'],
['123_test'],
[new Node()],
[Server::class],
];
}
}