diff --git a/src/State.php b/src/State.php index 9f82696..a8f1933 100644 --- a/src/State.php +++ b/src/State.php @@ -14,8 +14,8 @@ final class State implements StateInterface public function __construct( private readonly StateManager $manager, + private readonly string $jobCode, private readonly string $stepCode, - private readonly string $stepLabel, ) { } @@ -55,8 +55,8 @@ public function teardown(): void public function toArray(): array { return [ - 'code' => $this->stepCode, - 'label' => $this->stepLabel ?: $this->stepCode, + 'jobCode' => $this->jobCode, + 'stepCode' => $this->stepCode, 'metrics' => iterator_to_array($this->walkMetrics()), ]; } diff --git a/src/StateManager.php b/src/StateManager.php index fe04630..81938e8 100644 --- a/src/StateManager.php +++ b/src/StateManager.php @@ -38,11 +38,56 @@ public function __destruct() $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 @@ -75,11 +120,12 @@ private function sendUpdate(): void $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,