Path: blob/1.0-develop/app/Console/Commands/Schedule/ProcessRunnableCommand.php
10262 views
<?php12namespace Pterodactyl\Console\Commands\Schedule;34use Illuminate\Console\Command;5use Pterodactyl\Models\Schedule;6use Illuminate\Support\Facades\Log;7use Illuminate\Database\Eloquent\Builder;8use Pterodactyl\Services\Schedules\ProcessScheduleService;910class ProcessRunnableCommand extends Command11{12protected $signature = 'p:schedule:process';1314protected $description = 'Process schedules in the database and determine which are ready to run.';1516/**17* Handle command execution.18*/19public function handle(): int20{21$schedules = Schedule::query()22->with('tasks')23->whereRelation('server', fn (Builder $builder) => $builder->whereNull('status'))24->where('is_active', true)25->where('is_processing', false)26->whereRaw('next_run_at <= NOW()')27->get();2829if ($schedules->count() < 1) {30$this->line('There are no scheduled tasks for servers that need to be run.');3132return 0;33}3435$bar = $this->output->createProgressBar(count($schedules));36foreach ($schedules as $schedule) {37$bar->clear();38$this->processSchedule($schedule);39$bar->advance();40$bar->display();41}4243$this->line('');4445return 0;46}4748/**49* Processes a given schedule and logs and errors encountered the console output. This should50* never throw an exception out, otherwise you'll end up killing the entire run group causing51* any other schedules to not process correctly.52*53* @see https://github.com/pterodactyl/panel/issues/260954*/55protected function processSchedule(Schedule $schedule)56{57if ($schedule->tasks->isEmpty()) {58return;59}6061try {62$this->getLaravel()->make(ProcessScheduleService::class)->handle($schedule);6364$this->line(trans('command/messages.schedule.output_line', [65'schedule' => $schedule->name,66'hash' => $schedule->hashid,67]));68} catch (\Throwable $exception) {69Log::error($exception, ['schedule_id' => $schedule->id]);7071$this->error("An error was encountered while processing Schedule #$schedule->id: " . $exception->getMessage());72}73}74}757677