Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
46 changes: 46 additions & 0 deletions src/queue/BaseBatchedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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
*/
Expand Down
Loading