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

Simplify envelopes #217

Closed
wants to merge 20 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"psr/container": "^1.0|^2.0",
"psr/log": "^2.0|^3.0",
"symfony/console": "^5.4|^6.0",
"yiisoft/arrays": "^3.1",
"yiisoft/definitions": "^1.0|^2.0|^3.0",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/injector": "^1.0"
Expand Down
3 changes: 2 additions & 1 deletion phpbench.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"runner.path": "tests/Benchmark",
"runner.revs": 100000,
"runner.iterations": 5,
"runner.warmup": 5
"runner.warmup": 5,
"runner.retry_threshold": 5
}
74 changes: 74 additions & 0 deletions src/Message/AbstractEnvelope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Message;

use Yiisoft\Arrays\ArrayHelper;

abstract class AbstractEnvelope implements EnvelopeInterface
{
protected array $metadata = [];
private MessageInterface $message;

public function __construct(MessageInterface $message)
{
$this->metadata = $message->getMetadata();
$envelopes = [static::class];
while ($message instanceof EnvelopeInterface) {
if ($message::class !== static::class) {
$envelopes = [$message::class];
}

$message = $message->getMessage();
}
$this->message = $message;

if (isset($this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY]) && is_array($this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY])) {
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = array_merge(
$envelopes,
array_filter(
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY],
static fn (string $envelope): bool => !in_array($envelope, $envelopes),
),
);
} else {
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = [static::class];
}
}

public function getMessage(): MessageInterface
{
return $this->message;
}

public function getHandlerName(): string
{
return $this->message->getHandlerName();
}

public function getData(): mixed
{
return $this->message->getData();
}

public function getMetadata(): array
{
return ArrayHelper::merge(
$this->metadata,
$this->getEnvelopeMetadata(),
);
}

public static function fromData(string $handlerName, mixed $data, array $metadata = []): MessageInterface
{
return static::fromMessage(Message::fromData($handlerName, $data, $metadata));
}

/**
* Metadata of the envelope
*
* @return array
*/
abstract protected function getEnvelopeMetadata(): array;
}
2 changes: 0 additions & 2 deletions src/Message/EnvelopeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ interface EnvelopeInterface extends MessageInterface
public static function fromMessage(MessageInterface $message): self;

public function getMessage(): MessageInterface;

public function withMessage(MessageInterface $message): self;
}
62 changes: 0 additions & 62 deletions src/Message/EnvelopeTrait.php

This file was deleted.

18 changes: 6 additions & 12 deletions src/Message/IdEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,28 @@
/**
* ID envelope allows to identify a message.
*/
final class IdEnvelope implements EnvelopeInterface
final class IdEnvelope extends AbstractEnvelope
{
use EnvelopeTrait;

public const MESSAGE_ID_KEY = 'yii-message-id';

public function __construct(
private MessageInterface $message,
private string|int|null $id = null,
MessageInterface $message,
private readonly string|int|null $id = null,
) {
parent::__construct($message);
}

public static function fromMessage(MessageInterface $message): self
{
return new self($message, $message->getMetadata()[self::MESSAGE_ID_KEY] ?? null);
}

public function setId(string|int|null $id): void
{
$this->id = $id;
}

public function getId(): string|int|null
{
return $this->id ?? $this->message->getMetadata()[self::MESSAGE_ID_KEY] ?? null;
return $this->id ?? $this->metadata[self::MESSAGE_ID_KEY] ?? null;
}

private function getEnvelopeMetadata(): array
protected function getEnvelopeMetadata(): array
{
return [self::MESSAGE_ID_KEY => $this->getId()];
}
Expand Down
10 changes: 1 addition & 9 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
final class Message implements MessageInterface
{
/**
* @param string $handlerName A name of a handler which should handle this message.
* @param mixed $data Message data, encodable by a queue adapter
* @param array $metadata Message metadata, encodable by a queue adapter
* @param string|null $id Message id
*/
public function __construct(
private string $handlerName,
Expand Down Expand Up @@ -37,12 +37,4 @@ public function getMetadata(): array
{
return $this->metadata;
}

public function withMetadata(array $metadata): self
{
$instance = clone $this;
$instance->metadata = $metadata;

return $instance;
}
}
6 changes: 0 additions & 6 deletions src/Message/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ public static function fromData(string $handlerName, mixed $data, array $metadat

/**
* Returns handler name.
*
* @return string
*/
public function getHandlerName(): string;

/**
* Returns payload data.
*
* @return mixed
*/
public function getData(): mixed;

/**
* Returns message metadata: timings, attempts count, metrics, etc.
*
* @return array
*/
public function getMetadata(): array;
}
20 changes: 8 additions & 12 deletions src/Middleware/FailureHandling/FailureEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,28 @@

namespace Yiisoft\Queue\Middleware\FailureHandling;

use Yiisoft\Queue\Message\EnvelopeInterface;
use Yiisoft\Queue\Message\EnvelopeTrait;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Queue\Message\AbstractEnvelope;
use Yiisoft\Queue\Message\MessageInterface;

final class FailureEnvelope implements EnvelopeInterface
final class FailureEnvelope extends AbstractEnvelope
{
use EnvelopeTrait;

public const FAILURE_META_KEY = 'failure-meta';

public function __construct(
private MessageInterface $message,
private array $meta = [],
MessageInterface $message,
private readonly array $failureMeta = [],
) {
parent::__construct($message);
}

public static function fromMessage(MessageInterface $message): self
{
return new self($message, $message->getMetadata()[self::FAILURE_META_KEY] ?? []);
}

public function getMetadata(): array
protected function getEnvelopeMetadata(): array
{
$meta = $this->message->getMetadata();
$meta[self::FAILURE_META_KEY] = array_merge($meta[self::FAILURE_META_KEY] ?? [], $this->meta);

return $meta;
return [self::FAILURE_META_KEY => ArrayHelper::merge($this->metadata[self::FAILURE_META_KEY] ?? [], $this->failureMeta)];
}
}
6 changes: 1 addition & 5 deletions tests/Unit/EnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public function testEnvelopeDuplicates(): void

$stack = $message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY];
$this->assertIsArray($stack);
$this->assertEquals([
IdEnvelope::class,
IdEnvelope::class,
IdEnvelope::class,
], $stack);
$this->assertEquals([IdEnvelope::class], $stack);

$this->assertEquals('test-id-3', $message->getMetadata()[IdEnvelope::MESSAGE_ID_KEY]);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/Message/JsonMessageSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public function testUnserializeWithMetadata(): void

$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertEquals($payload['data'], $message->getData());
$this->assertEquals(['int' => 1, 'str' => 'string', 'bool' => true, EnvelopeInterface::ENVELOPE_STACK_KEY => []], $message->getMetadata());
$this->assertEquals(
['int' => 1, 'str' => 'string', 'bool' => true, EnvelopeInterface::ENVELOPE_STACK_KEY => []],
$message->getMetadata()
);
}

