From 178b8f8ce6cb66154cd7bc525bcc6d70dfcee3af Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 31 Aug 2018 21:00:13 -0700 Subject: [PATCH] More logical time handling --- .../Server/Tasks/ActionController.php | 14 ++++++++++++-- .../Schedules/ProcessScheduleService.php | 17 +++++++++-------- .../Schedules/ProcessScheduleServiceTest.php | 11 ++++++----- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Server/Tasks/ActionController.php b/app/Http/Controllers/Server/Tasks/ActionController.php index 410d7c189..8d5a83bd7 100644 --- a/app/Http/Controllers/Server/Tasks/ActionController.php +++ b/app/Http/Controllers/Server/Tasks/ActionController.php @@ -2,7 +2,7 @@ namespace Pterodactyl\Http\Controllers\Server\Tasks; -use Carbon\Carbon; +use Cake\Chronos\Chronos; use Illuminate\Http\Request; use Illuminate\Http\Response; use Pterodactyl\Http\Controllers\Controller; @@ -11,12 +11,22 @@ use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; class ActionController extends Controller { + /** + * @var \Pterodactyl\Services\Schedules\ProcessScheduleService + */ private $processScheduleService; + /** * @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface */ private $repository; + /** + * ActionController constructor. + * + * @param \Pterodactyl\Services\Schedules\ProcessScheduleService $processScheduleService + * @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository + */ public function __construct(ProcessScheduleService $processScheduleService, ScheduleRepositoryInterface $repository) { $this->processScheduleService = $processScheduleService; @@ -61,7 +71,7 @@ class ActionController extends Controller $server = $request->attributes->get('server'); $this->authorize('toggle-schedule', $server); - $this->processScheduleService->setRunTimeOverride(Carbon::now())->handle( + $this->processScheduleService->setRunTimeOverride(Chronos::now())->handle( $request->attributes->get('schedule') ); diff --git a/app/Services/Schedules/ProcessScheduleService.php b/app/Services/Schedules/ProcessScheduleService.php index b134caa35..8c157e39e 100644 --- a/app/Services/Schedules/ProcessScheduleService.php +++ b/app/Services/Schedules/ProcessScheduleService.php @@ -2,8 +2,9 @@ namespace Pterodactyl\Services\Schedules; -use Carbon\Carbon; +use DateTimeInterface; use Cron\CronExpression; +use Cake\Chronos\Chronos; use Pterodactyl\Models\Schedule; use Pterodactyl\Services\Schedules\Tasks\RunTaskService; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; @@ -21,7 +22,7 @@ class ProcessScheduleService private $runnerService; /** - * @var \Carbon\Carbon|null + * @var \DateTimeInterface|null */ private $runTimeOverride; @@ -41,10 +42,10 @@ class ProcessScheduleService * 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 + * @param \DateTimeInterface $time * @return $this */ - public function setRunTimeOverride(Carbon $time) + public function setRunTimeOverride(DateTimeInterface $time) { $this->runTimeOverride = $time; @@ -83,14 +84,14 @@ class ProcessScheduleService * Get the timestamp to store in the database as the next_run time for a schedule. * * @param string $formatted - * @return \DateTime|string + * @return string */ private function getRunAtTime(string $formatted) { - if ($this->runTimeOverride instanceof Carbon) { - return $this->runTimeOverride->toDateTimeString(); + if (! is_null($this->runTimeOverride)) { + return $this->runTimeOverride->format(Chronos::ATOM); } - return CronExpression::factory($formatted)->getNextRunDate(); + return CronExpression::factory($formatted)->getNextRunDate()->format(Chronos::ATOM); } } diff --git a/tests/Unit/Services/Schedules/ProcessScheduleServiceTest.php b/tests/Unit/Services/Schedules/ProcessScheduleServiceTest.php index 27e20082a..92f7c3960 100644 --- a/tests/Unit/Services/Schedules/ProcessScheduleServiceTest.php +++ b/tests/Unit/Services/Schedules/ProcessScheduleServiceTest.php @@ -3,9 +3,9 @@ namespace Tests\Unit\Services\Schedules; use Mockery as m; -use Carbon\Carbon; use Tests\TestCase; use Cron\CronExpression; +use Cake\Chronos\Chronos; use Pterodactyl\Models\Task; use Pterodactyl\Models\Schedule; use Pterodactyl\Services\Schedules\Tasks\RunTaskService; @@ -40,7 +40,8 @@ class ProcessScheduleServiceTest extends TestCase public function setUp() { parent::setUp(); - Carbon::setTestNow(Carbon::now()); + + Chronos::setTestNow(Chronos::now()); $this->repository = m::mock(ScheduleRepositoryInterface::class); $this->runnerService = m::mock(RunTaskService::class); @@ -63,7 +64,7 @@ class ProcessScheduleServiceTest extends TestCase $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('update')->with($model->id, [ 'is_processing' => true, - 'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(), + 'next_run_at' => CronExpression::factory($formatted)->getNextRunDate()->format(Chronos::ATOM), ]); $this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull(); @@ -83,12 +84,12 @@ class ProcessScheduleServiceTest extends TestCase $this->repository->shouldReceive('update')->with($model->id, [ 'is_processing' => true, - 'next_run_at' => Carbon::now()->addSeconds(15)->toDateTimeString(), + 'next_run_at' => Chronos::now()->addSeconds(15)->toAtomString(), ]); $this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull(); - $this->service->setRunTimeOverride(Carbon::now()->addSeconds(15))->handle($model); + $this->service->setRunTimeOverride(Chronos::now()->addSeconds(15))->handle($model); $this->assertTrue(true); } }