Path: blob/1.0-develop/app/Console/Commands/Schedule/ProcessRunnableCommand.php
7461 views
<?php12namespace Pterodactyl\Console\Commands\Schedule;34use Exception;5use Illuminate\Console\Command;6use Pterodactyl\Models\Schedule;7use Illuminate\Support\Facades\Log;8use Illuminate\Database\Eloquent\Builder;9use Pterodactyl\Services\Schedules\ProcessScheduleService;1011class ProcessRunnableCommand extends Command12{13protected $signature = 'p:schedule:process';1415protected $description = 'Process schedules in the database and determine which are ready to run.';1617/**18* Handle command execution.19*/20public function handle(): int21{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();2930if ($schedules->count() < 1) {31$this->line('There are no scheduled tasks for servers that need to be run.');3233return 0;34}3536$bar = $this->output->createProgressBar(count($schedules));37foreach ($schedules as $schedule) {38$bar->clear();39$this->processSchedule($schedule);40$bar->advance();41$bar->display();42}4344$this->line('');4546return 0;47}4849/**50* Processes a given schedule and logs and errors encountered the console output. This should51* never throw an exception out, otherwise you'll end up killing the entire run group causing52* any other schedules to not process correctly.53*54* @see https://github.com/pterodactyl/panel/issues/260955*/56protected function processSchedule(Schedule $schedule)57{58if ($schedule->tasks->isEmpty()) {59return;60}6162try {63$this->getLaravel()->make(ProcessScheduleService::class)->handle($schedule);6465$this->line(trans('command/messages.schedule.output_line', [66'schedule' => $schedule->name,67'hash' => $schedule->hashid,68]));69} catch (\Throwable|\Exception $exception) {70Log::error($exception, ['schedule_id' => $schedule->id]);7172$this->error("An error was encountered while processing Schedule #$schedule->id: " . $exception->getMessage());73}74}75}767778