@@ -20,20 +20,22 @@ protected function fetchMessage(array $queues, string $deliveryId, int $redelive
20
20
21
21
$ this ->getConnection ()->beginTransaction ();
22
22
23
- $ message = $ this ->getConnection ()->createQueryBuilder ()
23
+ $ query = $ this ->getConnection ()->createQueryBuilder ()
24
24
->select ('* ' )
25
25
->from ($ this ->getContext ()->getTableName ())
26
26
->andWhere ('delivery_id IS NULL ' )
27
27
->andWhere ('delayed_until IS NULL OR delayed_until <= :delayedUntil ' )
28
28
->andWhere ('queue IN (:queues) ' )
29
29
->addOrderBy ('priority ' , 'desc ' )
30
30
->addOrderBy ('published_at ' , 'asc ' )
31
- ->setParameter ('delayedUntil ' , $ now , \Doctrine \DBAL \ParameterType::INTEGER )
32
- ->setParameter ('queues ' , array_values ($ queues ), \Doctrine \DBAL \Connection::PARAM_STR_ARRAY )
33
- ->setMaxResults (1 )
34
- ->execute ()
35
- ->fetch ()
36
- ;
31
+ ->setMaxResults (1 );
32
+
33
+ // select for update
34
+ $ message = $ this ->getConnection ()->executeQuery (
35
+ $ query ->getSQL ().' ' .$ this ->getConnection ()->getDatabasePlatform ()->getWriteLockSQL (),
36
+ ['delayedUntil ' => $ now , 'queues ' => array_values ($ queues )],
37
+ ['delayedUntil ' => \Doctrine \DBAL \ParameterType::INTEGER , 'queues ' => \Doctrine \DBAL \Connection::PARAM_STR_ARRAY ]
38
+ )->fetch ();
37
39
38
40
if (!$ message ) {
39
41
$ this ->getConnection ()->commit ();
@@ -50,7 +52,8 @@ protected function fetchMessage(array $queues, string $deliveryId, int $redelive
50
52
->setParameter ('id ' , $ message ['id ' ], Type::GUID )
51
53
->setParameter ('deliveryId ' , $ deliveryId , Type::STRING )
52
54
->setParameter ('redeliverAfter ' , $ now + $ redeliveryDelay , Type::BIGINT )
53
- ->execute ();
55
+ ->execute ()
56
+ ;
54
57
55
58
$ this ->getConnection ()->commit ();
56
59
@@ -61,7 +64,8 @@ protected function fetchMessage(array $queues, string $deliveryId, int $redelive
61
64
->setParameter ('deliveryId ' , $ deliveryId , Type::STRING )
62
65
->setMaxResults (1 )
63
66
->execute ()
64
- ->fetch ();
67
+ ->fetch ()
68
+ ;
65
69
66
70
return $ deliveredMessage ?: null ;
67
71
} catch (\Exception $ e ) {
0 commit comments