Files

79 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2017-09-16 23:10:00 -05:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2017-09-16 23:10:00 -05:00
namespace Pterodactyl\Console\Commands\Schedule;
2026-10-05 18:10:18 -04:00
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
2017-09-16 23:10:00 -05:00
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Log;
use Pterodactyl\Contracts\Schedules\ProcessesSchedules;
use Pterodactyl\Models\Schedule;
use Throwable;
2017-09-16 23:10:00 -05:00
2026-10-05 18:10:18 -04:00
#[Description('Process schedules in the database and determine which are ready to run.')]
#[Signature('p:schedule:process')]
2017-09-16 23:10:00 -05:00
class ProcessRunnableCommand extends Command
{
/**
* Handle command execution.
2017-09-16 23:10:00 -05:00
*/
2022-10-14 10:59:20 -06:00
public function handle(): int
2018-08-31 20:52:15 -07:00
{
$schedules = Schedule::query()
2026-10-05 18:10:18 -04:00
->with(['tasks', 'server.node'])
->whereRelation('server', fn (Builder $builder) => $builder->whereNull('status'))
->where('is_active', true)
->where('is_processing', false)
->whereRaw('next_run_at <= NOW()')
->get();
2017-09-16 23:10:00 -05:00
2018-09-03 14:32:33 -07:00
if ($schedules->count() < 1) {
$this->line('There are no scheduled tasks for servers that need to be run.');
2022-10-14 10:59:20 -06:00
return 0;
2018-09-03 14:32:33 -07:00
}
2017-09-16 23:10:00 -05:00
$bar = $this->output->createProgressBar(count($schedules));
foreach ($schedules as $schedule) {
$bar->clear();
$this->processSchedule($schedule);
2017-09-16 23:10:00 -05:00
$bar->advance();
2017-09-19 22:10:14 -05:00
$bar->display();
}
2017-09-16 23:10:00 -05:00
$this->line('');
2022-10-14 10:59:20 -06:00
return 0;
2017-09-16 23:10:00 -05:00
}
/**
* Processes a given schedule and logs and errors encountered the console output. This should
* never throw an exception out, otherwise you'll end up killing the entire run group causing
* any other schedules to not process correctly.
*
* @see https://github.com/pterodactyl/panel/issues/2609
*/
2026-10-05 18:10:18 -04:00
protected function processSchedule(Schedule $schedule): void
{
if ($schedule->tasks->isEmpty()) {
return;
}
try {
2026-10-05 18:10:18 -04:00
$this->getLaravel()->make(ProcessesSchedules::class)->process($schedule);
$this->line(trans('command/messages.schedule.output_line', [
'schedule' => $schedule->name,
'hash' => $schedule->hashid,
]));
2026-10-05 18:10:18 -04:00
} catch (Throwable $throwable) {
Log::error($throwable->getMessage(), ['exception' => $throwable, 'schedule_id' => $schedule->id]);
2026-10-05 18:10:18 -04:00
$this->error("An error was encountered while processing Schedule #$schedule->id: ".$throwable->getMessage());
}
}
2017-09-16 23:10:00 -05:00
}