From bc87ac2169ba9939e7219873184d3a8cd0bc53bd Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 25 May 2020 11:43:20 +0200 Subject: [PATCH 1/2] Expose more metadata on a job. --- README.md | 11 +++++++++++ src/Queue/DoctrineQueue.php | 27 +++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4e8b94a..bfa16cc 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,17 @@ return array( ) ); ``` + +### Getting slm/queue-doctrine specific data + +The `DoctrineQueue` will populate all jobs with some additional metadata about the job. This metadata is stored under +a specific key name. + +```php +$job->getMetadata(DoctrineQueue::METADATA_SCHEDULED_KEY); // Returns `\DateTimeImmutable` date for which job is scheduled. +$job->getMetadata(DoctrineQueue::METADATA_PRIORITY_KEY); // Returns integer priority of the job. +$job->getMetadata(DoctrineQueue::METADATA_ID_KEY); // Returns integer ID of the job in the database. +``` Provided Worker Strategies -------------------------- diff --git a/src/Queue/DoctrineQueue.php b/src/Queue/DoctrineQueue.php index 6a88e3b..99dc79e 100644 --- a/src/Queue/DoctrineQueue.php +++ b/src/Queue/DoctrineQueue.php @@ -31,6 +31,15 @@ class DoctrineQueue extends AbstractQueue implements DoctrineQueueInterface public const DEFAULT_PRIORITY = 1024; + // A metadata field is populated with a date (`\DateTimeImmutable`) under this key. + public const METADATA_SCHEDULED_KEY = '__scheduled__'; + + // A metadata field is populated with the priority (`int`) under this key. + public const METADATA_PRIORITY_KEY = '__priority__'; + + // A metadata field is populated with the ID of this job in the database (`int`) under this key. + public const METADATA_ID_KEY = '__id__'; + /** * Options for this queue * @@ -193,8 +202,7 @@ public function pop(array $options = []): ?JobInterface return null; } - // Add job ID to meta data - return $this->unserializeJob($row['data'], ['__id__' => $row['id']]); + return $this->unserializeJobFromRow($row); } /** @@ -313,8 +321,7 @@ public function peek(int $id): JobInterface throw new JobNotFoundException(sprintf("Job with id '%s' does not exists.", $id)); } - // Add job ID to meta data - return $this->unserializeJob($row['data'], ['__id__' => $row['id']]); + return $this->unserializeJobFromRow($row); } /** @@ -456,4 +463,16 @@ protected function purge(): void ); } } + + private function unserializeJobFromRow(array $row): JobInterface + { + return $this->unserializeJob( + $row['data'], + [ + self::METADATA_ID_KEY => $row['id'], + self::METADATA_SCHEDULED_KEY => new \DateTimeImmutable($row['scheduled']), + self::METADATA_PRIORITY_KEY => $row['priority'], + ] + ); + } } From 282a05ea8e2594d73a9f951320df85eb7ead5dc7 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Thu, 4 Jun 2020 16:28:25 +0200 Subject: [PATCH 2/2] Fix tests. --- tests/Queue/DoctrineQueueTest.php | 40 +++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/tests/Queue/DoctrineQueueTest.php b/tests/Queue/DoctrineQueueTest.php index a7e03b0..ac37cf8 100644 --- a/tests/Queue/DoctrineQueueTest.php +++ b/tests/Queue/DoctrineQueueTest.php @@ -79,7 +79,12 @@ public function testPushPop(): void $poppedJob = $this->queue->pop(); - static::assertEquals($job, $poppedJob); + $this->assertCount(4, $poppedJob->getMetadata()); + $this->assertEquals('SlmQueueDoctrineTest\Asset\SimpleJob', $poppedJob->getMetadata('__name__')); + $this->assertEquals('1', $poppedJob->getMetadata('__id__')); + $this->assertTrue(new \DateTime('-10 seconds') < $poppedJob->getMetadata('__scheduled__')); + $this->assertTrue($poppedJob->getMetadata('__scheduled__') < new \DateTime('+10 seconds')); + $this->assertEquals('1024', $poppedJob->getMetadata('__priority__')); } public function testPopHighestPriority(): void @@ -99,9 +104,29 @@ public function testPopHighestPriority(): void 'priority' => 20, ]); - static::assertEquals($jobB, $this->queue->pop()); - static::assertEquals($jobA, $this->queue->pop()); - static::assertEquals($jobC, $this->queue->pop()); + // Job B + $this->assertCount(4, $jobB->getMetadata()); + $this->assertEquals('SlmQueueDoctrineTest\Asset\SimpleJob', $jobB->getMetadata('__name__')); + $this->assertEquals('2', $jobB->getMetadata('__id__')); + $this->assertTrue(new \DateTime('-10 seconds') < $jobB->getMetadata('__scheduled__')); + $this->assertTrue($jobB->getMetadata('__scheduled__') < new \DateTime('+10 seconds')); + $this->assertEquals('5', $jobB->getMetadata('__priority__')); + + // Job A + $this->assertCount(4, $jobA->getMetadata()); + $this->assertEquals('SlmQueueDoctrineTest\Asset\SimpleJob', $jobA->getMetadata('__name__')); + $this->assertEquals('1', $jobA->getMetadata('__id__')); + $this->assertTrue(new \DateTime('-10 seconds') < $jobA->getMetadata('__scheduled__')); + $this->assertTrue($jobA->getMetadata('__scheduled__') < new \DateTime('+10 seconds')); + $this->assertEquals('10', $jobA->getMetadata('__priority__')); + + // Job C + $this->assertCount(4, $jobC->getMetadata()); + $this->assertEquals('SlmQueueDoctrineTest\Asset\SimpleJob', $jobC->getMetadata('__name__')); + $this->assertEquals('3', $jobC->getMetadata('__id__')); + $this->assertTrue(new \DateTime('-10 seconds') < $jobC->getMetadata('__scheduled__')); + $this->assertTrue($jobC->getMetadata('__scheduled__') < new \DateTime('+10 seconds')); + $this->assertEquals('20', $jobC->getMetadata('__priority__')); } public function testJobCanBePushedMoreThenOnce(): void @@ -491,7 +516,12 @@ public function testPeek(): void $peekedJob = $this->queue->peek($job->getId()); - static::assertEquals($job, $peekedJob); + $this->assertCount(4, $peekedJob->getMetadata()); + $this->assertEquals('SlmQueueDoctrineTest\Asset\SimpleJob', $peekedJob->getMetadata('__name__')); + $this->assertEquals('1', $peekedJob->getMetadata('__id__')); + $this->assertTrue(new \DateTime('-10 seconds') < $peekedJob->getMetadata('__scheduled__')); + $this->assertTrue($peekedJob->getMetadata('__scheduled__') < new \DateTime('+10 seconds')); + $this->assertEquals('1024', $peekedJob->getMetadata('__priority__')); } public function testPeekNonExistent(): void