first commit
This commit is contained in:
81
bundles/External/PompoBundle/Command/StockSynchronizationCommand.php
vendored
Normal file
81
bundles/External/PompoBundle/Command/StockSynchronizationCommand.php
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace External\PompoBundle\Command;
|
||||
|
||||
use External\PompoBundle\DataGo\Synchronizer\StockSynchronizer as DataGoStockSynchronizer;
|
||||
use External\PompoBundle\DRS\Synchronizer\StockSynchronizer as DRSStockSynchronizer;
|
||||
use External\PompoBundle\Util\Configuration;
|
||||
use External\PompoBundle\Util\PompoUtil;
|
||||
use KupShop\KupShopBundle\Util\Logging\SentryLogger;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class StockSynchronizationCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'pompo:sync_stock';
|
||||
|
||||
/** @required */
|
||||
public Configuration $configuration;
|
||||
/** @required */
|
||||
public PompoUtil $pompoUtil;
|
||||
/** @required */
|
||||
public SentryLogger $sentryLogger;
|
||||
|
||||
/**
|
||||
* Sklad se synchronizuje v samostatnem procesu, aby to bylo rychly, nic ho nebrzdilo a byl furt aktualni.
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$interval = 60;
|
||||
|
||||
while (true) {
|
||||
sqlGetConnection()->connect();
|
||||
|
||||
$timeStart = microtime(true);
|
||||
|
||||
// DRS sklad synchronizujeme pouze na pompu
|
||||
if ($this->configuration->isPompo()) {
|
||||
// Sesynchronizuju DRS sklad - sklad prodejen
|
||||
$this->synchronize(function () use ($output) {
|
||||
$output->writeln('Synchronizing DRS stock...');
|
||||
$this->pompoUtil->synchronizeDRS([DRSStockSynchronizer::getType()]);
|
||||
$output->writeln('DRS Stock changes synchronized');
|
||||
}, $output);
|
||||
}
|
||||
|
||||
// Sesynchronizuju DataGo sklad - hlavni sklad
|
||||
$this->synchronize(function () use ($output) {
|
||||
$output->writeln('Synchronizing DataGo stock...');
|
||||
$this->pompoUtil->synchronizeDataGo([DataGoStockSynchronizer::getType()]);
|
||||
$output->writeln('DataGo Stock changes synchronized');
|
||||
}, $output);
|
||||
|
||||
$executionTime = (int) (microtime(true) - $timeStart);
|
||||
|
||||
sqlGetConnection()->close();
|
||||
|
||||
$sleep = max((int) ($interval - $executionTime), 0);
|
||||
|
||||
$output->writeln(sprintf('Sleeping for %s seconds...', $sleep));
|
||||
|
||||
sleep($sleep);
|
||||
}
|
||||
}
|
||||
|
||||
private function synchronize(callable $callable, OutputInterface $output): void
|
||||
{
|
||||
try {
|
||||
$callable();
|
||||
} catch (\Throwable $e) {
|
||||
if (isDevelopment()) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->sentryLogger->captureException($e);
|
||||
$output->writeln(sprintf('Some error occurred during dispatch: %s', $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user