Skip to content

Commit 6490b02

Browse files
authored
Merge pull request #17058 from craftcms/bugfix/batched-jobs-and-ttr
don't push new job for each item if `ttr` is `null`
2 parents 4e7b121 + 33ed4a8 commit 6490b02

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fixed an error that could occur when changing a field’s type. ([#17045](https://github.com/craftcms/cms/issues/17045))
66
- Fixed a bug where field settings weren’t being retained when changing a field’s type to a preselected value. ([#17045](https://github.com/craftcms/cms/issues/17045))
7+
- Fixed a bug where batched jobs that were pushed without an explicit TTR were getting new jobs spawned after each item processed. ([#17058](https://github.com/craftcms/cms/pull/17058))
78

89
## 4.15.0-beta.1 - 2025-04-08
910

src/queue/BaseBatchedJob.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use craft\helpers\ConfigHelper;
1313
use craft\helpers\Queue as QueueHelper;
1414
use craft\i18n\Translation;
15+
use yii\queue\RetryableJobInterface;
1516

1617
/**
1718
* BaseBatchedJob is the base class for large jobs that may need to spawn
@@ -61,6 +62,18 @@ abstract class BaseBatchedJob extends BaseJob
6162
private ?Batchable $_data = null;
6263
private ?int $_totalItems = null;
6364

65+
/**
66+
* @inheritdoc
67+
*/
68+
public function init(): void
69+
{
70+
parent::init();
71+
72+
$this->ttr = $this->ttr ??
73+
($this instanceof RetryableJobInterface ? $this->getTtr() : null) ??
74+
Craft::$app->getQueue()->ttr;
75+
}
76+
6477
public function __sleep(): array
6578
{
6679
return array_keys(Craft::getObjectVars($this));
@@ -142,10 +155,10 @@ public function execute($queue): void
142155
}
143156
}
144157

145-
// Make sure we don't hit the TTL, even if the next item takes twice as long as the average
158+
// Make sure we don't hit the TTR, even if the next item takes twice as long as the average
146159
$runningTime = microtime(true) - $start;
147160
$avgRunningTime = $runningTime / $i;
148-
if ($runningTime + ($avgRunningTime * 2) > $this->ttr) {
161+
if ($this->ttr !== null && $runningTime + ($avgRunningTime * 2) > $this->ttr) {
149162
break;
150163
}
151164

0 commit comments

Comments
 (0)