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