This commit is contained in:
2021-02-26 00:59:52 +03:00
committed by GitHub
parent 9cefa847a9
commit e670cb161c
54 changed files with 1410 additions and 1280 deletions

82
src/Message.php Normal file
View File

@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace Nsq;
use Amp\Promise;
use Nsq\Exception\MessageException;
use function Amp\call;
final class Message
{
private bool $processed = false;
public function __construct(
public string $id,
public string $body,
public int $timestamp,
public int $attempts,
private ConsumerInterface $consumer,
) {
}
public static function compose(Frame\Message $message, ConsumerInterface $consumer): self
{
return new self(
$message->id,
$message->body,
$message->timestamp,
$message->attempts,
$consumer,
);
}
/**
* @return Promise<void>
*/
public function finish(): Promise
{
return call(function (): \Generator {
if ($this->processed) {
throw MessageException::processed($this);
}
yield $this->consumer->fin($this->id);
$this->processed = true;
});
}
/**
* @return Promise<void>
*/
public function requeue(int $timeout): Promise
{
return call(function () use ($timeout): \Generator {
if ($this->processed) {
throw MessageException::processed($this);
}
yield $this->consumer->req($this->id, $timeout);
$this->processed = true;
});
}
/**
* @return Promise<void>
*/
public function touch(): Promise
{
return call(function (): \Generator {
if ($this->processed) {
throw MessageException::processed($this);
}
yield $this->consumer->touch($this->id);
$this->processed = true;
});
}
}