Skip to content

Commit

Permalink
entity service node (#43)
Browse files Browse the repository at this point in the history
* entity service node

* entity service node

* fix data
  • Loading branch information
kuaukutsu authored Dec 17, 2023
1 parent 591673f commit 9185f10
Show file tree
Hide file tree
Showing 45 changed files with 1,008 additions and 128 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ test-builder:
-e XDEBUG_MODE=off \
cli php ./bin/builder.php --task=5

test-pm:
test-even:
docker-compose -f ./docker-compose.yml run --rm -u ${USER} -w /src/tests \
-e XDEBUG_MODE=off \
cli php ./bin/pm.php --heartbeat=5 --iterval=1 --process=5
cli php ./bin/task.even.php --heartbeat=5 --iterval=1 --process=5

test-odd:
docker-compose -f ./docker-compose.yml run --rm -u ${USER} -w /src/tests \
-e XDEBUG_MODE=off \
cli php ./bin/task.odd.php --heartbeat=5 --iterval=1 --process=5
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

## Глоссарий

- **Node** - узел, возможность развести по отдельным сервисам различные обрабатываемые кейсы.
- **Task** - задача, содержит конфигурацию этапов - подзадач, состояние обработки, и результат работы.
- **TaskStage** - структурная единица задачи - этап, подзадача. Описывает полезную работу.
- **TaskManager** - менеджер процессов, контролирует запуск и работу задач.
Expand All @@ -13,6 +14,7 @@

Общий процесс можно описать через три основные компоненты:

- узел (**EntityNode**)
- задача (**EntityTask**)
- менеджер процессов (**TaskManager**)
- обработчик этапов (**TaskHandler**)
Expand Down
36 changes: 36 additions & 0 deletions src/EntityNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace kuaukutsu\poc\task;

use Psr\Container\ContainerExceptionInterface;
use kuaukutsu\poc\task\service\StageCommand;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\service\TaskCommand;
use kuaukutsu\poc\task\service\TaskQuery;

interface EntityNode
{
public function label(): string;

/**
* @throws ContainerExceptionInterface
*/
public function getTaskQuery(): TaskQuery;

/**
* @throws ContainerExceptionInterface
*/
public function getTaskCommand(): TaskCommand;

/**
* @throws ContainerExceptionInterface
*/
public function getStageQuery(): StageQuery;

/**
* @throws ContainerExceptionInterface
*/
public function getStageCommand(): StageCommand;
}
6 changes: 3 additions & 3 deletions src/TaskBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class TaskBuilder
{
public function __construct(private readonly TaskCreator $factory)
public function __construct(private readonly TaskCreator $creator)
{
}

Expand All @@ -29,7 +29,7 @@ public function create(string $title, EntityWrapper ...$stages): TaskDraft
public function build(TaskDraft $draft, ?TaskStageContext $context = null): EntityTask
{
return $context === null
? $this->factory->create($draft)
: $this->factory->createFromContext($draft, $context);
? $this->creator->create($draft)
: $this->creator->createFromContext($draft, $context);
}
}
50 changes: 35 additions & 15 deletions src/service/TaskCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use kuaukutsu\poc\task\dto\TaskModelCreate;
use kuaukutsu\poc\task\dto\TaskModel;
use kuaukutsu\poc\task\exception\BuilderException;
use kuaukutsu\poc\task\exception\NotFoundException;
use kuaukutsu\poc\task\state\TaskStateReady;
use kuaukutsu\poc\task\state\TaskStateRelation;
use kuaukutsu\poc\task\handler\TaskFactory;
Expand All @@ -25,7 +26,6 @@ public function __construct(
private readonly TaskCommand $taskCommand,
private readonly StageCommand $stageCommand,
private readonly TaskFactory $factory,
private readonly TaskDestroyer $destroyer,
) {
}

Expand Down Expand Up @@ -67,37 +67,44 @@ public function createFromContext(TaskDraft $draft, TaskStageContext $context):
*/
private function save(TaskDraft $draft): TaskModel
{
$task = $this->taskCommand->create(
new EntityUuid($draft->getUuid()),
new TaskModelCreate(
title: $draft->getTitle(),
flag: $draft->getFlag(),
state: serialize($draft->getState()),
options: $draft->getOptions(),
checksum: $draft->getChecksum(),
)
);
$uuid = new EntityUuid($draft->getUuid());

$stageState = new TaskStateReady();
$stageStateSerialize = serialize($stageState);

try {
$order = 0;
foreach ($draft->getStages() as $stage) {
$this->stageCommand->create(
new EntityUuid(),
new StageModelCreate(
taskUuid: $task->uuid,
taskUuid: $draft->getUuid(),
flag: $stageState->getFlag()->toValue(),
state: serialize($stageState),
state: $stageStateSerialize,
handler: serialize($stage),
order: ++$order,
)
);
}
} catch (Exception $exception) {
$this->destroyer->purge(
new EntityUuid($task->uuid)
$this->purge($uuid);

throw new BuilderException("[{$draft->getTitle()}] TaskBuilder failed.", $exception);
}

try {
$task = $this->taskCommand->create(
$uuid,
new TaskModelCreate(
title: $draft->getTitle(),
flag: $draft->getFlag(),
state: serialize($draft->getState()),
options: $draft->getOptions(),
checksum: $draft->getChecksum(),
)
);
} catch (Exception $exception) {
$this->purge($uuid);

throw new BuilderException("[{$draft->getTitle()}] TaskBuilder failed.", $exception);
}
Expand All @@ -122,4 +129,17 @@ private function validateDraft(TaskDraft $draft): void
);
}
}