public function testUnserializeEnvelopeStack(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SendAgainMiddlewareTest extends TestCase
public static function queueSendingStrategyProvider(): array
{
return [
/*[
[
SendAgainMiddleware::class,
true,
[],
Expand All @@ -42,7 +42,7 @@ public static function queueSendingStrategyProvider(): array
true,
[SendAgainMiddleware::META_KEY_RESEND . '-' => 1],
[SendAgainMiddleware::META_KEY_RESEND . '-' => 2],
],*/
],
[
SendAgainMiddleware::class,
false,
Expand Down Expand Up @@ -151,9 +151,6 @@ public function testQueueSendingStrategies(
$this->expectExceptionMessage('testException');
}

$metaInitial = [FailureEnvelope::FAILURE_META_KEY => $metaInitial];
$metaResult = [FailureEnvelope::FAILURE_META_KEY => $metaResult];

$handler = $this->getHandler($metaResult, $suites);
$queue = $this->getPreparedQueue($metaResult, $suites);

Expand All @@ -162,7 +159,7 @@ public function testQueueSendingStrategies(
new Message(
'test',
null,
$metaInitial
[FailureEnvelope::FAILURE_META_KEY => $metaInitial],
),
new Exception('testException'),
$queue
Expand Down Expand Up @@ -194,7 +191,7 @@ private function getHandler(array $metaResult, bool $suites): MessageFailureHand
$pipelineAssertion = static function (FailureHandlingRequest $request) use (
$metaResult
): FailureHandlingRequest {
Assert::assertEquals($metaResult, $request->getMessage()->getMetadata());
Assert::assertEquals($metaResult, $request->getMessage()->getMetadata()[FailureEnvelope::FAILURE_META_KEY] ?? []);

throw $request->getException();
};
Expand All @@ -209,7 +206,7 @@ private function getHandler(array $metaResult, bool $suites): MessageFailureHand
private function getPreparedQueue(array $metaResult, bool $suites): QueueInterface
{
$queueAssertion = static function (MessageInterface $message) use ($metaResult): MessageInterface {
Assert::assertEquals($metaResult, $message->getMetadata());
Assert::assertEquals($metaResult, $message->getMetadata()[FailureEnvelope::FAILURE_META_KEY] ?? []);

return $message;
};
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public function testAdapterNotConfiguredExceptionForRun(): void

public function testRunWithSignalLoop(): void
{
if (!extension_loaded('pcntl')) {
self::markTestSkipped('PCNTL support required');
}

$this->loop = new SignalLoop();
$queue = $this
->getQueue()
Expand Down
Loading