More logical time handling

This commit is contained in:
Dane Everitt 2018-08-31 21:00:13 -07:00
parent e5636405f3
commit 178b8f8ce6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 27 additions and 15 deletions

View file

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

View file

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

View file

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