From 7467beb72f6063252ccaa4ebdfb3ac7829be956f Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Thu, 27 Jul 2023 13:47:56 +0100 Subject: [PATCH] fix(regression): avoid overriding laravel schedule command to store last run Signed-off-by: Sami Mazouz --- .../src/Console/ConsoleServiceProvider.php | 19 +++++++++--- framework/core/src/Console/Server.php | 25 +++++++++++++-- .../Foundation/Console/ScheduleRunCommand.php | 31 ------------------- 3 files changed, 38 insertions(+), 37 deletions(-) delete mode 100644 framework/core/src/Foundation/Console/ScheduleRunCommand.php diff --git a/framework/core/src/Console/ConsoleServiceProvider.php b/framework/core/src/Console/ConsoleServiceProvider.php index 09ef2e366..7f60c38dc 100644 --- a/framework/core/src/Console/ConsoleServiceProvider.php +++ b/framework/core/src/Console/ConsoleServiceProvider.php @@ -9,6 +9,7 @@ namespace Flarum\Console; +use Carbon\Carbon; use Flarum\Console\Cache\Factory; use Flarum\Database\Console\MigrateCommand; use Flarum\Database\Console\ResetCommand; @@ -17,14 +18,17 @@ use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\Console\AssetsPublishCommand; use Flarum\Foundation\Console\CacheClearCommand; use Flarum\Foundation\Console\InfoCommand; -use Flarum\Foundation\Console\ScheduleRunCommand; +use Flarum\Settings\SettingsRepositoryInterface; +use Illuminate\Console\Events\CommandFinished; use Illuminate\Console\Scheduling\CacheEventMutex; use Illuminate\Console\Scheduling\CacheSchedulingMutex; use Illuminate\Console\Scheduling\EventMutex; use Illuminate\Console\Scheduling\Schedule as LaravelSchedule; use Illuminate\Console\Scheduling\ScheduleListCommand; +use Illuminate\Console\Scheduling\ScheduleRunCommand; use Illuminate\Console\Scheduling\SchedulingMutex; use Illuminate\Contracts\Container\Container; +use Illuminate\Events\Dispatcher; class ConsoleServiceProvider extends AbstractServiceProvider { @@ -73,13 +77,20 @@ class ConsoleServiceProvider extends AbstractServiceProvider }); } - public function boot(Container $container): void + public function boot(Container $container, Dispatcher $events, LaravelSchedule $schedule): void { - $schedule = $container->make(LaravelSchedule::class); - foreach ($container->make('flarum.console.scheduled') as $scheduled) { $event = $schedule->command($scheduled['command'], $scheduled['args']); $scheduled['callback']($event); } + + $events->listen(CommandFinished::class, function (CommandFinished $event) use ($container) { + $command = $event->command; + $settings = $container->make(SettingsRepositoryInterface::class); + + if ($command === ScheduleRunCommand::getDefaultName()) { + $settings->set('schedule.last_run', Carbon::now()); + } + }); } } diff --git a/framework/core/src/Console/Server.php b/framework/core/src/Console/Server.php index 4c90d04a2..a9afed30d 100644 --- a/framework/core/src/Console/Server.php +++ b/framework/core/src/Console/Server.php @@ -12,10 +12,15 @@ namespace Flarum\Console; use Flarum\Foundation\ErrorHandling\Registry; use Flarum\Foundation\ErrorHandling\Reporter; use Flarum\Foundation\SiteInterface; +use Illuminate\Console\Events\CommandFinished; +use Illuminate\Console\Events\CommandStarting; use Illuminate\Container\Container; +use Illuminate\Events\Dispatcher; use Symfony\Component\Console\Application; use Symfony\Component\Console\ConsoleEvents; +use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Event\ConsoleErrorEvent; +use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Symfony\Component\EventDispatcher\EventDispatcher; class Server @@ -35,15 +40,31 @@ class Server $console->add($command); } - $this->handleErrors($console); + $this->handleEvents($console, $app->getContainer()); exit($console->run()); } - private function handleErrors(Application $console): void + private function handleEvents(Application $console, Container $container): void { $dispatcher = new EventDispatcher(); + $dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) use ($container) { + $events = $container->make(Dispatcher::class); + + $events->dispatch( + new CommandStarting($event->getCommand()->getName(), $event->getInput(), $event->getOutput()) + ); + }); + + $dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) use ($container) { + $events = $container->make(Dispatcher::class); + + $events->dispatch( + new CommandFinished($event->getCommand()->getName(), $event->getInput(), $event->getOutput(), $event->getExitCode()) + ); + }); + $dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) { $container = Container::getInstance(); diff --git a/framework/core/src/Foundation/Console/ScheduleRunCommand.php b/framework/core/src/Foundation/Console/ScheduleRunCommand.php deleted file mode 100644 index 23403d47e..000000000 --- a/framework/core/src/Foundation/Console/ScheduleRunCommand.php +++ /dev/null @@ -1,31 +0,0 @@ -settings->set('schedule.last_run', $this->startedAt); - } -}