Skip to content

Commit e376e77

Browse files
committed
DbQueue use lock for update from hectororm/query package instead of try another job
1 parent 4c7aa8d commit e376e77

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.0-beta9] - 2025-02-04
9+
10+
### Changed
11+
12+
- `DbQueue` use lock for update from `hectororm/query` package instead of try another job
13+
814
## [1.0.0-beta8] - 2025-02-03
915

1016
### Added

src/Queue/DbQueue.php

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,41 +82,35 @@ public function size(): int
8282
*/
8383
public function consume(): ?DbJob
8484
{
85-
$attempts = 0;
86-
87-
do {
88-
$jobRaw = $this->addBuilderConditions($this->getQueryBuilder())
89-
->orderBy('job_id', Order::ORDER_ASC)
90-
->limit(1)
91-
->fetchOne();
92-
93-
if (null === $jobRaw) {
94-
return null;
95-
}
96-
97-
// Lock
98-
$affected = $this->getQueryBuilder()
99-
->whereEquals(
100-
array_filter(
101-
$jobRaw,
102-
fn($k) => in_array(
103-
$k,
104-
['job_id', 'queue', 'availability_time', 'attempts', 'lock_time']
105-
),
106-
ARRAY_FILTER_USE_KEY
107-
)
85+
$jobRaw = $this->addBuilderConditions($this->getQueryBuilder())
86+
->orderBy('job_id', Order::ORDER_ASC)
87+
->limit(1)
88+
->fetchOne(true);
89+
90+
if (null === $jobRaw) {
91+
return null;
92+
}
93+
94+
// Lock
95+
$affected = $this->getQueryBuilder()
96+
->whereEquals(
97+
array_filter(
98+
$jobRaw,
99+
fn($k) => in_array(
100+
$k,
101+
['job_id', 'queue', 'availability_time', 'attempts', 'lock_time']
102+
),
103+
ARRAY_FILTER_USE_KEY
108104
)
109-
->update($updatedData = [
110-
'lock_time' => $this->now()->format('Y-m-d H:i:s'),
111-
'attempts' => ($jobRaw['attempts'] ?? 0) + 1,
112-
]);
113-
114-
if ($affected === 1) {
115-
return $this->createJob(array_replace($jobRaw, $updatedData));
116-
}
105+
)
106+
->update($updatedData = [
107+
'lock_time' => $this->now()->format('Y-m-d H:i:s'),
108+
'attempts' => ($jobRaw['attempts'] ?? 0) + 1,
109+
]);
117110

118-
$attempts++;
119-
} while ($attempts < 5);
111+
if ($affected === 1) {
112+
return $this->createJob(array_replace($jobRaw, $updatedData));
113+
}
120114

121115
return null;
122116
}

0 commit comments

Comments
 (0)