diff --git a/CHANGELOG.md b/CHANGELOG.md index 81aa3b4ca..8f08f1443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Change Log +## [0.9.7](https://github.com/php-enqueue/enqueue-dev/tree/0.9.7) (2019-02-01) +[Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.9.6...0.9.7) + +**Merged pull requests:** + +- Avoid OutOfMemoryException [\#725](https://github.com/php-enqueue/enqueue-dev/pull/725) ([DamienHarper](https://github.com/DamienHarper)) +- \[async-event-dispatcher\] Add default to php\_serializer\_event\_transformer [\#748](https://github.com/php-enqueue/enqueue-dev/pull/748) ([GCalmels](https://github.com/GCalmels)) +- \[async-event-dispatcher\] Fixed param on EventTransformer [\#736](https://github.com/php-enqueue/enqueue-dev/pull/736) ([samnela](https://github.com/samnela)) +- \[job-queue\] Install stable dependencies [\#745](https://github.com/php-enqueue/enqueue-dev/pull/745) ([mbabic131](https://github.com/mbabic131)) +- \[job-queue\] Fix job status processor [\#735](https://github.com/php-enqueue/enqueue-dev/pull/735) ([ASKozienko](https://github.com/ASKozienko)) +- \[redis\] Fix messages sent with incorrect delivery delay [\#738](https://github.com/php-enqueue/enqueue-dev/pull/738) ([niels-nijens](https://github.com/niels-nijens)) +- \[dbal\] Exception on affected record !=1 [\#733](https://github.com/php-enqueue/enqueue-dev/pull/733) ([otzy](https://github.com/otzy)) +- \[bundle\]\[dbal\] Use doctrine bundle configured connections [\#732](https://github.com/php-enqueue/enqueue-dev/pull/732) ([ASKozienko](https://github.com/ASKozienko)) +- \[pheanstalk\] Add unit tests for PheanstalkConsumer [\#726](https://github.com/php-enqueue/enqueue-dev/pull/726) ([alanpoulain](https://github.com/alanpoulain)) +- \[pheanstalk\] Requeuing a message should not acknowledge it beforehand [\#722](https://github.com/php-enqueue/enqueue-dev/pull/722) ([alanpoulain](https://github.com/alanpoulain)) +- \[sqs\] Dead Letter Queue Adoption [\#720](https://github.com/php-enqueue/enqueue-dev/pull/720) ([cshum](https://github.com/cshum)) + ## [0.9.6](https://github.com/php-enqueue/enqueue-dev/tree/0.9.6) (2019-01-09) [Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.9.5...0.9.6) diff --git a/docs/transport/redis.md b/docs/transport/redis.md index f4c84e29a..de9c5d62d 100644 --- a/docs/transport/redis.md +++ b/docs/transport/redis.md @@ -71,7 +71,7 @@ $factory = new RedisConnectionFactory([]); $factory = new RedisConnectionFactory([ 'host' => 'example.com', 'port' => 1000, - 'vendor' => 'phpredis', + 'scheme_extensions' => ['phpredis'], ]); // same as above but given as DSN string @@ -101,13 +101,13 @@ use Enqueue\Redis\RedisConnectionFactory; $connectionFactory = new RedisConnectionFactory([ 'host' => 'localhost', 'port' => 6379, - 'scheme_extensions' => 'predis', + 'scheme_extensions' => ['predis'], ]); $context = $connectionFactory->createContext(); ``` -* With custom redis instance: +* With predis and custom [options](https://github.com/nrk/predis/wiki/Client-Options): It gives you more control over vendor specific features. @@ -116,10 +116,15 @@ It gives you more control over vendor specific features. use Enqueue\Redis\RedisConnectionFactory; use Enqueue\Redis\PRedis; -$config = []; -$options = []; +$config = [ + 'host' => 'localhost', + 'port' => 6379, + 'predis_options' => [ + 'prefix' => 'ns:' + ] +]; -$redis = new PRedis(new \PRedis\Client($config, $options)); +$redis = new PRedis($config); $factory = new RedisConnectionFactory($redis); ``` @@ -232,4 +237,4 @@ To use it with Enqueue Redis you have to pass REDIS_URL to RedisConnectionFactor $connection = new \Enqueue\Redis\RedisConnectionFactory(getenv('REDIS_URL')); ``` -[back to index](../index.md) \ No newline at end of file +[back to index](../index.md) diff --git a/pkg/async-event-dispatcher/DependencyInjection/AsyncTransformersPass.php b/pkg/async-event-dispatcher/DependencyInjection/AsyncTransformersPass.php index 0adcfcb45..afe10217d 100644 --- a/pkg/async-event-dispatcher/DependencyInjection/AsyncTransformersPass.php +++ b/pkg/async-event-dispatcher/DependencyInjection/AsyncTransformersPass.php @@ -18,6 +18,7 @@ public function process(ContainerBuilder $container) $transformerIdsMap = []; $eventNamesMap = []; + $defaultTransformer = null; foreach ($container->findTaggedServiceIds('enqueue.event_transformer') as $serviceId => $tagAttributes) { foreach ($tagAttributes as $tagAttribute) { if (false == isset($tagAttribute['eventName'])) { @@ -28,11 +29,24 @@ public function process(ContainerBuilder $container) $transformerName = isset($tagAttribute['transformerName']) ? $tagAttribute['transformerName'] : $serviceId; - $eventNamesMap[$eventName] = $transformerName; - $transformerIdsMap[$transformerName] = $serviceId; + if (isset($tagAttribute['default']) && $tagAttribute['default']) { + $defaultTransformer = [ + 'id' => $serviceId, + 'transformerName' => $transformerName, + 'eventName' => $eventName, + ]; + } else { + $eventNamesMap[$eventName] = $transformerName; + $transformerIdsMap[$transformerName] = $serviceId; + } } } + if ($defaultTransformer) { + $eventNamesMap[$defaultTransformer['eventName']] = $defaultTransformer['transformerName']; + $transformerIdsMap[$defaultTransformer['transformerName']] = $defaultTransformer['id']; + } + $container->getDefinition('enqueue.events.registry') ->replaceArgument(0, $eventNamesMap) ->replaceArgument(1, $transformerIdsMap) diff --git a/pkg/async-event-dispatcher/EventTransformer.php b/pkg/async-event-dispatcher/EventTransformer.php index 6b6aa0f39..abb67566f 100644 --- a/pkg/async-event-dispatcher/EventTransformer.php +++ b/pkg/async-event-dispatcher/EventTransformer.php @@ -13,7 +13,7 @@ interface EventTransformer * * @return Message */ - public function toMessage($eventName, Event $event); + public function toMessage($eventName, Event $event = null); /** * If you able to transform message back to event return it. diff --git a/pkg/async-event-dispatcher/Resources/config/services.yml b/pkg/async-event-dispatcher/Resources/config/services.yml index 2cef52714..2b3fbc0b0 100644 --- a/pkg/async-event-dispatcher/Resources/config/services.yml +++ b/pkg/async-event-dispatcher/Resources/config/services.yml @@ -25,10 +25,10 @@ services: - '@event_dispatcher' - '@enqueue.events.async_listener' - enqueue.events.php_serializer_event_transofrmer: + enqueue.events.php_serializer_event_transformer: class: 'Enqueue\AsyncEventDispatcher\PhpSerializerEventTransformer' public: public arguments: - '@enqueue.events.context' tags: - - {name: 'enqueue.event_transformer', eventName: '/.*/', transformerName: 'php_serializer' } + - {name: 'enqueue.event_transformer', eventName: '/.*/', transformerName: 'php_serializer', default: true } diff --git a/pkg/dbal/DbalProducer.php b/pkg/dbal/DbalProducer.php index f1666de24..5435629eb 100644 --- a/pkg/dbal/DbalProducer.php +++ b/pkg/dbal/DbalProducer.php @@ -115,7 +115,7 @@ public function send(Destination $destination, Message $message): void } try { - $this->context->getDbalConnection()->insert($this->context->getTableName(), $dbalMessage, [ + $rowsAffected = $this->context->getDbalConnection()->insert($this->context->getTableName(), $dbalMessage, [ 'id' => Type::GUID, 'published_at' => Type::INTEGER, 'body' => Type::TEXT, @@ -125,10 +125,14 @@ public function send(Destination $destination, Message $message): void 'queue' => Type::STRING, 'time_to_live' => Type::INTEGER, 'delayed_until' => Type::INTEGER, - 'redelivered' => Type::BOOLEAN, + 'redelivered' => Type::SMALLINT, 'delivery_id' => Type::STRING, 'redeliver_after' => Type::BIGINT, ]); + + if (1 !== $rowsAffected) { + throw new Exception('The message was not enqueued. Dbal did not confirm that the record is inserted.'); + } } catch (\Exception $e) { throw new Exception('The transport fails to send the message due to some internal error.', 0, $e); } diff --git a/pkg/enqueue-bundle/EnqueueBundle.php b/pkg/enqueue-bundle/EnqueueBundle.php index 6db17e15d..195013578 100644 --- a/pkg/enqueue-bundle/EnqueueBundle.php +++ b/pkg/enqueue-bundle/EnqueueBundle.php @@ -5,6 +5,7 @@ use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension; use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventsPass; use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncTransformersPass; +use Enqueue\Doctrine\DoctrineSchemaCompilerPass; use Enqueue\Symfony\Client\DependencyInjection\AnalyzeRouteCollectionPass; use Enqueue\Symfony\Client\DependencyInjection\BuildClientExtensionsPass; use Enqueue\Symfony\Client\DependencyInjection\BuildCommandSubscriberRoutesPass as BuildClientCommandSubscriberRoutesPass; @@ -39,5 +40,7 @@ public function build(ContainerBuilder $container): void $container->addCompilerPass(new AsyncEventsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100); $container->addCompilerPass(new AsyncTransformersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100); } + + $container->addCompilerPass(new DoctrineSchemaCompilerPass()); } } diff --git a/pkg/enqueue-bundle/Tests/Functional/App/TestAsyncEventTransformer.php b/pkg/enqueue-bundle/Tests/Functional/App/TestAsyncEventTransformer.php index 63d6df75a..4293ab85e 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/TestAsyncEventTransformer.php +++ b/pkg/enqueue-bundle/Tests/Functional/App/TestAsyncEventTransformer.php @@ -24,7 +24,7 @@ public function __construct(Context $context) $this->context = $context; } - public function toMessage($eventName, Event $event) + public function toMessage($eventName, Event $event = null) { if (Event::class === get_class($event)) { return $this->context->createMessage(json_encode('')); diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml index 978da5d18..eab956cc4 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml @@ -14,6 +14,14 @@ framework: router: { resource: '%kernel.root_dir%/config/routing.yml' } default_locale: '%locale%' +doctrine: + dbal: + connections: + custom: + url: "%env(DOCTRINE_DSN)%" + driver: pdo_mysql + charset: UTF8 + services: test_enqueue.client.default.driver: alias: 'enqueue.client.default.driver' diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index 163527175..ee464080f 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -136,6 +136,13 @@ public function provideEnqueueConfigs() 'transport' => getenv('MONGO_DSN'), ], ]]; + + yield 'doctrine' => [[ + 'default' => [ + 'transport' => 'doctrine://custom', + ], + ]]; + // // yield 'gps' => [[ // 'transport' => [ diff --git a/pkg/enqueue/Consumption/QueueConsumer.php b/pkg/enqueue/Consumption/QueueConsumer.php index 7ba927696..3454b302d 100644 --- a/pkg/enqueue/Consumption/QueueConsumer.php +++ b/pkg/enqueue/Consumption/QueueConsumer.php @@ -323,9 +323,11 @@ private function onProcessorException(ExtensionInterface $extension, Consumer $c } } - $prev = new \ReflectionProperty('Exception', 'previous'); - $prev->setAccessible(true); - $prev->setValue($wrapper, $exception); + if ($exception !== $wrapper) { + $prev = new \ReflectionProperty('Exception', 'previous'); + $prev->setAccessible(true); + $prev->setValue($wrapper, $exception); + } throw $e; } diff --git a/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php new file mode 100644 index 000000000..db8f0dd76 --- /dev/null +++ b/pkg/enqueue/Doctrine/DoctrineConnectionFactoryFactory.php @@ -0,0 +1,54 @@ +doctrine = $doctrine; + $this->fallbackFactory = $fallbackFactory; + } + + public function create($config): ConnectionFactory + { + if (is_string($config)) { + $config = ['dsn' => $config]; + } + + if (false == is_array($config)) { + throw new \InvalidArgumentException('The config must be either array or DSN string.'); + } + + if (false == array_key_exists('dsn', $config)) { + throw new \InvalidArgumentException('The config must have dsn key set.'); + } + + $dsn = Dsn::parseFirst($config['dsn']); + + if ('doctrine' === $dsn->getScheme()) { + $config = $dsn->getQuery(); + $config['connection_name'] = $dsn->getHost(); + + return new ManagerRegistryConnectionFactory($this->doctrine, $config); + } + + return $this->fallbackFactory->create($config); + } +} diff --git a/pkg/enqueue/Doctrine/DoctrineDriverFactory.php b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php new file mode 100644 index 000000000..aab6489aa --- /dev/null +++ b/pkg/enqueue/Doctrine/DoctrineDriverFactory.php @@ -0,0 +1,41 @@ +fallbackFactory = $fallbackFactory; + } + + public function create(ConnectionFactory $factory, Config $config, RouteCollection $collection): DriverInterface + { + $dsn = $config->getTransportOption('dsn'); + + if (empty($dsn)) { + throw new \LogicException('This driver factory relies on dsn option from transport config. The option is empty or not set.'); + } + + $dsn = Dsn::parseFirst($dsn); + + if ('doctrine' === $dsn->getScheme()) { + return new DbalDriver($factory->createContext(), $config, $collection); + } + + return $this->fallbackFactory->create($factory, $config, $collection); + } +} diff --git a/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php b/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php new file mode 100644 index 000000000..25016a761 --- /dev/null +++ b/pkg/enqueue/Doctrine/DoctrineSchemaCompilerPass.php @@ -0,0 +1,39 @@ +hasDefinition('doctrine')) { + return; + } + + foreach ($container->getParameter('enqueue.transports') as $name) { + $diUtils = DiUtils::create(TransportFactory::MODULE, $name); + + $container->register($diUtils->format('connection_factory_factory.outer'), DoctrineConnectionFactoryFactory::class) + ->setDecoratedService($diUtils->format('connection_factory_factory'), $diUtils->format('connection_factory_factory.inner')) + ->addArgument(new Reference('doctrine')) + ->addArgument(new Reference($diUtils->format('connection_factory_factory.inner'))) + ; + } + + foreach ($container->getParameter('enqueue.clients') as $name) { + $diUtils = DiUtils::create(ClientFactory::MODULE, $name); + + $container->register($diUtils->format('driver_factory.outer'), DoctrineDriverFactory::class) + ->setDecoratedService($diUtils->format('driver_factory'), $diUtils->format('driver_factory.inner')) + ->addArgument(new Reference($diUtils->format('driver_factory.inner'))) + ; + } + } +} diff --git a/pkg/job-queue/JobProcessor.php b/pkg/job-queue/JobProcessor.php index 77c03eefe..8f197f742 100644 --- a/pkg/job-queue/JobProcessor.php +++ b/pkg/job-queue/JobProcessor.php @@ -260,7 +260,7 @@ protected function saveJob(Job $job) */ protected function sendCalculateRootJobStatusEvent(Job $job) { - $this->producer->sendEvent(Commands::CALCULATE_ROOT_JOB_STATUS, [ + $this->producer->sendCommand(Commands::CALCULATE_ROOT_JOB_STATUS, [ 'jobId' => $job->getId(), ]); } diff --git a/pkg/job-queue/Tests/JobProcessorTest.php b/pkg/job-queue/Tests/JobProcessorTest.php index 483e8ef2d..5ee8159a1 100644 --- a/pkg/job-queue/Tests/JobProcessorTest.php +++ b/pkg/job-queue/Tests/JobProcessorTest.php @@ -169,7 +169,7 @@ public function testCreateChildJobShouldCreateAndSaveJobAndPublishRecalculateRoo $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('sendEvent') + ->method('sendCommand') ->with(Commands::CALCULATE_ROOT_JOB_STATUS, ['jobId' => 12345]) ; @@ -246,7 +246,7 @@ public function testStartJobShouldUpdateJobWithRunningStatusAndStartAtTime() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('sendEvent') + ->method('sendCommand') ; $processor = new JobProcessor($storage, $producer); @@ -316,7 +316,7 @@ public function testSuccessJobShouldUpdateJobWithSuccessStatusAndStopAtTime() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('sendEvent') + ->method('sendCommand') ; $processor = new JobProcessor($storage, $producer); @@ -386,7 +386,7 @@ public function testFailJobShouldUpdateJobWithFailStatusAndStopAtTime() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('sendEvent') + ->method('sendCommand') ; $processor = new JobProcessor($storage, $producer); @@ -456,7 +456,7 @@ public function testCancelJobShouldUpdateJobWithCancelStatusAndStoppedAtTimeAndS $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('sendEvent') + ->method('sendCommand') ; $processor = new JobProcessor($storage, $producer); diff --git a/pkg/job-queue/composer.json b/pkg/job-queue/composer.json index 0dab189f1..4581d6cee 100644 --- a/pkg/job-queue/composer.json +++ b/pkg/job-queue/composer.json @@ -8,7 +8,7 @@ "require": { "php": "^7.1.3", "enqueue/enqueue": "^0.9", - "enqueue/null": "0.9.x-dev", + "enqueue/null": "^0.9", "queue-interop/queue-interop": "^0.7|^0.8", "doctrine/orm": "~2.4" }, diff --git a/pkg/pheanstalk/PheanstalkConsumer.php b/pkg/pheanstalk/PheanstalkConsumer.php index 337b77d9d..d8460f58a 100644 --- a/pkg/pheanstalk/PheanstalkConsumer.php +++ b/pkg/pheanstalk/PheanstalkConsumer.php @@ -88,11 +88,22 @@ public function acknowledge(Message $message): void */ public function reject(Message $message, bool $requeue = false): void { - $this->acknowledge($message); + InvalidMessageException::assertMessageInstanceOf($message, PheanstalkMessage::class); + + if (false == $message->getJob()) { + throw new \LogicException(sprintf( + 'The message could not be %s because it does not have job set.', + $requeue ? 'requeued' : 'rejected' + )); + } if ($requeue) { $this->pheanstalk->release($message->getJob(), $message->getPriority(), $message->getDelay()); + + return; } + + $this->acknowledge($message); } private function convertJobToMessage(Job $job): PheanstalkMessage diff --git a/pkg/pheanstalk/Tests/PheanstalkConsumerTest.php b/pkg/pheanstalk/Tests/PheanstalkConsumerTest.php index 60015dc99..7600e9921 100644 --- a/pkg/pheanstalk/Tests/PheanstalkConsumerTest.php +++ b/pkg/pheanstalk/Tests/PheanstalkConsumerTest.php @@ -96,7 +96,7 @@ public function testShouldReceiveNoWaitFromQueueAndReturnNullIfNoMessageInQueue( public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue() { $destination = new PheanstalkDestination('theQueueName'); - $message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); + $message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); $job = new Job('theJobId', json_encode($message)); @@ -118,6 +118,109 @@ public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue $this->assertSame($job, $actualMessage->getJob()); } + public function testShouldAcknowledgeMessage() + { + $destination = new PheanstalkDestination('theQueueName'); + $message = new PheanstalkMessage(); + + $job = new Job('theJobId', json_encode($message)); + $message->setJob($job); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('delete') + ->with($job) + ; + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $consumer->acknowledge($message); + } + + public function testAcknowledgeShouldThrowExceptionIfMessageHasNoJob() + { + $destination = new PheanstalkDestination('theQueueName'); + $pheanstalk = $this->createPheanstalkMock(); + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The message could not be acknowledged because it does not have job set.'); + + $consumer->acknowledge(new PheanstalkMessage()); + } + + public function testShouldRejectMessage() + { + $destination = new PheanstalkDestination('theQueueName'); + $message = new PheanstalkMessage(); + + $job = new Job('theJobId', json_encode($message)); + $message->setJob($job); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('delete') + ->with($job) + ; + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $consumer->reject($message); + } + + public function testRejectShouldThrowExceptionIfMessageHasNoJob() + { + $destination = new PheanstalkDestination('theQueueName'); + $pheanstalk = $this->createPheanstalkMock(); + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The message could not be rejected because it does not have job set.'); + + $consumer->reject(new PheanstalkMessage()); + } + + public function testShouldRequeueMessage() + { + $destination = new PheanstalkDestination('theQueueName'); + $message = new PheanstalkMessage(); + + $job = new Job('theJobId', json_encode($message)); + $message->setJob($job); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('release') + ->with($job, Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY) + ; + $pheanstalk + ->expects($this->never()) + ->method('delete') + ; + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $consumer->reject($message, true); + } + + public function testRequeueShouldThrowExceptionIfMessageHasNoJob() + { + $destination = new PheanstalkDestination('theQueueName'); + $pheanstalk = $this->createPheanstalkMock(); + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The message could not be requeued because it does not have job set.'); + + $consumer->reject(new PheanstalkMessage(), true); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk */ diff --git a/pkg/redis/RedisProducer.php b/pkg/redis/RedisProducer.php index 3721ed20a..cc7d63230 100644 --- a/pkg/redis/RedisProducer.php +++ b/pkg/redis/RedisProducer.php @@ -64,7 +64,7 @@ public function send(Destination $destination, Message $message): void $payload = $this->context->getSerializer()->toString($message); if ($message->getDeliveryDelay()) { - $deliveryAt = time() + $message->getDeliveryDelay(); + $deliveryAt = time() + $message->getDeliveryDelay() / 1000; $this->context->getRedis()->zadd($destination->getName().':delayed', $payload, $deliveryAt); } else { $this->context->getRedis()->lpush($destination->getName(), $payload); diff --git a/pkg/redis/Tests/RedisProducerTest.php b/pkg/redis/Tests/RedisProducerTest.php index 12082734c..daef46b61 100644 --- a/pkg/redis/Tests/RedisProducerTest.php +++ b/pkg/redis/Tests/RedisProducerTest.php @@ -88,6 +88,45 @@ public function testShouldCallLPushOnSend() $producer->send($destination, new RedisMessage()); } + /** + * Tests if Redis::zadd is called with the expected 'score' (used as delivery timestamp). + * + * @depends testShouldCallLPushOnSend + */ + public function testShouldCallZaddOnSendWithDeliveryDelay() + { + $destination = new RedisDestination('aDestination'); + + $redisMock = $this->createRedisMock(); + $redisMock + ->expects($this->once()) + ->method('zadd') + ->with( + 'aDestination:delayed', + $this->isJson(), + $this->equalTo(time() + 5) + ) + ; + + $context = $this->createContextMock(); + $context + ->expects($this->once()) + ->method('getRedis') + ->willReturn($redisMock) + ; + $context + ->expects($this->once()) + ->method('getSerializer') + ->willReturn(new JsonSerializer()) + ; + + $message = new RedisMessage(); + $message->setDeliveryDelay(5000); // 5 seconds in milliseconds + + $producer = new RedisProducer($context); + $producer->send($destination, $message); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|RedisContext */ diff --git a/pkg/sqs/SqsClient.php b/pkg/sqs/SqsClient.php index 65cf2fb29..4fc87fabb 100644 --- a/pkg/sqs/SqsClient.php +++ b/pkg/sqs/SqsClient.php @@ -43,6 +43,11 @@ public function receiveMessage(array $args): Result return $this->callApi('receiveMessage', $args); } + public function changeMessageVisibility(array $args): Result + { + return $this->callApi('changeMessageVisibility', $args); + } + public function purgeQueue(array $args): Result { return $this->callApi('purgeQueue', $args); diff --git a/pkg/sqs/SqsConsumer.php b/pkg/sqs/SqsConsumer.php index b6ded7e20..3c8714e3d 100644 --- a/pkg/sqs/SqsConsumer.php +++ b/pkg/sqs/SqsConsumer.php @@ -133,14 +133,19 @@ public function reject(Message $message, bool $requeue = false): void { InvalidMessageException::assertMessageInstanceOf($message, SqsMessage::class); - $this->context->getSqsClient()->deleteMessage([ - '@region' => $this->queue->getRegion(), - 'QueueUrl' => $this->context->getQueueUrl($this->queue), - 'ReceiptHandle' => $message->getReceiptHandle(), - ]); - if ($requeue) { - $this->context->createProducer()->send($this->queue, $message); + $this->context->getSqsClient()->changeMessageVisibility([ + '@region' => $this->queue->getRegion(), + 'QueueUrl' => $this->context->getQueueUrl($this->queue), + 'ReceiptHandle' => $message->getReceiptHandle(), + 'VisibilityTimeout' => 0, + ]); + } else { + $this->context->getSqsClient()->deleteMessage([ + '@region' => $this->queue->getRegion(), + 'QueueUrl' => $this->context->getQueueUrl($this->queue), + 'ReceiptHandle' => $message->getReceiptHandle(), + ]); } } diff --git a/pkg/sqs/Tests/SqsConsumerTest.php b/pkg/sqs/Tests/SqsConsumerTest.php index 5d1f1cdeb..67e6f8fc4 100644 --- a/pkg/sqs/Tests/SqsConsumerTest.php +++ b/pkg/sqs/Tests/SqsConsumerTest.php @@ -203,26 +203,15 @@ public function testShouldRejectMessageAndRequeue() $client = $this->createSqsClientMock(); $client ->expects($this->once()) - ->method('deleteMessage') + ->method('changeMessageVisibility') ->with($this->identicalTo([ - '@region' => null, + '@region' => 'theRegion', 'QueueUrl' => 'theQueueUrl', 'ReceiptHandle' => 'theReceipt', + 'VisibilityTimeout' => 0, ])) ; - $message = new SqsMessage(); - $message->setReceiptHandle('theReceipt'); - - $destination = new SqsDestination('queue'); - - $producer = $this->createProducerMock(); - $producer - ->expects($this->once()) - ->method('send') - ->with($this->identicalTo($destination), $this->identicalTo($message)) - ; - $context = $this->createContextMock(); $context ->expects($this->once()) @@ -235,11 +224,16 @@ public function testShouldRejectMessageAndRequeue() ->willReturn('theQueueUrl') ; $context - ->expects($this->once()) + ->expects($this->never()) ->method('createProducer') - ->willReturn($producer) ; + $message = new SqsMessage(); + $message->setReceiptHandle('theReceipt'); + + $destination = new SqsDestination('queue'); + $destination->setRegion('theRegion'); + $consumer = new SqsConsumer($context, $destination); $consumer->reject($message, true); }