Skip to content

Commit 178b8f8

Browse files
committed
More logical time handling
1 parent e563640 commit 178b8f8

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

app/Http/Controllers/Server/Tasks/ActionController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pterodactyl\Http\Controllers\Server\Tasks;
44

5-
use Carbon\Carbon;
5+
use Cake\Chronos\Chronos;
66
use Illuminate\Http\Request;
77
use Illuminate\Http\Response;
88
use Pterodactyl\Http\Controllers\Controller;
@@ -11,12 +11,22 @@
1111

1212
class ActionController extends Controller
1313
{
14+
/**
15+
* @var \Pterodactyl\Services\Schedules\ProcessScheduleService
16+
*/
1417
private $processScheduleService;
18+
1519
/**
1620
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
1721
*/
1822
private $repository;
1923

24+
/**
25+
* ActionController constructor.
26+
*
27+
* @param \Pterodactyl\Services\Schedules\ProcessScheduleService $processScheduleService
28+
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
29+
*/
2030
public function __construct(ProcessScheduleService $processScheduleService, ScheduleRepositoryInterface $repository)
2131
{
2232
$this->processScheduleService = $processScheduleService;
@@ -61,7 +71,7 @@ public function trigger(Request $request): Response
6171
$server = $request->attributes->get('server');
6272
$this->authorize('toggle-schedule', $server);
6373

64-
$this->processScheduleService->setRunTimeOverride(Carbon::now())->handle(
74+
$this->processScheduleService->setRunTimeOverride(Chronos::now())->handle(
6575
$request->attributes->get('schedule')
6676
);
6777

app/Services/Schedules/ProcessScheduleService.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Pterodactyl\Services\Schedules;
44

5-
use Carbon\Carbon;
5+
use DateTimeInterface;
66
use Cron\CronExpression;
7+
use Cake\Chronos\Chronos;
78
use Pterodactyl\Models\Schedule;
89
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
910
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
@@ -21,7 +22,7 @@ class ProcessScheduleService
2122
private $runnerService;
2223

2324
/**
24-
* @var \Carbon\Carbon|null
25+
* @var \DateTimeInterface|null
2526
*/
2627
private $runTimeOverride;
2728

@@ -41,10 +42,10 @@ public function __construct(RunTaskService $runnerService, ScheduleRepositoryInt
4142
* Set the time that this schedule should be run at. This will override the time
4243
* defined on the schedule itself. Useful for triggering one-off task runs.
4344
*
44-
* @param \Carbon\Carbon $time
45+
* @param \DateTimeInterface $time
4546
* @return $this
4647
*/
47-
public function setRunTimeOverride(Carbon $time)
48+
public function setRunTimeOverride(DateTimeInterface $time)
4849
{
4950
$this->runTimeOverride = $time;
5051

@@ -83,14 +84,14 @@ public function handle(Schedule $schedule)
8384
* Get the timestamp to store in the database as the next_run time for a schedule.
8485
*
8586
* @param string $formatted
86-
* @return \DateTime|string
87+
* @return string
8788
*/
8889
private function getRunAtTime(string $formatted)
8990
{
90-
if ($this->runTimeOverride instanceof Carbon) {
91-
return $this->runTimeOverride->toDateTimeString();
91+
if (! is_null($this->runTimeOverride)) {
92+
return $this->runTimeOverride->format(Chronos::ATOM);
9293
}
9394

94-
return CronExpression::factory($formatted)->getNextRunDate();
95+
return CronExpression::factory($formatted)->getNextRunDate()->format(Chronos::ATOM);
9596
}
9697
}

tests/Unit/Services/Schedules/ProcessScheduleServiceTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Tests\Unit\Services\Schedules;
44

55
use Mockery as m;
6-
use Carbon\Carbon;
76
use Tests\TestCase;
87
use Cron\CronExpression;
8+
use Cake\Chronos\Chronos;
99
use Pterodactyl\Models\Task;
1010
use Pterodactyl\Models\Schedule;
1111
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
@@ -40,7 +40,8 @@ class ProcessScheduleServiceTest extends TestCase
4040
public function setUp()
4141
{
4242
parent::setUp();
43-
Carbon::setTestNow(Carbon::now());
43+
44+
Chronos::setTestNow(Chronos::now());
4445

4546
$this->repository = m::mock(ScheduleRepositoryInterface::class);
4647
$this->runnerService = m::mock(RunTaskService::class);
@@ -63,7 +64,7 @@ public function testScheduleIsUpdatedAndRun()
6364
$formatted = sprintf('%s %s %s * %s', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
6465
$this->repository->shouldReceive('update')->with($model->id, [
6566
'is_processing' => true,
66-
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),
67+
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate()->format(Chronos::ATOM),
6768
]);
6869

6970
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
@@ -83,12 +84,12 @@ public function testScheduleRunTimeCanBeOverridden()
8384

8485
$this->repository->shouldReceive('update')->with($model->id, [
8586
'is_processing' => true,
86-
'next_run_at' => Carbon::now()->addSeconds(15)->toDateTimeString(),
87+
'next_run_at' => Chronos::now()->addSeconds(15)->toAtomString(),
8788
]);
8889

8990
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
9091

91-
$this->service->setRunTimeOverride(Carbon::now()->addSeconds(15))->handle($model);
92+
$this->service->setRunTimeOverride(Chronos::now()->addSeconds(15))->handle($model);
9293
$this->assertTrue(true);
9394
}
9495
}

0 commit comments

Comments
 (0)