Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the state management #9

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

public function __construct(
private readonly StateManager $manager,
private readonly string $jobCode,
private readonly string $stepCode,
private readonly string $stepLabel,
) {
}

Expand Down Expand Up @@ -52,11 +52,11 @@
$this->manager->teardown($this);
}

public function toArray(): array

Check failure on line 55 in src/State.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Kiboko\Component\Flow\RabbitMQ\State::toArray() return type has no value type specified in iterable type array.
{
return [
'code' => $this->stepCode,
'label' => $this->stepLabel ?: $this->stepCode,
'jobCode' => $this->jobCode,
'stepCode' => $this->stepCode,
'metrics' => iterator_to_array($this->walkMetrics()),
];
}
Expand Down
52 changes: 49 additions & 3 deletions src/StateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

class StateManager
{
private array $steps = [];

Check failure on line 13 in src/StateManager.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Kiboko\Component\Flow\RabbitMQ\StateManager::$steps type has no value type specified in iterable type array.
private array $tearedDown = [];

Check failure on line 14 in src/StateManager.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Kiboko\Component\Flow\RabbitMQ\StateManager::$tearedDown type has no value type specified in iterable type array.
private int $messageCount = 0;
private int $lineCount = 0;
private readonly Channel $channel;
Expand All @@ -38,14 +38,59 @@
$this->channel->close();
}

public static function withoutAuthentication(
string $host,
string $vhost,
string $topic,
?string $exchange = null,
?int $port = null,
?int $lineThreshold = 1000,
): self {
$connection = new Client([
'host' => $host,
'port' => $port,
'vhost' => $vhost,
'user' => 'guest',
'password' => 'guest',
]);
$connection->connect();

return new self(connection: $connection, topic: $topic, lineThreshold: $lineThreshold, exchange: $exchange);
}

public static function withAuthentication(
string $host,
string $vhost,
string $topic,
string $user,
string $password,
?string $exchange = null,
?int $port = null,
?int $lineThreshold = 1000,
): self {
$connection = new Client([
'host' => $host,
'port' => $port,
'vhost' => $vhost,
'user' => $user,
'password' => $password,
]);
$connection->connect();

return new self(connection: $connection, topic: $topic, lineThreshold: $lineThreshold, exchange: $exchange);
}


public function stepState(
string $jobCode,
string $stepCode,
string $stepLabel,
): State {
return $this->steps[] = new State($this, $stepCode, $stepLabel);
$this->steps[] = $state = new State($this, $jobCode, $stepCode);

return $state;
}

public function trySend($count): void

Check failure on line 93 in src/StateManager.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Kiboko\Component\Flow\RabbitMQ\StateManager::trySend() has parameter $count with no type specified.
{
$this->lineCount += $count;

Expand Down Expand Up @@ -75,11 +120,12 @@
$this->channel->publish(
json_encode([
'messageNumber' => ++$this->messageCount,
'id' => Uuid::uuid4(),
'execution' => getenv('EXECUTION_ID'),
'date' => ['date' => $date->format('c'), 'tz' => $date->getTimezone()->getName()],
'stepsUpdates' => array_map(fn (State $step) => $step->toArray(), $this->steps),
], \JSON_THROW_ON_ERROR),
[
'type' => 'update',
'content-type' => 'application/json',
],
$this->exchange,
Expand Down
Loading