PMCORE-949 Use of buffered query on cron's calculate parameter/task exhausts php's memory limit

This commit is contained in:
Roly Gutierrez
2022-08-05 11:45:35 -04:00
committed by Julio Cesar Laura Avendaño
parent c48f006c6a
commit 809a1feeef
4 changed files with 240 additions and 90 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace ProcessMaker\Util;
class BatchProcessWithIndexes
{
/**
* Start of the query.
* @var int
*/
private $start = 0;
/**
* Limit of the query.
* @var int
*/
private $limit = 1000;
/**
* Total size of the query.
* @var int
*/
private $size = 0;
/**
* Constructor of the class.
* @param int $size
*/
public function __construct(int $size)
{
$this->size = $size;
}
/**
* Set custom limit of the query.
* @param int $limit
* @return self
*/
public function setLimit(int $limit): self
{
$this->limit = $limit;
return $this;
}
/**
* Batch process returning the index for query.
* @param callable $callback
* @return void
*/
public function process(callable $callback): void
{
for ($batch = 1; $this->start < $this->size; $batch++) {
$callback($this->start, $this->limit);
$this->start = $batch * $this->limit;
}
}
}