1
0
mirror of https://github.com/flarum/core.git synced 2025-08-05 16:07:34 +02:00

fix(regression): avoid overriding laravel schedule command to store last run

Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
This commit is contained in:
Sami Mazouz
2023-07-27 13:47:56 +01:00
parent 6ec0911cd1
commit 7467beb72f
3 changed files with 38 additions and 37 deletions

View File

@@ -9,6 +9,7 @@
namespace Flarum\Console; namespace Flarum\Console;
use Carbon\Carbon;
use Flarum\Console\Cache\Factory; use Flarum\Console\Cache\Factory;
use Flarum\Database\Console\MigrateCommand; use Flarum\Database\Console\MigrateCommand;
use Flarum\Database\Console\ResetCommand; use Flarum\Database\Console\ResetCommand;
@@ -17,14 +18,17 @@ use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Console\AssetsPublishCommand; use Flarum\Foundation\Console\AssetsPublishCommand;
use Flarum\Foundation\Console\CacheClearCommand; use Flarum\Foundation\Console\CacheClearCommand;
use Flarum\Foundation\Console\InfoCommand; 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\CacheEventMutex;
use Illuminate\Console\Scheduling\CacheSchedulingMutex; use Illuminate\Console\Scheduling\CacheSchedulingMutex;
use Illuminate\Console\Scheduling\EventMutex; use Illuminate\Console\Scheduling\EventMutex;
use Illuminate\Console\Scheduling\Schedule as LaravelSchedule; use Illuminate\Console\Scheduling\Schedule as LaravelSchedule;
use Illuminate\Console\Scheduling\ScheduleListCommand; use Illuminate\Console\Scheduling\ScheduleListCommand;
use Illuminate\Console\Scheduling\ScheduleRunCommand;
use Illuminate\Console\Scheduling\SchedulingMutex; use Illuminate\Console\Scheduling\SchedulingMutex;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
class ConsoleServiceProvider extends AbstractServiceProvider 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) { foreach ($container->make('flarum.console.scheduled') as $scheduled) {
$event = $schedule->command($scheduled['command'], $scheduled['args']); $event = $schedule->command($scheduled['command'], $scheduled['args']);
$scheduled['callback']($event); $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());
}
});
} }
} }

View File

@@ -12,10 +12,15 @@ namespace Flarum\Console;
use Flarum\Foundation\ErrorHandling\Registry; use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter; use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Foundation\SiteInterface; use Flarum\Foundation\SiteInterface;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent; use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
class Server class Server
@@ -35,15 +40,31 @@ class Server
$console->add($command); $console->add($command);
} }
$this->handleErrors($console); $this->handleEvents($console, $app->getContainer());
exit($console->run()); exit($console->run());
} }
private function handleErrors(Application $console): void private function handleEvents(Application $console, Container $container): void
{ {
$dispatcher = new EventDispatcher(); $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) { $dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
$container = Container::getInstance(); $container = Container::getInstance();

View File

@@ -1,31 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Foundation\Console;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Events\Dispatcher;
class ScheduleRunCommand extends \Illuminate\Console\Scheduling\ScheduleRunCommand
{
public function __construct(
protected SettingsRepositoryInterface $settings
) {
parent::__construct();
}
public function handle(Schedule $schedule, Dispatcher $dispatcher, ExceptionHandler $handler): void
{
parent::handle($schedule, $dispatcher, $handler);
$this->settings->set('schedule.last_run', $this->startedAt);
}
}