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

Extract getId / setId method from the MessageInterface to a new one #183

Closed
wants to merge 1 commit 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
5 changes: 4 additions & 1 deletion src/Adapter/SynchronousAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Message\MessageInterface;
use Yiisoft\Yii\Queue\Message\ParametrizedMessageInterface;
use Yiisoft\Yii\Queue\QueueFactory;
use Yiisoft\Yii\Queue\QueueInterface;
use Yiisoft\Yii\Queue\Worker\WorkerInterface;
Expand Down Expand Up @@ -66,7 +67,9 @@ public function push(MessageInterface $message): void
$key = count($this->messages) + $this->current;
$this->messages[] = $message;

$message->setId((string) $key);
if ($message instanceof ParametrizedMessageInterface) {
$message->setId((string) $key);
}
}

public function subscribe(callable $handlerCallback): void
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/JobFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use RuntimeException;
use Throwable;
use Yiisoft\Yii\Queue\Message\MessageInterface;
use Yiisoft\Yii\Queue\Message\ParametrizedMessageInterface;

class JobFailureException extends RuntimeException
{
public function __construct(private MessageInterface $queueMessage, Throwable $previous)
{
$error = $previous->getMessage();
$messageId = $queueMessage->getId() ?? 'null';
$messageId = $queueMessage instanceof ParametrizedMessageInterface ? ($queueMessage->getId() ?? 'null') : 'null';
$messageText = "Processing of message #$messageId is stopped because of an exception:\n$error.";

parent::__construct($messageText, 0, $previous);
Expand Down
17 changes: 5 additions & 12 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Yiisoft\Yii\Queue\Message;

final class Message implements MessageInterface
final class Message implements ParametrizedMessageInterface
{
use ParametrizedMessageTrait;

/**
* @param mixed $data Message data, encodable by a queue adapter
* @param array $metadata Message metadata, encodable by a queue adapter
Expand All @@ -15,8 +17,9 @@ public function __construct(
private string $handlerName,
private mixed $data,
private array $metadata = [],
private ?string $id = null
?string $id = null
) {
$this->setId($id);
}

public function getHandlerName(): string
Expand All @@ -29,16 +32,6 @@ public function getData(): mixed
return $this->data;
}

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

public function getId(): ?string
{
return $this->id;
}

public function getMetadata(): array
{
return $this->metadata;
Expand Down
9 changes: 0 additions & 9 deletions src/Message/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@

interface MessageInterface
{
public function setId(?string $id): void;

/**
* Returns unique message ID.
*
* @return string|null
*/
public function getId(): ?string;

/**
* Returns handler name.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Message/ParametrizedMessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Message;

interface ParametrizedMessageInterface extends MessageInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name is a bit confusing. Parametrized means there are parameters. Here either there aren't or only one. Is there a better name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was temporary named, thought about moving also getMetadata.

Is there a better name?

Any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IdentifyableMessageInterface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or MessageIdInterface? :)

Also it might be closed after introducing envelope

{
public function setId(?string $id): void;

/**
* Returns unique message ID.
*
* @return string|null
*/
public function getId(): ?string;
}
25 changes: 25 additions & 0 deletions src/Message/ParametrizedMessageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Message;

trait ParametrizedMessageTrait
{
protected ?string $id = null;

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

/**
* Returns unique message ID.
*
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}
}
3 changes: 0 additions & 3 deletions src/Middleware/FailureHandling/FailureHandlingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public function __construct(private MessageInterface $message, private Throwable
{
}

/**
* @return MessageInterface
*/
public function getMessage(): MessageInterface
{
return $this->message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Yiisoft\Yii\Queue\Message\Message;
use Yiisoft\Yii\Queue\Message\MessageInterface;
use Yiisoft\Yii\Queue\Message\ParametrizedMessageInterface;
use Yiisoft\Yii\Queue\Middleware\FailureHandling\FailureHandlingRequest;
use Yiisoft\Yii\Queue\Middleware\FailureHandling\MessageFailureHandlerInterface;
use Yiisoft\Yii\Queue\Middleware\FailureHandling\MiddlewareFailureInterface;
Expand Down Expand Up @@ -62,11 +63,12 @@ public function processFailure(
): FailureHandlingRequest {
$message = $request->getMessage();
if ($this->suites($message)) {
$id = $message instanceof ParametrizedMessageInterface ? $message->getId() : null;
$messageNew = new Message(
handlerName: $message->getHandlerName(),
data: $message->getData(),
metadata: $this->formNewMeta($message),
id: $message->getId(),
id: $id,
);
($this->queue ?? $request->getQueue())->push(
$messageNew,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Yiisoft\Yii\Queue\Message\Message;
use Yiisoft\Yii\Queue\Message\MessageInterface;
use Yiisoft\Yii\Queue\Message\ParametrizedMessageInterface;
use Yiisoft\Yii\Queue\Middleware\FailureHandling\FailureHandlingRequest;
use Yiisoft\Yii\Queue\Middleware\FailureHandling\MessageFailureHandlerInterface;
use Yiisoft\Yii\Queue\Middleware\FailureHandling\MiddlewareFailureInterface;
Expand Down Expand Up @@ -40,11 +41,12 @@ public function processFailure(
): FailureHandlingRequest {
$message = $request->getMessage();
if ($this->suites($message)) {
$id = $message instanceof ParametrizedMessageInterface ? $message->getId() : null;
$message = new Message(
handlerName: $message->getHandlerName(),
data: $message->getData(),
metadata: $this->createMeta($message),
id: $message->getId(),
id: $id,
);
$message = $this->queue?->push($message) ?? $request->getQueue()->push($message);

Expand Down
6 changes: 5 additions & 1 deletion src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException;
use Yiisoft\Yii\Queue\Message\MessageInterface;
use Yiisoft\Yii\Queue\Message\ParametrizedMessageInterface;
use Yiisoft\Yii\Queue\Middleware\Push\AdapterPushHandler;
use Yiisoft\Yii\Queue\Middleware\Push\MessageHandlerPushInterface;
use Yiisoft\Yii\Queue\Middleware\Push\MiddlewarePushInterface;
Expand Down Expand Up @@ -59,7 +60,10 @@ public function push(

$this->logger->info(
'Pushed message with handler name "{handlerName}" to the queue. Assigned ID #{id}.',
['handlerName' => $message->getHandlerName(), 'id' => $message->getId() ?? 'null']
[
'handlerName' => $message->getHandlerName(),
'id' => $message instanceof ParametrizedMessageInterface ? ($message->getId() ?? 'null') : 'null',
]
);

return $message;
Expand Down
10 changes: 8 additions & 2 deletions src/Worker/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Yiisoft\Injector\Injector;
use Yiisoft\Yii\Queue\Exception\JobFailureException;
use Yiisoft\Yii\Queue\Message\MessageInterface;
use Yiisoft\Yii\Queue\Message\ParametrizedMessageInterface;
use Yiisoft\Yii\Queue\Middleware\Consume\ConsumeFinalHandler;
use Yiisoft\Yii\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher;
use Yiisoft\Yii\Queue\Middleware\Consume\ConsumeRequest;
Expand All @@ -41,11 +42,16 @@
}

/**
* @throws Throwable
* @throws JobFailureException
* @throws RuntimeException
*/
public function process(MessageInterface $message, QueueInterface $queue): MessageInterface
{
$this->logger->info('Processing message #{message}.', ['message' => $message->getId()]);
if ($message instanceof ParametrizedMessageInterface) {
$this->logger->info('Processing message #{message}.', ['message' => $message->getId()]);
} else {
$this->logger->debug('Processing message with data: {data}.', ['data' => $message->getData()]);

Check warning on line 53 in src/Worker/Worker.php

View check run for this annotation

Codecov / codecov/patch

src/Worker/Worker.php#L53

Added line #L53 was not covered by tests
}

$name = $message->getHandlerName();
$handler = $this->getHandler($name);
Expand Down
Loading