job refactoring

This commit is contained in:
Yuri Kuznetsov
2021-09-23 12:56:57 +03:00
parent ce806c5aa8
commit 111524b6c6
20 changed files with 133 additions and 84 deletions

View File

@@ -31,14 +31,18 @@ namespace Espo\Classes\AppInfo;
use Espo\Core\Console\Params;
use Espo\Core\Utils\ClassFinder;
use Espo\Core\Job\MetadataProvider;
class Jobs
{
private $classFinder;
public function __construct(ClassFinder $classFinder)
private $metadataProvider;
public function __construct(ClassFinder $classFinder, MetadataProvider $metadataProvider)
{
$this->classFinder = $classFinder;
$this->metadataProvider = $metadataProvider;
}
public function process(Params $params): string
@@ -47,9 +51,14 @@ class Jobs
$list = array_map(
function ($item) {
return ' '. $item;
return ' ' . $item;
},
array_keys($this->classFinder->getMap('Jobs'))
array_unique(
array_merge(
array_keys($this->classFinder->getMap('Jobs')),
$this->metadataProvider->getScheduledJobNameList()
)
)
);
asort($list);

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\{
Utils\Config,

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\Exceptions\Error;

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\Exceptions\Error;

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
class CheckNewExtensionVersion extends CheckNewVersion
{
@@ -50,4 +50,4 @@ class CheckNewExtensionVersion extends CheckNewVersion
$this->entityManager->saveEntity($job);
}
}
}

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\{
Utils\Config,

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\Record\ServiceContainer;

View File

@@ -27,15 +27,11 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\{
Job\JobDataLess,
};
use Espo\Core\Job\JobDataLess;
class Dummy implements JobDataLess
{
public function run(): void
{
}
public function run(): void {}
}

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\{
Job\JobDataLess,

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Classes\Jobs;
use Espo\Core\Job\JobDataLess;

View File

@@ -34,7 +34,7 @@ use Espo\Core\Job\Job;
use Espo\Core\Job\Job\Data;
use Espo\Core\Job\JobManager;
abstract class AbstractGroupJob implements Job
class ProcessJobGroup implements Job
{
private const PORTION_NUMBER = 100;

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Core\Job\Job\Jobs;
use Espo\Core\{
Job\QueueName,

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Core\Job\Job\Jobs;
use Espo\Core\{
Job\QueueName,

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Jobs;
namespace Espo\Core\Job\Job\Jobs;
use Espo\Core\{
Job\QueueName,

View File

@@ -29,6 +29,8 @@
namespace Espo\Core\Job;
use Espo\Core\Job\MetadataProvider;
use Espo\Core\{
Exceptions\Error,
Utils\ClassFinder,
@@ -41,14 +43,20 @@ class JobFactory
private $injectableFactory;
public function __construct(ClassFinder $classFinder, InjectableFactory $injectableFactory)
{
private $metadataProvider;
public function __construct(
ClassFinder $classFinder,
InjectableFactory $injectableFactory,
MetadataProvider $metadataProvider
) {
$this->classFinder = $classFinder;
$this->injectableFactory = $injectableFactory;
$this->metadataProvider = $metadataProvider;
}
/**
* Create a job.
* Create a job by a scheduled job name.
*
* @return Job|JobDataLess
* @throws Error
@@ -65,7 +73,7 @@ class JobFactory
}
/**
* Create a job by class name.
* Create a job by a class name.
* @return Job|JobDataLess
*/
@@ -78,6 +86,12 @@ class JobFactory
private function getClassName(string $name): ?string
{
$className = $this->metadataProvider->getJobClassName($name);
if ($className) {
return $className;
}
return $this->classFinder->find('Jobs', ucfirst($name));
}
}

View File

@@ -50,7 +50,7 @@ class MetadataProvider
$items = $this->metadata->get(['app', 'scheduledJobs']) ?? [];
foreach ($items as $name => $item) {
$isPreparable = $item['isPreparable'] ?? false;
$isPreparable = (bool) ($item['preparatorClassName'] ?? null);
if ($isPreparable) {
$list[] = $name;
@@ -60,13 +60,40 @@ class MetadataProvider
return $list;
}
public function isJobSystem(string $name): bool
{
return (bool) $this->metadata->get(['app', 'scheduledJobs', $name, 'isSystem']);
}
public function isJobPreparable(string $name): bool
{
return (bool) $this->metadata->get(['app', 'scheduledJobs', $name, 'isPreparable']);
return (bool) $this->metadata->get(['app', 'scheduledJobs', $name, 'preparatorClassName']);
}
public function getPreparatorClassName(string $name): ?string
{
return $this->metadata->get(['app', 'scheduledJobs', $name, 'preparatorClassName']);
}
public function getJobClassName(string $name): ?string
{
return $this->metadata->get(['app', 'scheduledJobs', $name, 'jobClassName']);
}
public function getScheduledJobNameList(): array
{
return array_keys($this->metadata->get(['app', 'scheduledJobs']) ?? []);
}
public function getNonSystemScheduledJobNameList(): array
{
return array_filter(
$this->getScheduledJobNameList(),
function (string $item) {
$isSystem = (bool) $this->metadata->get(['app', 'scheduledJobs', $item, 'isSystem']);
return !$isSystem;
}
);
}
}

View File

@@ -40,7 +40,7 @@ use Espo\Entities\Job as JobEntity;
use DateTimeImmutable;
class GroupPreparator implements Preparator
class ProcessJobGroupPreparator implements Preparator
{
private $entityManager;

View File

@@ -29,6 +29,8 @@
namespace Espo\Core\Utils;
use Espo\Core\Job\MetadataProvider;
use Espo\Core\{
Utils\ClassFinder,
Utils\Language,
@@ -59,28 +61,45 @@ class ScheduledJob
'default' => '* * * * * cd {DOCUMENT_ROOT}; {PHP-BINARY} -f {CRON-FILE} > /dev/null 2>&1',
];
protected $classFinder;
private $classFinder;
protected $language;
private $language;
protected $entityManager;
private $entityManager;
public function __construct(ClassFinder $classFinder, EntityManager $entityManager, Language $language)
{
private $metadataProvider;
public function __construct(
ClassFinder $classFinder,
EntityManager $entityManager,
Language $language,
MetadataProvider $metadataProvider
) {
$this->classFinder = $classFinder;
$this->entityManager = $entityManager;
$this->language = $language;
$this->metadataProvider = $metadataProvider;
$this->systemUtil = new System();
}
public function getAvailableList(): array
{
$map = $this->classFinder->getMap('Jobs');
$list = array_filter(
array_merge(
$this->metadataProvider->getNonSystemScheduledJobNameList(),
array_keys(
$this->classFinder->getMap('Jobs')
)
),
function (string $item) {
return !$this->metadataProvider->isJobSystem($item);
}
);
$list = array_keys($map);
asort($list);
return $list;
return array_values($list);
}
public function getSetupMessage(): array

View File

@@ -1,34 +0,0 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 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\Jobs;
use Espo\Core\Job\Job\Jobs\AbstractGroupJob;
class ProcessJobGroup extends AbstractGroupJob {}

View File

@@ -3,44 +3,62 @@
"name": "Process Job Group",
"isSystem": true,
"scheduling": "* * * * *",
"isPreparable": true,
"preparatorClassName": "Espo\\Core\\Job\\Preparator\\Preparators\\GroupPreparator"
"jobClassName": "Espo\\Core\\Job\\Job\\Jobs\\ProcessJobGroup",
"preparatorClassName": "Espo\\Core\\Job\\Preparator\\Preparators\\ProcessJobGroupPreparator"
},
"ProcessJobQueueQ0": {
"name": "Process Job Queue q0",
"isSystem": true,
"scheduling": "* * * * *"
"scheduling": "* * * * *",
"jobClassName": "Espo\\Core\\Job\\Job\\Jobs\\ProcessJobQueueQ0"
},
"ProcessJobQueueQ1": {
"name": "Process Job Queue q1",
"isSystem": true,
"scheduling": "*/1 * * * *"
"scheduling": "*/1 * * * *",
"jobClassName": "Espo\\Core\\Job\\Job\\Jobs\\ProcessJobQueueQ1"
},
"ProcessJobQueueE0": {
"name": "Process Job Queue e0",
"isSystem": true,
"scheduling": "* * * * *"
"scheduling": "* * * * *",
"jobClassName": "Espo\\Core\\Job\\Job\\Jobs\\ProcessJobQueueE0"
},
"Dummy": {
"isSystem": true,
"scheduling": "1 */12 * * *"
"scheduling": "1 */12 * * *",
"jobClassName": "Espo\\Classes\\Jobs\\Dummy"
},
"CheckNewVersion": {
"name": "Check for New Version",
"isSystem": true,
"scheduling": "15 5 * * *"
"scheduling": "15 5 * * *",
"jobClassName": "Espo\\Classes\\Jobs\\CheckNewVersion"
},
"CheckNewExtensionVersion": {
"name": "Check for New Versions of Installed Extensions",
"isSystem": true,
"scheduling": "25 5 * * *"
"scheduling": "25 5 * * *",
"jobClassName": "Espo\\Classes\\Jobs\\CheckNewExtensionVersion"
},
"Cleanup": {
"jobClassName": "Espo\\Classes\\Jobs\\Cleanup"
},
"AuthTokenControl": {
"jobClassName": "Espo\\Classes\\Jobs\\AuthTokenControl"
},
"SendEmailNotifications": {
"jobClassName": "Espo\\Classes\\Jobs\\SendEmailNotifications"
},
"ProcessWebhookQueue": {
"jobClassName": "Espo\\Classes\\Jobs\\ProcessWebhookQueue"
},
"CheckEmailAccounts": {
"isPreparable": true,
"preparatorClassName": "Espo\\Classes\\JobPreparators\\CheckEmailAccounts"
"preparatorClassName": "Espo\\Classes\\JobPreparators\\CheckEmailAccounts",
"jobClassName": "Espo\\Classes\\Jobs\\CheckEmailAccounts"
},
"CheckInboundEmails": {
"isPreparable": true,
"preparatorClassName": "Espo\\Classes\\JobPreparators\\CheckInboundEmails"
"preparatorClassName": "Espo\\Classes\\JobPreparators\\CheckInboundEmails",
"jobClassName": "Espo\\Classes\\Jobs\\CheckInboundEmails"
}
}