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
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue