|
1 | 1 | <?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 | | - */ |
9 | 2 |
|
10 | 3 | namespace Pterodactyl\Console\Commands\Schedule; |
11 | 4 |
|
12 | | -use Cake\Chronos\Chronos; |
| 5 | +use Throwable; |
| 6 | +use Exception; |
13 | 7 | use Illuminate\Console\Command; |
14 | | -use Illuminate\Support\Collection; |
| 8 | +use Pterodactyl\Models\Schedule; |
| 9 | +use Illuminate\Support\Facades\Log; |
15 | 10 | use Pterodactyl\Services\Schedules\ProcessScheduleService; |
16 | | -use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; |
17 | 11 |
|
18 | 12 | class ProcessRunnableCommand extends Command |
19 | 13 | { |
20 | | - /** |
21 | | - * @var string |
22 | | - */ |
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; |
34 | | - |
35 | 14 | /** |
36 | 15 | * @var string |
37 | 16 | */ |
38 | 17 | protected $signature = 'p:schedule:process'; |
39 | 18 |
|
40 | 19 | /** |
41 | | - * ProcessRunnableCommand constructor. |
42 | | - * |
43 | | - * @param \Pterodactyl\Services\Schedules\ProcessScheduleService $processScheduleService |
44 | | - * @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository |
| 20 | + * @var string |
45 | 21 | */ |
46 | | - public function __construct(ProcessScheduleService $processScheduleService, ScheduleRepositoryInterface $repository) |
47 | | - { |
48 | | - parent::__construct(); |
49 | | - |
50 | | - $this->processScheduleService = $processScheduleService; |
51 | | - $this->repository = $repository; |
52 | | - } |
| 22 | + protected $description = 'Process schedules in the database and determine which are ready to run.'; |
53 | 23 |
|
54 | 24 | /** |
55 | 25 | * Handle command execution. |
56 | 26 | */ |
57 | 27 | public function handle() |
58 | 28 | { |
59 | | - $schedules = $this->repository->getSchedulesToProcess(Chronos::now()->toAtomString()); |
| 29 | + $schedules = Schedule::query()->with('tasks') |
| 30 | + ->where('is_active', true) |
| 31 | + ->where('is_processing', false) |
| 32 | + ->whereRaw('next_run_at <= NOW()') |
| 33 | + ->get(); |
| 34 | + |
60 | 35 | if ($schedules->count() < 1) { |
61 | 36 | $this->line('There are no scheduled tasks for servers that need to be run.'); |
62 | 37 |
|
63 | 38 | return; |
64 | 39 | } |
65 | 40 |
|
66 | 41 | $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); |
70 | | - |
71 | | - if ($this->input->isInteractive()) { |
72 | | - $bar->clear(); |
73 | | - $this->line(trans('command/messages.schedule.output_line', [ |
74 | | - 'schedule' => $schedule->name, |
75 | | - 'hash' => $schedule->hashid, |
76 | | - ])); |
77 | | - } |
78 | | - } |
79 | | - |
| 42 | + foreach ($schedules as $schedule) { |
| 43 | + $bar->clear(); |
| 44 | + $this->processSchedule($schedule); |
80 | 45 | $bar->advance(); |
81 | 46 | $bar->display(); |
82 | | - }); |
| 47 | + } |
83 | 48 |
|
84 | 49 | $this->line(''); |
85 | 50 | } |
| 51 | + |
| 52 | + /** |
| 53 | + * Processes a given schedule and logs and errors encountered the console output. This should |
| 54 | + * never throw an exception out, otherwise you'll end up killing the entire run group causing |
| 55 | + * any other schedules to not process correctly. |
| 56 | + * |
| 57 | + * @param \Pterodactyl\Models\Schedule $schedule |
| 58 | + * @see https://github.com/pterodactyl/panel/issues/2609 |
| 59 | + */ |
| 60 | + protected function processSchedule(Schedule $schedule) |
| 61 | + { |
| 62 | + if ($schedule->tasks->isEmpty()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + $this->getLaravel()->make(ProcessScheduleService::class)->handle($schedule); |
| 68 | + |
| 69 | + $this->line(trans('command/messages.schedule.output_line', [ |
| 70 | + 'schedule' => $schedule->name, |
| 71 | + 'hash' => $schedule->hashid, |
| 72 | + ])); |
| 73 | + } catch (Throwable | Exception $exception) { |
| 74 | + Log::error($exception, ['schedule_id' => $schedule->id]); |
| 75 | + |
| 76 | + $this->error("An error was encountered while processing Schedule #{$schedule->id}: " . $exception->getMessage()); |
| 77 | + } |
| 78 | + } |
86 | 79 | } |
0 commit comments