diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index e342d49d4ab..ffe44641501 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -9,4 +9,8 @@ ### Extensibility - Added `craft\fields\BaseRelationField::canShowSiteMenu()`. +- Added `craft\queue\BaseBatchedJob::after()`. +- Added `craft\queue\BaseBatchedJob::afterBatch()`. +- Added `craft\queue\BaseBatchedJob::before()`. +- Added `craft\queue\BaseBatchedJob::beforeBatch()`. - `craft\fields\data\ColorData` now extends `craft\base\Model` and includes `blue`, `green`, `hex`, `luma`, `red`, and `rgb` attributes in its array keys. ([#17265](https://github.com/craftcms/cms/issues/17265)) diff --git a/src/queue/BaseBatchedJob.php b/src/queue/BaseBatchedJob.php index 47c87bf15b4..0e1aba4c4aa 100644 --- a/src/queue/BaseBatchedJob.php +++ b/src/queue/BaseBatchedJob.php @@ -133,6 +133,12 @@ public function execute($queue): void $startMemory = $memoryLimit != -1 ? memory_get_usage() : null; $start = microtime(true); + if ($this->itemOffset === 0) { + $this->before(); + } + + $this->beforeBatch(); + $i = 0; foreach ($items as $item) { @@ -168,11 +174,15 @@ public function execute($queue): void } } + $this->afterBatch(); + // Spawn another job if there are more items if ($this->itemOffset < $this->totalItems()) { $nextJob = clone $this; $nextJob->batchIndex++; QueueHelper::push($nextJob, $this->priority, 0, $this->ttr, $queue); + } else { + $this->after(); } } @@ -183,6 +193,42 @@ public function execute($queue): void */ abstract protected function processItem(mixed $item): void; + /** + * Does things before the first item of the first batch. + * + * @since 4.16.0 + */ + protected function before(): void + { + } + + /** + * Does things after the last item of the last batch. + * + * @since 4.16.0 + */ + protected function after(): void + { + } + + /** + * Does things before the first item of the current batch. + * + * @since 4.16.0 + */ + protected function beforeBatch(): void + { + } + + /** + * Does things after the last item of the current batch. + * + * @since 4.16.0 + */ + protected function afterBatch(): void + { + } + /** * @inheritdoc */