diff --git a/app/Console/Commands/ProcessQueue.php b/app/Console/Commands/ProcessQueue.php index 14a3e360..4bd8ab5e 100644 --- a/app/Console/Commands/ProcessQueue.php +++ b/app/Console/Commands/ProcessQueue.php @@ -22,7 +22,11 @@ class ProcessQueue extends Command '--stop-when-empty' => null, ]); - Log::info(Artisan::output()); + $jobOutput = trim(Artisan::output()); + if (!empty($jobOutput)) { + Log::info($jobOutput); + } + ///** @var App\Support\WorkCommand $queueWorker */ //$queueWorker = new App\Support\WorkCommand(app('queue.worker'), app('cache.store')); diff --git a/app/Console/Cron.php b/app/Console/Cron.php new file mode 100644 index 00000000..5808ba96 --- /dev/null +++ b/app/Console/Cron.php @@ -0,0 +1,90 @@ +scheduler = $scheduler; + foreach ($this->cronTasks as $task) { + /** @var Command $cronTask */ + $cronTask = app($task); + $signature = $cronTask->getSignature(); + + if (empty($signature)) { + continue; + } + + $this->cronRunners[$signature] = $cronTask; + } + } + + /** + * Try to figure out which commands are supposed to run right now + * + * @return array string of tasks that were run + */ + public function run(): array + { + $events = $this->scheduler->dueEvents(app()); + if (empty($events)) { + return []; + } + + $run = []; + + /** @var \Illuminate\Console\Scheduling\Event $event */ + foreach ($events as $event) { + foreach ($this->cronRunners as $signature => $task) { + if (!str_contains($event->command, $signature)) { + continue; + } + + $task->callEvent(); + $run[] = $signature; + } + } + + return $run; + } +} diff --git a/app/Console/Cron/FifteenMinute.php b/app/Console/Cron/FifteenMinute.php new file mode 100644 index 00000000..86ad9481 --- /dev/null +++ b/app/Console/Cron/FifteenMinute.php @@ -0,0 +1,26 @@ +callEvent(); + } + + public function callEvent() + { + event(new CronFifteenMinute()); + } +} diff --git a/app/Console/Cron/FiveMinute.php b/app/Console/Cron/FiveMinute.php new file mode 100644 index 00000000..801386d9 --- /dev/null +++ b/app/Console/Cron/FiveMinute.php @@ -0,0 +1,29 @@ +callEvent(); + } + + public function callEvent() + { + event(new CronFiveMinute()); + } +} diff --git a/app/Console/Cron/Hourly.php b/app/Console/Cron/Hourly.php index f0c894c2..cebdcf9d 100644 --- a/app/Console/Cron/Hourly.php +++ b/app/Console/Cron/Hourly.php @@ -2,14 +2,14 @@ namespace App\Console\Cron; -use App\Contracts\Command; +use App\Contracts\CronCommand; use App\Events\CronHourly; /** * This just calls the CronHourly event, so all of the * listeners, etc can just be called to run those tasks */ -class Hourly extends Command +class Hourly extends CronCommand { protected $signature = 'cron:hourly'; protected $description = 'Run the hourly cron tasks'; @@ -17,7 +17,11 @@ class Hourly extends Command public function handle(): void { - $this->redirectLoggingToFile('cron'); + $this->callEvent(); + } + + public function callEvent() + { event(new CronHourly()); } } diff --git a/app/Console/Cron/JobQueue.php b/app/Console/Cron/JobQueue.php index e8f47fb1..0cf3a974 100644 --- a/app/Console/Cron/JobQueue.php +++ b/app/Console/Cron/JobQueue.php @@ -2,14 +2,14 @@ namespace App\Console\Cron; -use App\Contracts\Command; +use App\Contracts\CronCommand; use Illuminate\Support\Facades\Artisan; /** * This just calls the CronHourly event, so all of the * listeners, etc can just be called to run those tasks */ -class JobQueue extends Command +class JobQueue extends CronCommand { protected $signature = 'cron:queue'; protected $description = 'Run the cron queue tasks'; @@ -17,9 +17,16 @@ class JobQueue extends Command public function handle(): void { - $this->redirectLoggingToFile('cron'); - Artisan::call('queue:cron'); + $this->callEvent(); - $this->info(Artisan::output()); + $queueOutput = trim(Artisan::output()); + if (!empty($queueOutput)) { + $this->info($queueOutput); + } + } + + public function callEvent() + { + Artisan::call('queue:cron'); } } diff --git a/app/Console/Cron/Monthly.php b/app/Console/Cron/Monthly.php index 33b21768..a0757988 100644 --- a/app/Console/Cron/Monthly.php +++ b/app/Console/Cron/Monthly.php @@ -2,7 +2,7 @@ namespace App\Console\Cron; -use App\Contracts\Command; +use App\Contracts\CronCommand; use App\Events\CronMonthly; /** @@ -11,7 +11,7 @@ use App\Events\CronMonthly; * * The actual cron tasks are in app/Cron */ -class Monthly extends Command +class Monthly extends CronCommand { protected $signature = 'cron:monthly'; protected $description = 'Run the monthly cron tasks'; @@ -19,7 +19,11 @@ class Monthly extends Command public function handle(): void { - $this->redirectLoggingToFile('cron'); + $this->callEvent(); + } + + public function callEvent() + { event(new CronMonthly()); } } diff --git a/app/Console/Cron/Nightly.php b/app/Console/Cron/Nightly.php index 368949f5..25fea64c 100644 --- a/app/Console/Cron/Nightly.php +++ b/app/Console/Cron/Nightly.php @@ -2,7 +2,7 @@ namespace App\Console\Cron; -use App\Contracts\Command; +use App\Contracts\CronCommand; use App\Events\CronNightly; /** @@ -11,7 +11,7 @@ use App\Events\CronNightly; * * The actual cron tasks are in app/Cron */ -class Nightly extends Command +class Nightly extends CronCommand { protected $signature = 'cron:nightly'; protected $description = 'Run the nightly cron tasks'; @@ -19,7 +19,11 @@ class Nightly extends Command public function handle(): void { - $this->redirectLoggingToFile('cron'); + $this->callEvent(); + } + + public function callEvent() + { event(new CronNightly()); } } diff --git a/app/Console/Cron/ThirtyMinute.php b/app/Console/Cron/ThirtyMinute.php new file mode 100644 index 00000000..b8940a7d --- /dev/null +++ b/app/Console/Cron/ThirtyMinute.php @@ -0,0 +1,26 @@ +callEvent(); + } + + public function callEvent() + { + event(new CronThirtyMinute()); + } +} diff --git a/app/Console/Cron/Weekly.php b/app/Console/Cron/Weekly.php index 3ae11ca8..9885c125 100644 --- a/app/Console/Cron/Weekly.php +++ b/app/Console/Cron/Weekly.php @@ -2,7 +2,7 @@ namespace App\Console\Cron; -use App\Contracts\Command; +use App\Contracts\CronCommand; use App\Events\CronWeekly; /** @@ -11,7 +11,7 @@ use App\Events\CronWeekly; * * The actual cron tasks are in app/Cron */ -class Weekly extends Command +class Weekly extends CronCommand { protected $signature = 'cron:weekly'; protected $description = 'Run the weekly cron tasks'; @@ -19,7 +19,11 @@ class Weekly extends Command public function handle(): void { - $this->redirectLoggingToFile('cron'); + $this->callEvent(); + } + + public function callEvent() + { event(new CronWeekly()); } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 27c8ba11..3850bdf5 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,10 +2,13 @@ namespace App\Console; +use App\Console\Cron\FifteenMinute; +use App\Console\Cron\FiveMinute; use App\Console\Cron\Hourly; use App\Console\Cron\JobQueue; use App\Console\Cron\Monthly; use App\Console\Cron\Nightly; +use App\Console\Cron\ThirtyMinute; use App\Console\Cron\Weekly; use App\Services\CronService; use Illuminate\Console\Scheduling\Schedule; @@ -33,10 +36,16 @@ class Kernel extends ConsoleKernel ->withoutOverlapping(); } + /* + * NOTE: IF MORE TASKS ARE ADDED, THEY ALSO MUST BE ADDED TO THE CRON.PHP + */ + $schedule->command(FiveMinute::class)->everyFiveMinutes(); + $schedule->command(FifteenMinute::class)->everyFifteenMinutes(); + $schedule->command(ThirtyMinute::class)->everyThirtyMinutes(); $schedule->command(Nightly::class)->dailyAt('01:00'); + $schedule->command(Hourly::class)->hourly(); $schedule->command(Weekly::class)->weeklyOn(0); $schedule->command(Monthly::class)->monthlyOn(1); - $schedule->command(Hourly::class)->hourly(); // When spatie-backups runs /*if (config('backup.backup.enabled', false) === true) { diff --git a/app/Contracts/Command.php b/app/Contracts/Command.php index a042ef8e..5531a008 100644 --- a/app/Contracts/Command.php +++ b/app/Contracts/Command.php @@ -29,6 +29,16 @@ abstract class Command extends \Illuminate\Console\Command }*/ } + /** + * Return the signature of the command + * + * @return string + */ + public function getSignature(): string + { + return $this->signature; + } + /** * Splice the logger and replace the active handlers with the handlers from the * a stack in config/logging.php diff --git a/app/Contracts/CronCommand.php b/app/Contracts/CronCommand.php new file mode 100644 index 00000000..26b58eab --- /dev/null +++ b/app/Contracts/CronCommand.php @@ -0,0 +1,20 @@ +redirectLoggingToFile('cron'); + } +} diff --git a/app/Cron/Hourly/ClearExpiredSimbrief.php b/app/Cron/Hourly/ClearExpiredSimbrief.php index 8a770bb6..321b14e5 100644 --- a/app/Cron/Hourly/ClearExpiredSimbrief.php +++ b/app/Cron/Hourly/ClearExpiredSimbrief.php @@ -20,7 +20,7 @@ class ClearExpiredSimbrief extends Listener } /** - * @param \App\Events\CronNightly $event + * @param CronHourly $event */ public function handle(CronHourly $event): void { diff --git a/app/Events/CronFifteenMinute.php b/app/Events/CronFifteenMinute.php new file mode 100644 index 00000000..d14d2da4 --- /dev/null +++ b/app/Events/CronFifteenMinute.php @@ -0,0 +1,9 @@ +run(); - return response([ - 'content' => $output, + return response()->json([ + 'count' => count($run), + 'tasks' => $run, ]); } } diff --git a/app/Providers/CronServiceProvider.php b/app/Providers/CronServiceProvider.php index 0d97f546..6fc65ad1 100644 --- a/app/Providers/CronServiceProvider.php +++ b/app/Providers/CronServiceProvider.php @@ -12,9 +12,12 @@ use App\Cron\Nightly\PilotLeave; use App\Cron\Nightly\RecalculateBalances; use App\Cron\Nightly\RecalculateStats; use App\Cron\Nightly\SetActiveFlights; +use App\Events\CronFifteenMinute; +use App\Events\CronFiveMinute; use App\Events\CronHourly; use App\Events\CronMonthly; use App\Events\CronNightly; +use App\Events\CronThirtyMinute; use App\Events\CronWeekly; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -24,6 +27,15 @@ use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvi class CronServiceProvider extends ServiceProvider { protected $listen = [ + CronFiveMinute::class => [], + CronFifteenMinute::class => [], + CronThirtyMinute::class => [], + CronHourly::class => [ + DeletePireps::class, + RemoveExpiredBids::class, + RemoveExpiredLiveFlights::class, + ClearExpiredSimbrief::class, + ], CronNightly::class => [ ApplyExpenses::class, RecalculateBalances::class, @@ -32,19 +44,10 @@ class CronServiceProvider extends ServiceProvider RecalculateStats::class, NewVersionCheck::class, ], - CronWeekly::class => [ ], - CronMonthly::class => [ \App\Cron\Monthly\ApplyExpenses::class, ], - - CronHourly::class => [ - DeletePireps::class, - RemoveExpiredBids::class, - RemoveExpiredLiveFlights::class, - ClearExpiredSimbrief::class, - ], ]; } diff --git a/bin/cron b/bin/cron new file mode 100755 index 00000000..8e0d4d79 --- /dev/null +++ b/bin/cron @@ -0,0 +1,35 @@ +#!/usr/bin/env php +make(Kernel::class); + +// Run a null artisan thing just so Laravel internals can be setup properly +$status = $kernel->handle( + $input = new Symfony\Component\Console\Input\ArrayInput([ + 'command' => 'version', + ]), + new Symfony\Component\Console\Output\NullOutput() +); + +/** @var Cron $cron */ +$cron = app(Cron::class); +$run = $cron->run(); + +echo json_encode([ + 'count' => count($run), + 'tasks' => $run, +]);