This commit is contained in:
Yuri Kuznetsov
2023-04-20 15:00:58 +03:00
parent c2b856c3da
commit 3a007518e5
5 changed files with 28 additions and 81 deletions
+10 -18
View File
@@ -30,6 +30,7 @@
namespace Espo\Core\Job;
use Espo\Core\Exceptions\Error;
use Espo\Core\Job\QueueProcessor\Params;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Log;
@@ -44,7 +45,6 @@ use Throwable;
class JobManager
{
private bool $useProcessPool = false;
protected string $lastRunTimeFile = 'data/cache/application/cronLastRunTime.php';
public function __construct(
@@ -55,15 +55,13 @@ class JobManager
private ScheduleProcessor $scheduleProcessor,
private QueueUtil $queueUtil,
private AsyncPoolFactory $asyncPoolFactory,
private QueueProcessorFactory $queueProcessorFactory
private QueueProcessor $queueProcessor
) {
if ($this->config->get('jobRunInParallel')) {
if ($this->asyncPoolFactory->isSupported()) {
$this->useProcessPool = true;
}
else {
$this->log->warning("JobManager: useProcessPool requires pcntl and posix extensions.");
} else {
$this->log->warning("Enabled `jobRunInParallel` parameter requires pcntl and posix extensions.");
}
}
}
@@ -93,16 +91,14 @@ class JobManager
*/
public function processQueue(string $queue, int $limit): void
{
$params = QueueProcessorParams
$params = Params
::create()
->withQueue($queue)
->withLimit($limit)
->withUseProcessPool(false)
->withNoLock(true);
$processor = $this->queueProcessorFactory->create($params);
$processor->process();
$this->queueProcessor->process($params);
}
/**
@@ -110,30 +106,26 @@ class JobManager
*/
public function processGroup(string $group, int $limit): void
{
$params = QueueProcessorParams
$params = Params
::create()
->withGroup($group)
->withLimit($limit)
->withUseProcessPool(false)
->withNoLock(true);
$processor = $this->queueProcessorFactory->create($params);
$processor->process();
$this->queueProcessor->process($params);
}
private function processMainQueue(): void
{
$limit = (int) $this->config->get('jobMaxPortion', 0);
$params = QueueProcessorParams
$params = Params
::create()
->withUseProcessPool($this->useProcessPool)
->withLimit($limit);
$processor = $this->queueProcessorFactory->create($params);
$processor->process();
$this->queueProcessor->process($params);
}
/**
+13 -13
View File
@@ -29,47 +29,47 @@
namespace Espo\Core\Job;
use Spatie\Async\Pool as AsyncPool;
use Espo\Entities\Job as JobEntity;
use Espo\Core\Job\QueueProcessor\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Core\Utils\System;
use Espo\Entities\Job as JobEntity;
use Espo\Core\Job\Job\Status;
use Spatie\Async\Pool as AsyncPool;
class QueueProcessor
{
public function __construct(
private QueueProcessorParams $params,
private QueueUtil $queueUtil,
private JobRunner $jobRunner,
private AsyncPoolFactory $asyncPoolFactory,
private EntityManager $entityManager
) {}
public function process(): void
public function process(Params $params): void
{
$pool = $this->params->useProcessPool() ?
$pool = $params->useProcessPool() ?
$this->asyncPoolFactory->create() :
null;
$pendingJobList = $this->queueUtil->getPendingJobList(
$this->params->getQueue(),
$this->params->getGroup(),
$this->params->getLimit()
$params->getQueue(),
$params->getGroup(),
$params->getLimit()
);
foreach ($pendingJobList as $job) {
$this->processJob($job, $pool);
$this->processJob($params, $job, $pool);
}
$pool?->wait();
}
private function processJob(JobEntity $job, ?AsyncPool $pool = null): void
private function processJob(Params $params, JobEntity $job, ?AsyncPool $pool = null): void
{
$useProcessPool = $this->params->useProcessPool();
$noLock = $this->params->noLock();
$useProcessPool = $params->useProcessPool();
$noLock = $params->noLock();
$lockTable = $job->getScheduledJobId() && !$noLock;
@@ -27,9 +27,9 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job;
namespace Espo\Core\Job\QueueProcessor;
class QueueProcessorParams
class Params
{
private bool $useProcessPool = false;
private bool $noLock = false;
@@ -1,45 +0,0 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Job;
use Espo\Core\InjectableFactory;
class QueueProcessorFactory
{
public function __construct(private InjectableFactory $injectableFactory)
{}
public function create(QueueProcessorParams $params): QueueProcessor
{
return $this->injectableFactory->createWith(QueueProcessor::class, [
'params' => $params,
]);
}
}
@@ -30,7 +30,7 @@
namespace tests\unit\Espo\Core\Job;
use Espo\Core\{
Job\QueueProcessorParams,
Job\QueueProcessor\Params,
};
class QueueProcessorParamsTest extends \PHPUnit\Framework\TestCase
@@ -41,7 +41,7 @@ class QueueProcessorParamsTest extends \PHPUnit\Framework\TestCase
public function testParams1()
{
$params = QueueProcessorParams
$params = \Espo\Core\Job\QueueProcessor\Params
::create()
->withLimit(10);
@@ -55,7 +55,7 @@ class QueueProcessorParamsTest extends \PHPUnit\Framework\TestCase
public function testParams2()
{
$params = QueueProcessorParams
$params = \Espo\Core\Job\QueueProcessor\Params
::create()
->withLimit(10)
->withUseProcessPool(true)