Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Console/Commands/Schedule/ProcessRunnableCommand.php
10262 views
1
<?php
2
3
namespace Pterodactyl\Console\Commands\Schedule;
4
5
use Illuminate\Console\Command;
6
use Pterodactyl\Models\Schedule;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Database\Eloquent\Builder;
9
use Pterodactyl\Services\Schedules\ProcessScheduleService;
10
11
class ProcessRunnableCommand extends Command
12
{
13
protected $signature = 'p:schedule:process';
14
15
protected $description = 'Process schedules in the database and determine which are ready to run.';
16
17
/**
18
* Handle command execution.
19
*/
20
public function handle(): int
21
{
22
$schedules = Schedule::query()
23
->with('tasks')
24
->whereRelation('server', fn (Builder $builder) => $builder->whereNull('status'))
25
->where('is_active', true)
26
->where('is_processing', false)
27
->whereRaw('next_run_at <= NOW()')
28
->get();
29
30
if ($schedules->count() < 1) {
31
$this->line('There are no scheduled tasks for servers that need to be run.');
32
33
return 0;
34
}
35
36
$bar = $this->output->createProgressBar(count($schedules));
37
foreach ($schedules as $schedule) {
38
$bar->clear();
39
$this->processSchedule($schedule);
40
$bar->advance();
41
$bar->display();
42
}
43
44
$this->line('');
45
46
return 0;
47
}
48
49
/**
50
* Processes a given schedule and logs and errors encountered the console output. This should
51
* never throw an exception out, otherwise you'll end up killing the entire run group causing
52
* any other schedules to not process correctly.
53
*
54
* @see https://github.com/pterodactyl/panel/issues/2609
55
*/
56
protected function processSchedule(Schedule $schedule)
57
{
58
if ($schedule->tasks->isEmpty()) {
59
return;
60
}
61
62
try {
63
$this->getLaravel()->make(ProcessScheduleService::class)->handle($schedule);
64
65
$this->line(trans('command/messages.schedule.output_line', [
66
'schedule' => $schedule->name,
67
'hash' => $schedule->hashid,
68
]));
69
} catch (\Throwable $exception) {
70
Log::error($exception, ['schedule_id' => $schedule->id]);
71
72
$this->error("An error was encountered while processing Schedule #$schedule->id: " . $exception->getMessage());
73
}
74
}
75
}
76
77