diff --git a/CHANGELOG.md b/CHANGELOG.md index 3771f8042..b549a7116 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## Unreleased + +- **Other Features** + - [spiral/queue] Added `Spiral\Queue\TaskInterface` and `Spiral\Queue\Task` which will contain all the necessary data + for job processing. + ## 3.12.0 - 2024-02-29 - **Medium Impact Changes** diff --git a/src/Queue/src/Task.php b/src/Queue/src/Task.php new file mode 100644 index 000000000..438f3ae6f --- /dev/null +++ b/src/Queue/src/Task.php @@ -0,0 +1,61 @@ +> $headers headers of the task/job. + */ + public function __construct( + private readonly string $id, + private readonly string $queue, + private readonly string $name, + private readonly mixed $payload, + private readonly array $headers, + ) { + } + + public function getPayload(): mixed + { + return $this->payload; + } + + /** + * @return non-empty-string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return array> + */ + public function getHeaders(): array + { + return $this->headers; + } + + /** + * @return non-empty-string + */ + public function getQueue(): string + { + return $this->queue; + } + + /** + * @return non-empty-string + */ + public function getId(): string + { + return $this->id; + } +} diff --git a/src/Queue/src/TaskInterface.php b/src/Queue/src/TaskInterface.php new file mode 100644 index 000000000..12e216433 --- /dev/null +++ b/src/Queue/src/TaskInterface.php @@ -0,0 +1,41 @@ +> + */ + public function getHeaders(): array; + + /** + * Returns the name of the queue. + * + * @return non-empty-string + */ + public function getQueue(): string; + + /** + * Returns the unique identifier of the task in the queue. + * + * @return non-empty-string + */ + public function getId(): string; +} diff --git a/src/Queue/tests/TaskTest.php b/src/Queue/tests/TaskTest.php new file mode 100644 index 000000000..2837dfece --- /dev/null +++ b/src/Queue/tests/TaskTest.php @@ -0,0 +1,43 @@ + 'value'], ['header' => ['value']]); + $this->assertSame(['key' => 'value'], $task->getPayload()); + + $task = new Task('some-id', 'some-queue', 'some-name', 'string-payload', ['header' => ['value']]); + $this->assertSame('string-payload', $task->getPayload()); + } + + public function testGetName(): void + { + $task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]); + $this->assertSame('some-name', $task->getName()); + } + + public function testGetHeaders(): void + { + $task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]); + $this->assertSame(['header' => ['value']], $task->getHeaders()); + } + + public function testGetQueue(): void + { + $task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]); + $this->assertSame('some-queue', $task->getQueue()); + } + + public function testGetId(): void + { + $task = new Task('some-id', 'some-queue', 'some-name', ['key' => 'value'], ['header' => ['value']]); + $this->assertSame('some-id', $task->getId()); + } +}