Files
panel/app/Console/Commands/Schedule/ProcessRunnableCommand.php

74 lines
2.2 KiB
PHP
Raw Normal View History

2017-09-16 23:10:00 -05:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Console\Commands\Schedule;
2017-09-16 23:10:00 -05:00
use Illuminate\Console\Command;
2024-03-12 22:39:16 -04:00
use App\Models\Schedule;
use Illuminate\Database\Eloquent\Builder;
2024-03-12 22:39:16 -04:00
use App\Services\Schedules\ProcessScheduleService;
2024-05-07 08:55:05 +02:00
use Carbon\Carbon;
2017-09-16 23:10:00 -05:00
class ProcessRunnableCommand extends Command
{
protected $signature = 'p:schedule:process';
2017-09-16 23:10:00 -05:00
protected $description = 'Process schedules in the database and determine which are ready to run.';
2017-09-16 23:10:00 -05:00
/**
* Handle command execution.
2017-09-16 23:10:00 -05:00
*/
public function handle(): int
2018-08-31 20:52:15 -07:00
{
$schedules = Schedule::query()
->with('tasks')
->whereRelation('server', fn (Builder $builder) => $builder->whereNull('status'))
->where('is_active', true)
->where('is_processing', false)
2024-05-07 08:55:05 +02:00
->whereDate('next_run_at', '<=', Carbon::now()->toDateString())
->get();
2017-09-16 23:10:00 -05:00
if ($schedules->count() < 1) {
$this->line(__('commands.schedule.process.no_tasks'));
return 0;
}
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();
$bar->display();
}
2017-09-16 23:10:00 -05:00
$this->line('');
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.
*/
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,
2024-05-30 00:41:44 +02:00
'id' => $schedule->id,
]));
2023-02-23 12:30:16 -07:00
} catch (\Throwable|\Exception $exception) {
2024-03-16 23:27:24 -04:00
logger()->error($exception, ['schedule_id' => $schedule->id]);
$this->error(__('commands.schedule.process.no_tasks') . " #$schedule->id: " . $exception->getMessage());
}
}
2017-09-16 23:10:00 -05:00
}