mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added support to ignore errors in UpdateDbCommand
This commit is contained in:
parent
3734160cb4
commit
34ff831473
@ -9,9 +9,12 @@ use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
|||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Helper\ProgressBar;
|
use Symfony\Component\Console\Helper\ProgressBar;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
class UpdateDbCommand extends Command
|
class UpdateDbCommand extends Command
|
||||||
{
|
{
|
||||||
public const NAME = 'visit:update-db';
|
public const NAME = 'visit:update-db';
|
||||||
@ -33,6 +36,12 @@ class UpdateDbCommand extends Command
|
|||||||
->setHelp(
|
->setHelp(
|
||||||
'The GeoLite2 database is updated first Tuesday every month, so this command should be ideally run '
|
'The GeoLite2 database is updated first Tuesday every month, so this command should be ideally run '
|
||||||
. 'every first Wednesday'
|
. 'every first Wednesday'
|
||||||
|
)
|
||||||
|
->addOption(
|
||||||
|
'ignoreErrors',
|
||||||
|
'i',
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'Makes the command success even iof the update fails.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,19 +58,32 @@ class UpdateDbCommand extends Command
|
|||||||
});
|
});
|
||||||
|
|
||||||
$progressBar->finish();
|
$progressBar->finish();
|
||||||
$io->writeln('');
|
$io->newLine();
|
||||||
|
|
||||||
$io->success('GeoLite2 database properly updated');
|
$io->success('GeoLite2 database properly updated');
|
||||||
return ExitCodes::EXIT_SUCCESS;
|
return ExitCodes::EXIT_SUCCESS;
|
||||||
} catch (RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$progressBar->finish();
|
$progressBar->finish();
|
||||||
$io->writeln('');
|
$io->newLine();
|
||||||
|
|
||||||
$io->error('An error occurred while updating GeoLite2 database');
|
return $this->handleError($e, $io, $input);
|
||||||
if ($io->isVerbose()) {
|
|
||||||
$this->getApplication()->renderException($e, $output);
|
|
||||||
}
|
|
||||||
return ExitCodes::EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function handleError(RuntimeException $e, SymfonyStyle $io, InputInterface $input): int
|
||||||
|
{
|
||||||
|
$ignoreErrors = $input->getOption('ignoreErrors');
|
||||||
|
$baseErrorMsg = 'An error occurred while updating GeoLite2 database';
|
||||||
|
|
||||||
|
if ($ignoreErrors) {
|
||||||
|
$io->warning(sprintf('%s, but it was ignored', $baseErrorMsg));
|
||||||
|
return ExitCodes::EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
$io->error($baseErrorMsg);
|
||||||
|
if ($io->isVerbose()) {
|
||||||
|
$this->getApplication()->renderException($e, $io);
|
||||||
|
}
|
||||||
|
return ExitCodes::EXIT_FAILURE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase;
|
|||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Shlinkio\Shlink\CLI\Command\Visit\UpdateDbCommand;
|
use Shlinkio\Shlink\CLI\Command\Visit\UpdateDbCommand;
|
||||||
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
||||||
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
@ -31,27 +32,45 @@ class UpdateDbCommandTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function successMessageIsPrintedIfEverythingWorks()
|
public function successMessageIsPrintedIfEverythingWorks(): void
|
||||||
{
|
{
|
||||||
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->will(function () {
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->will(function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
$exitCode = $this->commandTester->getStatusCode();
|
||||||
|
|
||||||
$this->assertStringContainsString('GeoLite2 database properly updated', $output);
|
$this->assertStringContainsString('GeoLite2 database properly updated', $output);
|
||||||
|
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
|
||||||
$download->shouldHaveBeenCalledOnce();
|
$download->shouldHaveBeenCalledOnce();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function errorMessageIsPrintedIfAnExceptionIsThrown()
|
public function errorMessageIsPrintedIfAnExceptionIsThrown(): void
|
||||||
{
|
{
|
||||||
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
$exitCode = $this->commandTester->getStatusCode();
|
||||||
|
|
||||||
$this->assertStringContainsString('An error occurred while updating GeoLite2 database', $output);
|
$this->assertStringContainsString('An error occurred while updating GeoLite2 database', $output);
|
||||||
|
$this->assertEquals(ExitCodes::EXIT_FAILURE, $exitCode);
|
||||||
|
$download->shouldHaveBeenCalledOnce();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function warningMessageIsPrintedIfAnExceptionIsThrownAndErrorsAreIgnored(): void
|
||||||
|
{
|
||||||
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
|
||||||
|
|
||||||
|
$this->commandTester->execute(['--ignoreErrors' => true]);
|
||||||
|
$output = $this->commandTester->getDisplay();
|
||||||
|
$exitCode = $this->commandTester->getStatusCode();
|
||||||
|
|
||||||
|
$this->assertStringContainsString('ignored', $output);
|
||||||
|
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
|
||||||
$download->shouldHaveBeenCalledOnce();
|
$download->shouldHaveBeenCalledOnce();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user