private function purge(EntityUuid $uuid): void
{
try {
$this->stageCommand->removeByTask($uuid);
} catch (NotFoundException) {
}

try {
$this->taskCommand->remove($uuid);
} catch (NotFoundException) {
}
}
}
40 changes: 40 additions & 0 deletions src/tools/NodeServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace kuaukutsu\poc\task\tools;

use DI\FactoryInterface;
use Psr\Container\ContainerExceptionInterface;
use kuaukutsu\poc\task\EntityNode;

/**
* @template T
*/
final class NodeServiceFactory
{
public function __construct(private readonly FactoryInterface $factory)
{
}

/**
* @param class-string<T> $serviceName
* @return T
* @throws ContainerExceptionInterface
*/
public function factory(EntityNode $node, string $serviceName)
{
/**
* @var T
*/
return $this->factory->make(
$serviceName,
[
'taskQuery' => $node->getTaskQuery(),
'taskCommand' => $node->getTaskCommand(),
'stageQuery' => $node->getStageQuery(),
'stageCommand' => $node->getStageCommand(),
]
);
}
}
16 changes: 8 additions & 8 deletions tests/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\service\TaskCommand;
use kuaukutsu\poc\task\service\TaskQuery;
use kuaukutsu\poc\task\tests\service\StageCommandStub;
use kuaukutsu\poc\task\tests\service\StageQueryStub;
use kuaukutsu\poc\task\tests\service\TaskCommandStub;
use kuaukutsu\poc\task\tests\service\TaskQueryStub;
use kuaukutsu\poc\task\tests\nodes\test\TestStageCommand;
use kuaukutsu\poc\task\tests\nodes\test\TestStageQuery;
use kuaukutsu\poc\task\tests\nodes\test\TestTaskCommand;
use kuaukutsu\poc\task\tests\nodes\test\TestTaskQuery;

