2525use Hector \Connection \Connection ;
2626use Hector \Query \Component \Order ;
2727use Hector \Query \QueryBuilder ;
28+ use Hector \Query \Statement \Conditions ;
2829
2930class_exists (QueryBuilder::class) || throw QueueManagerException::missingPackage ('hectororm/query ' );
3031
@@ -34,11 +35,17 @@ public function __construct(
3435 private Connection $ connection ,
3536 string $ name = 'default ' ,
3637 private string $ tableName = 'queue_jobs ' ,
38+ private int $ retryTime = 30 ,
3739 private int $ maxAttempts = 5 ,
3840 ) {
3941 parent ::__construct ($ name );
4042 }
4143
44+ private function getRetryDateTimeLimit (): DateTimeInterface
45+ {
46+ return $ this ->now ()->sub (new DateInterval ('PT ' . $ this ->retryTime . 'S ' ));
47+ }
48+
4249 private function getQueryBuilder (): QueryBuilder
4350 {
4451 $ builder = new QueryBuilder ($ this ->connection );
@@ -48,16 +55,26 @@ private function getQueryBuilder(): QueryBuilder
4855 return $ builder ;
4956 }
5057
58+ private function addBuilderConditions (QueryBuilder $ builder ): QueryBuilder
59+ {
60+ $ retryLimit = $ this ->getRetryDateTimeLimit ();
61+
62+ return $ builder
63+ ->whereLessThanOrEqual ('availability_time ' , $ this ->now ()->format ('Y-m-d H:i:s ' ))
64+ ->whereLessThan ('attempts ' , $ this ->maxAttempts )
65+ ->where (function (Conditions $ where ) use ($ retryLimit ): void {
66+ $ where
67+ ->whereNull ('lock_time ' )
68+ ->orWhere ('lock_time ' , '< ' , $ retryLimit ->format ('Y-m-d H:i:s ' ));
69+ });
70+ }
71+
5172 /**
5273 * @inheritDoc
5374 */
5475 public function size (): int
5576 {
56- return $ this ->getQueryBuilder ()
57- ->whereLessThanOrEqual ('availability_time ' , $ this ->now ()->format ('Y-m-d H:i:s ' ))
58- ->whereNull ('lock_time ' )
59- ->whereLessThan ('attempts ' , $ this ->maxAttempts )
60- ->count ();
77+ return $ this ->addBuilderConditions ($ this ->getQueryBuilder ())->count ();
6178 }
6279
6380 /**
@@ -68,10 +85,7 @@ public function consume(): ?DbJob
6885 $ attempts = 0 ;
6986
7087 do {
71- $ jobRaw = $ this ->getQueryBuilder ()
72- ->whereLessThanOrEqual ('availability_time ' , $ this ->now ()->format ('Y-m-d H:i:s ' ))
73- ->whereNull ('lock_time ' )
74- ->whereLessThan ('attempts ' , $ this ->maxAttempts )
88+ $ jobRaw = $ this ->addBuilderConditions ($ this ->getQueryBuilder ())
7589 ->orderBy ('job_id ' , Order::ORDER_ASC )
7690 ->limit (1 )
7791 ->fetchOne ();
0 commit comments