Log console exceptions and nonzero exit codes.
This commit is contained in:
parent
44a3ef15e6
commit
a12b655cb4
|
@ -5,9 +5,12 @@ ini_set('display_errors', 1);
|
|||
|
||||
$autoloader = require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
$app = App\AppFactory::create($autoloader, [
|
||||
App\Environment::BASE_DIR => dirname(__DIR__),
|
||||
]);
|
||||
$app = App\AppFactory::create(
|
||||
$autoloader,
|
||||
[
|
||||
App\Environment::BASE_DIR => dirname(__DIR__),
|
||||
]
|
||||
);
|
||||
|
||||
$di = $app->getContainer();
|
||||
|
||||
|
|
|
@ -48,7 +48,9 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
|
||||
$migrationConfigurations = $buildMigrationConfigurationsEvent->getMigrationConfigurations();
|
||||
|
||||
$migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray($migrationConfigurations);
|
||||
$migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray(
|
||||
$migrationConfigurations
|
||||
);
|
||||
|
||||
$migrateFactory = Doctrine\Migrations\DependencyFactory::fromEntityManager(
|
||||
$migrateConfig,
|
||||
|
@ -113,13 +115,19 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
);
|
||||
|
||||
// Build default menus
|
||||
$dispatcher->addListener(App\Event\BuildAdminMenu::class, function (App\Event\BuildAdminMenu $e) {
|
||||
call_user_func(include(__DIR__ . '/menus/admin.php'), $e);
|
||||
});
|
||||
$dispatcher->addListener(
|
||||
App\Event\BuildAdminMenu::class,
|
||||
function (App\Event\BuildAdminMenu $e) {
|
||||
call_user_func(include(__DIR__ . '/menus/admin.php'), $e);
|
||||
}
|
||||
);
|
||||
|
||||
$dispatcher->addListener(App\Event\BuildStationMenu::class, function (App\Event\BuildStationMenu $e) {
|
||||
call_user_func(include(__DIR__ . '/menus/station.php'), $e);
|
||||
});
|
||||
$dispatcher->addListener(
|
||||
App\Event\BuildStationMenu::class,
|
||||
function (App\Event\BuildStationMenu $e) {
|
||||
call_user_func(include(__DIR__ . '/menus/station.php'), $e);
|
||||
}
|
||||
);
|
||||
|
||||
// Other event subscribers from across the application.
|
||||
$dispatcher->addCallableListener(
|
||||
|
@ -144,13 +152,15 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
App\Notification\Check\SyncTaskCheck::class
|
||||
);
|
||||
|
||||
$dispatcher->addServiceSubscriber([
|
||||
App\Radio\AutoDJ\Queue::class,
|
||||
App\Radio\AutoDJ\Annotations::class,
|
||||
App\Radio\Backend\Liquidsoap\ConfigWriter::class,
|
||||
App\Sync\Task\NowPlayingTask::class,
|
||||
App\Webhook\Dispatcher::class,
|
||||
App\Controller\Api\NowplayingController::class,
|
||||
]);
|
||||
|
||||
$dispatcher->addServiceSubscriber(
|
||||
[
|
||||
App\Console\ErrorHandler::class,
|
||||
App\Radio\AutoDJ\Queue::class,
|
||||
App\Radio\AutoDJ\Annotations::class,
|
||||
App\Radio\Backend\Liquidsoap\ConfigWriter::class,
|
||||
App\Sync\Task\NowPlayingTask::class,
|
||||
App\Webhook\Dispatcher::class,
|
||||
App\Controller\Api\NowplayingController::class,
|
||||
]
|
||||
);
|
||||
};
|
||||
|
|
|
@ -216,6 +216,7 @@ return [
|
|||
// Console
|
||||
App\Console\Application::class => function (DI\Container $di, App\EventDispatcher $dispatcher) {
|
||||
$console = new App\Console\Application('Command Line Interface', '1.0.0', $di);
|
||||
$console->setDispatcher($dispatcher);
|
||||
|
||||
// Trigger an event for the core app and all plugins to build their CLI commands.
|
||||
$event = new App\Event\BuildConsoleCommands($console);
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\ConsoleEvents;
|
||||
use Symfony\Component\Console\Event\ConsoleErrorEvent;
|
||||
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
class ErrorHandler implements EventSubscriberInterface
|
||||
{
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(LoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
ConsoleEvents::TERMINATE => [
|
||||
'onTerminate',
|
||||
],
|
||||
ConsoleEvents::ERROR => [
|
||||
'onError',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function onTerminate(ConsoleTerminateEvent $event): void
|
||||
{
|
||||
$command = $event->getCommand();
|
||||
|
||||
$exitCode = $event->getExitCode();
|
||||
if (0 === $exitCode) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = sprintf(
|
||||
'Console command `%s` exited with error code %d.',
|
||||
$command->getName(),
|
||||
$exitCode
|
||||
);
|
||||
$this->logger->warning($message);
|
||||
}
|
||||
|
||||
public function onError(ConsoleErrorEvent $event): void
|
||||
{
|
||||
$command = $event->getCommand();
|
||||
$exception = $event->getError();
|
||||
|
||||
$message = sprintf(
|
||||
'%s: %s (uncaught exception) at %s line %s while running console command `%s`',
|
||||
get_class($exception),
|
||||
$exception->getMessage(),
|
||||
$exception->getFile(),
|
||||
$exception->getLine(),
|
||||
$command->getName()
|
||||
);
|
||||
|
||||
$this->logger->error($message, ['exception' => $exception]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue