|
@@ -14,12 +14,14 @@ use Symfony\Component\Console\Command\Command;
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
+use Symfony\Component\Lock\Factory as Locker;
|
|
|
use Zend\I18n\Translator\TranslatorInterface;
|
|
|
use function sprintf;
|
|
|
|
|
|
class ProcessVisitsCommand extends Command
|
|
|
{
|
|
|
public const NAME = 'visit:process';
|
|
|
+ private const LOCK_NAME = 'visit_process';
|
|
|
|
|
|
/**
|
|
|
* @var VisitServiceInterface
|
|
@@ -33,69 +35,99 @@ class ProcessVisitsCommand extends Command
|
|
|
* @var TranslatorInterface
|
|
|
*/
|
|
|
private $translator;
|
|
|
+ /**
|
|
|
+ * @var Locker
|
|
|
+ */
|
|
|
+ private $locker;
|
|
|
+ /**
|
|
|
+ * @var OutputInterface
|
|
|
+ */
|
|
|
+ private $output;
|
|
|
|
|
|
public function __construct(
|
|
|
VisitServiceInterface $visitService,
|
|
|
IpLocationResolverInterface $ipLocationResolver,
|
|
|
+ Locker $locker,
|
|
|
TranslatorInterface $translator
|
|
|
) {
|
|
|
$this->visitService = $visitService;
|
|
|
$this->ipLocationResolver = $ipLocationResolver;
|
|
|
$this->translator = $translator;
|
|
|
+ $this->locker = $locker;
|
|
|
parent::__construct();
|
|
|
}
|
|
|
|
|
|
protected function configure(): void
|
|
|
{
|
|
|
- $this->setName(self::NAME)
|
|
|
- ->setDescription(
|
|
|
- $this->translator->translate('Processes visits where location is not set yet')
|
|
|
- );
|
|
|
+ $this
|
|
|
+ ->setName(self::NAME)
|
|
|
+ ->setDescription($this->translator->translate('Processes visits where location is not set yet'));
|
|
|
}
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void
|
|
|
{
|
|
|
- $this->visitService->locateVisits(function (Visit $visit) use ($output) {
|
|
|
- if (! $visit->hasRemoteAddr()) {
|
|
|
- $output->writeln(
|
|
|
- sprintf('<comment>%s</comment>', $this->translator->translate('Ignored visit with no IP address')),
|
|
|
- OutputInterface::VERBOSITY_VERBOSE
|
|
|
- );
|
|
|
- throw new IpCannotBeLocatedException('Ignored visit with no IP address');
|
|
|
- }
|
|
|
+ $this->output = $output;
|
|
|
+ $io = new SymfonyStyle($input, $output);
|
|
|
|
|
|
- $ipAddr = $visit->getRemoteAddr();
|
|
|
- $output->write(sprintf('%s <fg=blue>%s</>', $this->translator->translate('Processing IP'), $ipAddr));
|
|
|
- if ($ipAddr === IpAddress::LOCALHOST) {
|
|
|
- $output->writeln(
|
|
|
- sprintf(' [<comment>%s</comment>]', $this->translator->translate('Ignored localhost address'))
|
|
|
- );
|
|
|
- throw new IpCannotBeLocatedException('Ignored localhost address');
|
|
|
- }
|
|
|
+ $lock = $this->locker->createLock(self::LOCK_NAME);
|
|
|
+ if (! $lock->acquire()) {
|
|
|
+ $io->warning(sprintf(
|
|
|
+ $this->translator->translate('There is already an instance of the "%s" command in execution'),
|
|
|
+ self::NAME
|
|
|
+ ));
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- try {
|
|
|
- return $this->ipLocationResolver->resolveIpLocation($ipAddr);
|
|
|
- } catch (WrongIpException $e) {
|
|
|
- $output->writeln(
|
|
|
- sprintf(
|
|
|
- ' [<fg=red>%s</>]',
|
|
|
- $this->translator->translate('An error occurred while locating IP. Skipped')
|
|
|
- )
|
|
|
- );
|
|
|
- if ($output->isVerbose()) {
|
|
|
- $this->getApplication()->renderException($e, $output);
|
|
|
+ try {
|
|
|
+ $this->visitService->locateVisits(
|
|
|
+ [$this, 'getGeolocationDataForVisit'],
|
|
|
+ function (VisitLocation $location) use ($output) {
|
|
|
+ $output->writeln(sprintf(
|
|
|
+ ' [<info>' . $this->translator->translate('Address located at "%s"') . '</info>]',
|
|
|
+ $location->getCountryName()
|
|
|
+ ));
|
|
|
}
|
|
|
+ );
|
|
|
|
|
|
- throw new IpCannotBeLocatedException('An error occurred while locating IP', 0, $e);
|
|
|
+ $io->success($this->translator->translate('Finished processing all IPs'));
|
|
|
+ } finally {
|
|
|
+ $lock->release();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getGeolocationDataForVisit(Visit $visit): array
|
|
|
+ {
|
|
|
+ if (! $visit->hasRemoteAddr()) {
|
|
|
+ $this->output->writeln(sprintf(
|
|
|
+ '<comment>%s</comment>',
|
|
|
+ $this->translator->translate('Ignored visit with no IP address')
|
|
|
+ ), OutputInterface::VERBOSITY_VERBOSE);
|
|
|
+ throw new IpCannotBeLocatedException('Ignored visit with no IP address');
|
|
|
+ }
|
|
|
+
|
|
|
+ $ipAddr = $visit->getRemoteAddr();
|
|
|
+ $this->output->write(sprintf('%s <fg=blue>%s</>', $this->translator->translate('Processing IP'), $ipAddr));
|
|
|
+ if ($ipAddr === IpAddress::LOCALHOST) {
|
|
|
+ $this->output->writeln(
|
|
|
+ sprintf(' [<comment>%s</comment>]', $this->translator->translate('Ignored localhost address'))
|
|
|
+ );
|
|
|
+ throw new IpCannotBeLocatedException('Ignored localhost address');
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return $this->ipLocationResolver->resolveIpLocation($ipAddr);
|
|
|
+ } catch (WrongIpException $e) {
|
|
|
+ $this->output->writeln(
|
|
|
+ sprintf(
|
|
|
+ ' [<fg=red>%s</>]',
|
|
|
+ $this->translator->translate('An error occurred while locating IP. Skipped')
|
|
|
+ )
|
|
|
+ );
|
|
|
+ if ($this->output->isVerbose()) {
|
|
|
+ $this->getApplication()->renderException($e, $this->output);
|
|
|
}
|
|
|
- }, function (VisitLocation $location) use ($output) {
|
|
|
- $output->writeln(sprintf(
|
|
|
- ' [<info>' . $this->translator->translate('Address located at "%s"') . '</info>]',
|
|
|
- $location->getCountryName()
|
|
|
- ));
|
|
|
- });
|
|
|
|
|
|
- $io = new SymfonyStyle($input, $output);
|
|
|
- $io->success($this->translator->translate('Finished processing all IPs'));
|
|
|
+ throw new IpCannotBeLocatedException('An error occurred while locating IP', 0, $e);
|
|
|
+ }
|
|
|
}
|
|
|
}
|