use function DI\autowire;
use function DI\create;
Expand All @@ -40,10 +40,10 @@ private static function get(string $id)
if (self::$container === null) {
self::$container = new \DI\Container(
[
TaskQuery::class => autowire(TaskQueryStub::class),
TaskCommand::class => autowire(TaskCommandStub::class),
StageQuery::class => autowire(StageQueryStub::class),
StageCommand::class => autowire(StageCommandStub::class),
TaskQuery::class => autowire(TestTaskQuery::class),
TaskCommand::class => autowire(TestTaskCommand::class),
StageQuery::class => autowire(TestStageQuery::class),
StageCommand::class => autowire(TestStageCommand::class),
OutputInterface::class => create(NullOutput::class),
ConsoleOutputInterface::class => autowire(NullConsoleOutput::class),
]
Expand Down
8 changes: 4 additions & 4 deletions tests/TaskFinallyHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace kuaukutsu\poc\task\tests;

use kuaukutsu\poc\task\state\TaskStateMessage;
use kuaukutsu\poc\task\state\TaskStateSuccess;
use kuaukutsu\poc\task\tests\service\BaseStorage;
use kuaukutsu\poc\task\tests\stub\TestExceptionFinally;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use kuaukutsu\poc\task\handler\TaskFinallyHandler;
use kuaukutsu\poc\task\service\TaskDestroyer;
use kuaukutsu\poc\task\state\TaskStateMessage;
use kuaukutsu\poc\task\state\TaskStateSuccess;
use kuaukutsu\poc\task\EntityTask;
use kuaukutsu\poc\task\EntityUuid;
use kuaukutsu\poc\task\EntityWrapper;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\BaseStorage;
use kuaukutsu\poc\task\tests\stub\TestExceptionFinally;
use kuaukutsu\poc\task\tests\stub\TestFinally;
use kuaukutsu\poc\task\tests\stub\TestStageStub;

Expand Down
27 changes: 27 additions & 0 deletions tests/bin/bootstrap.even.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use kuaukutsu\poc\task\service\StageCommand;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\service\TaskCommand;
use kuaukutsu\poc\task\service\TaskQuery;
use kuaukutsu\poc\task\tests\nodes\even\EvenStageCommand;
use kuaukutsu\poc\task\tests\nodes\even\EvenStageQuery;
use kuaukutsu\poc\task\tests\nodes\even\EvenTaskCommand;
use kuaukutsu\poc\task\tests\nodes\even\EvenTaskQuery;

use function DI\autowire;
use function DI\create;

require_once dirname(__DIR__, 2) . '/vendor/autoload.php';

$definitions = [
TaskQuery::class => autowire(EvenTaskQuery::class),
TaskCommand::class => autowire(EvenTaskCommand::class),
StageQuery::class => autowire(EvenStageQuery::class),
StageCommand::class => autowire(EvenStageCommand::class),
ConsoleOutputInterface::class => create(ConsoleOutput::class),
];
27 changes: 27 additions & 0 deletions tests/bin/bootstrap.odd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use kuaukutsu\poc\task\service\StageCommand;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\service\TaskCommand;
use kuaukutsu\poc\task\service\TaskQuery;
use kuaukutsu\poc\task\tests\nodes\odd\OddStageCommand;
use kuaukutsu\poc\task\tests\nodes\odd\OddStageQuery;
use kuaukutsu\poc\task\tests\nodes\odd\OddTaskCommand;
use kuaukutsu\poc\task\tests\nodes\odd\OddTaskQuery;

use function DI\autowire;
use function DI\create;

require_once dirname(__DIR__, 2) . '/vendor/autoload.php';

$definitions = [
TaskQuery::class => autowire(OddTaskQuery::class),
TaskCommand::class => autowire(OddTaskCommand::class),
StageQuery::class => autowire(OddStageQuery::class),
StageCommand::class => autowire(OddStageCommand::class),
ConsoleOutputInterface::class => create(ConsoleOutput::class),
];
13 changes: 0 additions & 13 deletions tests/bin/bootstrap.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,11 @@

use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use kuaukutsu\poc\task\service\StageCommand;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\service\TaskCommand;
use kuaukutsu\poc\task\service\TaskQuery;
use kuaukutsu\poc\task\tests\service\StageCommandStub;
use kuaukutsu\poc\task\tests\service\StageQueryStub;
use kuaukutsu\poc\task\tests\service\TaskCommandStub;
use kuaukutsu\poc\task\tests\service\TaskQueryStub;

use function DI\autowire;
use function DI\create;

require_once dirname(__DIR__, 2) . '/vendor/autoload.php';

$definitions = [
TaskQuery::class => autowire(TaskQueryStub::class),
TaskCommand::class => autowire(TaskCommandStub::class),
StageQuery::class => autowire(StageQueryStub::class),
StageCommand::class => autowire(StageCommandStub::class),
ConsoleOutputInterface::class => create(ConsoleOutput::class),
];
21 changes: 19 additions & 2 deletions tests/bin/builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
declare(strict_types=1);

use DI\Container;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\EntityWrapper;
use kuaukutsu\poc\task\service\TaskCreator;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\nodes\EvenNode;
use kuaukutsu\poc\task\tests\nodes\OddNode;
use kuaukutsu\poc\task\tests\stub\IncreaseNumberStageStub;
use kuaukutsu\poc\task\tests\stub\NumberHandlerStageStub;
use kuaukutsu\poc\task\tests\stub\NumberSaveStageStub;
use kuaukutsu\poc\task\tools\NodeServiceFactory;

use function kuaukutsu\poc\task\tools\argument;

Expand All @@ -22,12 +26,25 @@
$container = new Container($definitions);

/** @noinspection PhpUnhandledExceptionInspection */
$builder = $container->get(TaskBuilder::class);
$serviceEvenCreator = $container->get(NodeServiceFactory::class)
->factory($container->get(EvenNode::class), TaskCreator::class);

/** @noinspection PhpUnhandledExceptionInspection */
$serviceOddCreator = $container->get(NodeServiceFactory::class)
->factory($container->get(OddNode::class), TaskCreator::class);

/** @noinspection PhpUnhandledExceptionInspection */
$builderEven = $container->make(TaskBuilder::class, ['creator' => $serviceEvenCreator]);

/** @noinspection PhpUnhandledExceptionInspection */
$builderOdd = $container->make(TaskBuilder::class, ['creator' => $serviceOddCreator]);

$taskCount = (int)argument('task', 4);
while ($taskCount > 0) {
$taskCount--;

$builder = ($taskCount % 2) === 0 ? $builderEven : $builderOdd;

$builder->build(
$builder->create(
$taskCount . ' date: ' . date('c'),
Expand Down
Loading

0 comments on commit 9185f10

Please sign in to comment.