2017-09-17 04:10:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Console\Commands\Schedule;
|
|
|
|
|
2020-10-27 03:16:39 +00:00
|
|
|
use Exception;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Throwable;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Illuminate\Console\Command;
|
2020-10-25 22:06:54 +00:00
|
|
|
use Pterodactyl\Models\Schedule;
|
2020-10-27 03:16:39 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-05-05 00:41:53 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2017-09-17 04:10:00 +00:00
|
|
|
use Pterodactyl\Services\Schedules\ProcessScheduleService;
|
|
|
|
|
|
|
|
class ProcessRunnableCommand extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-10-25 22:06:54 +00:00
|
|
|
protected $signature = 'p:schedule:process';
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-10-25 22:06:54 +00:00
|
|
|
protected $description = 'Process schedules in the database and determine which are ready to run.';
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
/**
|
2020-10-25 22:06:54 +00:00
|
|
|
* Handle command execution.
|
2017-09-17 04:10:00 +00:00
|
|
|
*/
|
2020-10-27 03:16:39 +00:00
|
|
|
public function handle()
|
2018-09-01 03:52:15 +00:00
|
|
|
{
|
2022-05-05 00:41:53 +00:00
|
|
|
$schedules = Schedule::query()
|
|
|
|
->with('tasks')
|
|
|
|
->whereRelation('server', fn (Builder $builder) => $builder->whereNull('status'))
|
2020-10-25 22:06:54 +00:00
|
|
|
->where('is_active', true)
|
|
|
|
->where('is_processing', false)
|
|
|
|
->whereRaw('next_run_at <= NOW()')
|
|
|
|
->get();
|
2017-09-17 04:10:00 +00:00
|
|
|
|
2018-09-03 21:32:33 +00:00
|
|
|
if ($schedules->count() < 1) {
|
|
|
|
$this->line('There are no scheduled tasks for servers that need to be run.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
$bar = $this->output->createProgressBar(count($schedules));
|
2020-10-25 22:06:54 +00:00
|
|
|
foreach ($schedules as $schedule) {
|
2020-10-27 03:16:39 +00:00
|
|
|
$bar->clear();
|
|
|
|
$this->processSchedule($schedule);
|
2017-09-17 04:10:00 +00:00
|
|
|
$bar->advance();
|
2017-09-20 03:10:14 +00:00
|
|
|
$bar->display();
|
2020-10-25 22:06:54 +00:00
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
|
|
|
|
$this->line('');
|
|
|
|
}
|
2020-10-27 03:16:39 +00: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
|
|
|
|
*/
|
|
|
|
protected function processSchedule(Schedule $schedule)
|
|
|
|
{
|
|
|
|
if ($schedule->tasks->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->getLaravel()->make(ProcessScheduleService::class)->handle($schedule);
|
|
|
|
|
|
|
|
$this->line(trans('command/messages.schedule.output_line', [
|
|
|
|
'schedule' => $schedule->name,
|
|
|
|
'hash' => $schedule->hashid,
|
|
|
|
]));
|
2022-05-04 23:01:29 +00:00
|
|
|
} catch (Throwable|Exception $exception) {
|
2020-10-27 03:16:39 +00:00
|
|
|
Log::error($exception, ['schedule_id' => $schedule->id]);
|
|
|
|
|
|
|
|
$this->error("An error was encountered while processing Schedule #{$schedule->id}: " . $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 04:10:00 +00:00
|
|
|
}
|