Skip to content

Commit

Permalink
State transfer (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
liquetsoft authored Oct 6, 2024
1 parent a0929f6 commit 75d93d2
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 51 deletions.
7 changes: 5 additions & 2 deletions Command/InstallFromFolderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->note("Installing full version of FIAS from '{$folder}' folder");
$start = microtime(true);

$state = new ArrayState();
$state = $state->setAndLockParameter(StateParameter::PATH_TO_EXTRACT_FOLDER, $folder);
$state = new ArrayState(
[
StateParameter::PATH_TO_EXTRACT_FOLDER->value => $folder,
]
);
$this->pipeline->run($state);

$total = round(microtime(true) - $start, 4);
Expand Down
27 changes: 10 additions & 17 deletions Command/InstallParallelRunningCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
namespace Liquetsoft\Fias\Symfony\LiquetsoftFiasBundle\Command;

use Liquetsoft\Fias\Component\Pipeline\Pipe\Pipe;
use Liquetsoft\Fias\Component\Pipeline\State\ArrayState;
use Liquetsoft\Fias\Component\Pipeline\State\StateParameter;
use Liquetsoft\Fias\Component\Pipeline\State\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* Команда для параллельных процессов, в которых идет установка ФИАС.
Expand All @@ -19,8 +18,10 @@
*/
final class InstallParallelRunningCommand extends Command
{
public function __construct(private readonly Pipe $pipeline)
{
public function __construct(
private readonly Pipe $pipeline,
private readonly SerializerInterface $serializer,
) {
parent::__construct();
}

Expand All @@ -32,7 +33,6 @@ protected function configure(): void
$this
->setName('liquetsoft:fias:install_parallel_running')
->setDescription('Command for running one single thread of installation process')
->addArgument('files', InputArgument::OPTIONAL, 'Json encoded list of files to process')
;
}

Expand All @@ -41,20 +41,13 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$files = $input->getArgument('files');
if (\is_array($files)) {
$files = reset($files);
$stdIn = file_get_contents('php://stdin');
if ($stdIn === false || $stdIn === '') {
return 1;
}

if ($files !== null && $files !== '') {
$files = json_decode((string) $files, true);
} else {
$stdIn = file_get_contents('php://stdin');
$files = $stdIn !== false ? json_decode($stdIn, true) : [];
}
$state = $this->serializer->deserialize($stdIn, State::class, 'json');

$state = new ArrayState();
$state->setAndLockParameter(StateParameter::FILES_TO_PROCEED, $files);
$this->pipeline->run($state);

return 0;
Expand Down
7 changes: 5 additions & 2 deletions Command/UpdateFromFolderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$io->note("Updating FIAS from the '{$folder}' folder");

$state = new ArrayState();
$state = $state->setAndLockParameter(StateParameter::PATH_TO_EXTRACT_FOLDER, $folder);
$state = new ArrayState(
[
StateParameter::PATH_TO_EXTRACT_FOLDER->value => $folder,
]
);
$this->pipeline->run($state);

$io->success("FIAS updated from the '{$folder}' folder");
Expand Down
27 changes: 10 additions & 17 deletions Command/UpdateParallelRunningCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
namespace Liquetsoft\Fias\Symfony\LiquetsoftFiasBundle\Command;

use Liquetsoft\Fias\Component\Pipeline\Pipe\Pipe;
use Liquetsoft\Fias\Component\Pipeline\State\ArrayState;
use Liquetsoft\Fias\Component\Pipeline\State\StateParameter;
use Liquetsoft\Fias\Component\Pipeline\State\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* Команда для параллельных процессов, в которых идет обновление ФИАС.
Expand All @@ -19,8 +18,10 @@
*/
final class UpdateParallelRunningCommand extends Command
{
public function __construct(private readonly Pipe $pipeline)
{
public function __construct(
private readonly Pipe $pipeline,
private readonly SerializerInterface $serializer,
) {
parent::__construct();
}

Expand All @@ -32,7 +33,6 @@ protected function configure(): void
$this
->setName('liquetsoft:fias:update_parallel_running')
->setDescription('Command for running one single thread of updating process')
->addArgument('files', InputArgument::OPTIONAL, 'Json encoded list of files to process')
;
}

Expand All @@ -41,20 +41,13 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$files = $input->getArgument('files');
if (\is_array($files)) {
$files = reset($files);
$stdIn = file_get_contents('php://stdin');
if ($stdIn === false || $stdIn === '') {
return 1;
}

if ($files !== null && $files !== '') {
$files = json_decode((string) $files, true);
} else {
$stdIn = file_get_contents('php://stdin');
$files = $stdIn !== false ? json_decode($stdIn, true) : [];
}
$state = $this->serializer->deserialize($stdIn, State::class, 'json');

$state = new ArrayState();
$state->setAndLockParameter(StateParameter::FILES_TO_PROCEED, $files);
$this->pipeline->run($state);

return 0;
Expand Down
12 changes: 12 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ services:
tags:
- { name: 'serializer.normalizer' }

liquetsoft_fias.serializer.pipeline_state_normalizer:
class: Liquetsoft\Fias\Component\Serializer\FiasPipelineStateNormalizer
tags:
- { name: 'serializer.normalizer' }

liquetsoft_fias.serializer.pipeline_state_denormalizer:
class: Liquetsoft\Fias\Component\Serializer\FiasPipelineStateDenormalizer
tags:
- { name: 'serializer.normalizer' }

liquetsoft_fias.serializer.compiled_denormalizer:
class: Liquetsoft\Fias\Symfony\LiquetsoftFiasBundle\Serializer\CompiledEntitesDenormalizer
tags:
Expand Down Expand Up @@ -326,6 +336,7 @@ services:
class: Liquetsoft\Fias\Symfony\LiquetsoftFiasBundle\Command\InstallParallelRunningCommand
arguments:
- '@liquetsoft_fias.pipe.install_parallel_running'
- '@serializer'
tags:
- { name: 'console.command' }

Expand Down Expand Up @@ -354,6 +365,7 @@ services:
class: Liquetsoft\Fias\Symfony\LiquetsoftFiasBundle\Command\UpdateParallelRunningCommand
arguments:
- '@liquetsoft_fias.pipe.update_parallel_running'
- '@serializer'
tags:
- { name: 'console.command' }

Expand Down
4 changes: 4 additions & 0 deletions Serializer/FiasSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Liquetsoft\Fias\Symfony\LiquetsoftFiasBundle\Serializer;

use Liquetsoft\Fias\Component\Serializer\FiasNameConverter;
use Liquetsoft\Fias\Component\Serializer\FiasPipelineStateDenormalizer;
use Liquetsoft\Fias\Component\Serializer\FiasPipelineStateNormalizer;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
Expand All @@ -31,6 +33,8 @@ public function __construct(?array $normalizers = null, ?array $encoders = null)
$normalizers = [
new CompiledEntitesDenormalizer(),
new FiasUuidNormalizer(),
new FiasPipelineStateNormalizer(),
new FiasPipelineStateDenormalizer(),
new DateTimeNormalizer(),
new ObjectNormalizer(
nameConverter: new FiasNameConverter(),
Expand Down
15 changes: 9 additions & 6 deletions Tests/Pipeline/InstallPipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ public function testInstall(): void
$versionEntity->setFullurl($versionFullUrl);
$versionEntity->setDeltaurl($versionDeltaUrl);

$state = new ArrayState();
$state->setAndLockParameter(StateParameter::PATH_TO_DOWNLOAD_FILE, $testArchive);
$state->setAndLockParameter(StateParameter::PATH_TO_EXTRACT_FOLDER, $testDir);
$state->setAndLockParameter(StateParameter::FIAS_NEXT_VERSION_NUMBER, $version);
$state->setAndLockParameter(StateParameter::FIAS_NEXT_VERSION_FULL_URL, $versionFullUrl);
$state->setAndLockParameter(StateParameter::FIAS_NEXT_VERSION_DELTA_URL, $versionDeltaUrl);
$state = new ArrayState(
[
StateParameter::PATH_TO_DOWNLOAD_FILE->value => $testArchive,
StateParameter::PATH_TO_EXTRACT_FOLDER->value => $testDir,
StateParameter::FIAS_NEXT_VERSION_NUMBER->value => $version,
StateParameter::FIAS_NEXT_VERSION_FULL_URL->value => $versionFullUrl,
StateParameter::FIAS_NEXT_VERSION_DELTA_URL->value => $versionDeltaUrl,
]
);

$pipeline = $this->createPipeLine();
$pipeline->run($state);
Expand Down
15 changes: 9 additions & 6 deletions Tests/Pipeline/UpdatePipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ public function testUpdate(): void
$versionEntity->setFullurl($versionFullUrl);
$versionEntity->setDeltaurl($versionDeltaUrl);

$state = new ArrayState();
$state->setAndLockParameter(StateParameter::PATH_TO_DOWNLOAD_FILE, $testArchive);
$state->setAndLockParameter(StateParameter::PATH_TO_EXTRACT_FOLDER, $testDir);
$state->setAndLockParameter(StateParameter::FIAS_NEXT_VERSION_NUMBER, $version);
$state->setAndLockParameter(StateParameter::FIAS_NEXT_VERSION_FULL_URL, $versionFullUrl);
$state->setAndLockParameter(StateParameter::FIAS_NEXT_VERSION_DELTA_URL, $versionDeltaUrl);
$state = new ArrayState(
[
StateParameter::PATH_TO_DOWNLOAD_FILE->value => $testArchive,
StateParameter::PATH_TO_EXTRACT_FOLDER->value => $testDir,
StateParameter::FIAS_NEXT_VERSION_NUMBER->value => $version,
StateParameter::FIAS_NEXT_VERSION_FULL_URL->value => $versionFullUrl,
StateParameter::FIAS_NEXT_VERSION_DELTA_URL->value => $versionDeltaUrl,
]
);

$pipeline = $this->createPipeLine();
$pipeline->run($state);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"php": ">=8.2",
"liquetsoft/fias-component": "^13.1",
"liquetsoft/fias-component": "dev-master",
"symfony/uid": "^5.0|^6.0|^7.0",
"symfony/framework-bundle": "^5.0|^6.0|^7.0",
"symfony/http-client": "^5.0|^6.0|^7.0",
Expand Down

0 comments on commit 75d93d2

Please sign in to comment.