From c6fdd8a59f109c69ddd9218bc19b1a0ced5e003d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 23 Jul 2019 16:36:56 +0200 Subject: [PATCH] Improvements and ensured LocateVisitsCommand does not swallow exceptions --- config/autoload/logger.local.php.dist | 36 +++++++++++++++---- data/infra/swoole.Dockerfile | 4 ++- .../src/Command/Visit/LocateVisitsCommand.php | 14 ++++++-- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/config/autoload/logger.local.php.dist b/config/autoload/logger.local.php.dist index 47203ed0..59feeb7b 100644 --- a/config/autoload/logger.local.php.dist +++ b/config/autoload/logger.local.php.dist @@ -1,16 +1,40 @@ [ - 'handlers' => [ - 'shlink_rotating_handler' => [ - 'level' => Logger::DEBUG, - ], +// For swoole, send logs to standard output +$logger = $isSwoole ? [ + 'handlers' => [ + 'shlink_rotating_handler' => [ + 'level' => Logger::EMERGENCY, // This basically disables regular file logs + ], + 'shlink_stdout_handler' => [ + 'class' => StreamHandler::class, + 'level' => Logger::INFO, + 'stream' => 'php://stdout', + 'formatter' => 'dashed', ], ], + 'loggers' => [ + 'Shlink' => [ + 'handlers' => ['shlink_stdout_handler'], + ], + ], +] : [ + 'handlers' => [ + 'shlink_rotating_handler' => [ + 'level' => Logger::DEBUG, + ], + ], +]; + +return [ + + 'logger' => $logger, + ]; diff --git a/data/infra/swoole.Dockerfile b/data/infra/swoole.Dockerfile index d3d2eae8..87c16064 100644 --- a/data/infra/swoole.Dockerfile +++ b/data/infra/swoole.Dockerfile @@ -84,7 +84,9 @@ WORKDIR /home/shlink # Expose swoole port EXPOSE 8080 -CMD /usr/local/bin/composer update && \ +CMD \ + # Install dependencies if the vendor dir does not exist + if [[ ! -d "./vendor" ]]; then /usr/local/bin/composer install ; fi && \ # When restarting the container, swoole might think it is already in execution # This forces the app to be started every second until the exit code is 0 until php ./vendor/bin/zend-expressive-swoole start; do sleep 1 ; done diff --git a/module/CLI/src/Command/Visit/LocateVisitsCommand.php b/module/CLI/src/Command/Visit/LocateVisitsCommand.php index fa458413..ef1f0298 100644 --- a/module/CLI/src/Command/Visit/LocateVisitsCommand.php +++ b/module/CLI/src/Command/Visit/LocateVisitsCommand.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\Visit; +use Exception; use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface; @@ -78,8 +79,8 @@ class LocateVisitsCommand extends Command $this->visitService->locateUnlocatedVisits( [$this, 'getGeolocationDataForVisit'], - function (VisitLocation $location) use ($output) { - if (! $location->isEmpty()) { + static function (VisitLocation $location) use ($output) { + if (!$location->isEmpty()) { $output->writeln( sprintf(' [Address located at "%s"]', $location->getCountryName()) ); @@ -88,9 +89,16 @@ class LocateVisitsCommand extends Command ); $this->io->success('Finished processing all IPs'); + return ExitCodes::EXIT_SUCCESS; + } catch (Exception $e) { + $this->io->error($e->getMessage()); + if ($this->io->isVerbose()) { + $this->getApplication()->renderException($e, $this->io); + } + + return ExitCodes::EXIT_FAILURE; } finally { $lock->release(); - return ExitCodes::EXIT_SUCCESS; } }