Skip to content

Commit 8c6327f

Browse files
committed
Let MySQL do the time logic when looking for tasks
1 parent 23872b8 commit 8c6327f

File tree

5 files changed

+23
-167
lines changed

5 files changed

+23
-167
lines changed

app/Console/Commands/Schedule/ProcessRunnableCommand.php

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,48 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Console\Commands\Schedule;
114

12-
use Cake\Chronos\Chronos;
135
use Illuminate\Console\Command;
14-
use Illuminate\Support\Collection;
6+
use Pterodactyl\Models\Schedule;
157
use Pterodactyl\Services\Schedules\ProcessScheduleService;
16-
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
178

189
class ProcessRunnableCommand extends Command
1910
{
2011
/**
2112
* @var string
2213
*/
23-
protected $description = 'Process schedules in the database and determine which are ready to run.';
24-
25-
/**
26-
* @var \Pterodactyl\Services\Schedules\ProcessScheduleService
27-
*/
28-
protected $processScheduleService;
29-
30-
/**
31-
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
32-
*/
33-
protected $repository;
14+
protected $signature = 'p:schedule:process';
3415

3516
/**
3617
* @var string
3718
*/
38-
protected $signature = 'p:schedule:process';
19+
protected $description = 'Process schedules in the database and determine which are ready to run.';
3920

4021
/**
41-
* ProcessRunnableCommand constructor.
22+
* Handle command execution.
23+
*
24+
* @param \Pterodactyl\Services\Schedules\ProcessScheduleService $service
4225
*
43-
* @param \Pterodactyl\Services\Schedules\ProcessScheduleService $processScheduleService
44-
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
26+
* @throws \Throwable
4527
*/
46-
public function __construct(ProcessScheduleService $processScheduleService, ScheduleRepositoryInterface $repository)
28+
public function handle(ProcessScheduleService $service)
4729
{
48-
parent::__construct();
30+
$schedules = Schedule::query()->with('tasks')
31+
->where('is_active', true)
32+
->where('is_processing', false)
33+
->whereRaw('next_run_at <= NOW()')
34+
->get();
4935

50-
$this->processScheduleService = $processScheduleService;
51-
$this->repository = $repository;
52-
}
53-
54-
/**
55-
* Handle command execution.
56-
*/
57-
public function handle()
58-
{
59-
$schedules = $this->repository->getSchedulesToProcess(Chronos::now()->toAtomString());
6036
if ($schedules->count() < 1) {
6137
$this->line('There are no scheduled tasks for servers that need to be run.');
6238

6339
return;
6440
}
6541

6642
$bar = $this->output->createProgressBar(count($schedules));
67-
$schedules->each(function ($schedule) use ($bar) {
68-
if ($schedule->tasks instanceof Collection && count($schedule->tasks) > 0) {
69-
$this->processScheduleService->handle($schedule);
43+
foreach ($schedules as $schedule) {
44+
if ($schedule->tasks->isNotEmpty()) {
45+
$service->handle($schedule);
7046

7147
if ($this->input->isInteractive()) {
7248
$bar->clear();
@@ -79,7 +55,7 @@ public function handle()
7955

8056
$bar->advance();
8157
$bar->display();
82-
});
58+
}
8359

8460
$this->line('');
8561
}

app/Contracts/Repository/ScheduleRepositoryInterface.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,4 @@ public function findServerSchedules(int $server): Collection;
2424
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2525
*/
2626
public function getScheduleWithTasks(int $schedule): Schedule;
27-
28-
/**
29-
* Return all of the schedules that should be processed.
30-
*
31-
* @param string $timestamp
32-
* @return \Illuminate\Support\Collection
33-
*/
34-
public function getSchedulesToProcess(string $timestamp): Collection;
3527
}

app/Helpers/Utilities.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public static function getScheduleNextRunDate(string $minute, string $hour, stri
5252
)->getNextRunDate());
5353
}
5454

55-
public static function checked($name, $default)
55+
/**
56+
* @param string $name
57+
* @param mixed $default
58+
* @return string
59+
*/
60+
public static function checked(string $name, $default)
5661
{
5762
$errors = session('errors');
5863

app/Repositories/Eloquent/ScheduleRepository.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,4 @@ public function getScheduleWithTasks(int $schedule): Schedule
4747
throw new RecordNotFoundException;
4848
}
4949
}
50-
51-
/**
52-
* Return all of the schedules that should be processed.
53-
*
54-
* @param string $timestamp
55-
* @return \Illuminate\Support\Collection
56-
*/
57-
public function getSchedulesToProcess(string $timestamp): Collection
58-
{
59-
return $this->getBuilder()->with('tasks')
60-
->where('is_active', true)
61-
->where('is_processing', false)
62-
->where('next_run_at', '<=', $timestamp)
63-
->get($this->getColumns());
64-
}
6550
}

tests/Unit/Commands/Schedule/ProcessRunnableCommandTest.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)