mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
refactoring
This commit is contained in:
@@ -47,16 +47,6 @@ use Throwable;
|
||||
*/
|
||||
class JobManager
|
||||
{
|
||||
const PENDING = 'Pending';
|
||||
|
||||
const READY = 'Ready';
|
||||
|
||||
const RUNNING = 'Running';
|
||||
|
||||
const SUCCESS = 'Success';
|
||||
|
||||
const FAILED = 'Failed';
|
||||
|
||||
private $useProcessPool = false;
|
||||
|
||||
protected $lastRunTimeFile = 'data/cache/application/cronLastRunTime.php';
|
||||
|
||||
@@ -101,7 +101,7 @@ class JobRunner
|
||||
throw new Error("Job {$id} not found.");
|
||||
}
|
||||
|
||||
if ($job->getStatus() !== JobManager::READY) {
|
||||
if ($job->getStatus() !== JobStatus::READY) {
|
||||
throw new Error("Can't run job {$id} with no status Ready.");
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ class JobRunner
|
||||
$job->set('startedAt', date('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
$job->set('status', JobManager::RUNNING);
|
||||
$job->set('status', JobStatus::RUNNING);
|
||||
$job->set('pid', System::getPid());
|
||||
|
||||
$this->entityManager->saveEntity($job);
|
||||
@@ -154,7 +154,7 @@ class JobRunner
|
||||
}
|
||||
}
|
||||
|
||||
$status = $isSuccess ? JobManager::SUCCESS : JobManager::FAILED;
|
||||
$status = $isSuccess ? JobStatus::SUCCESS : JobStatus::FAILED;
|
||||
|
||||
$job->set('status', $status);
|
||||
|
||||
|
||||
43
application/Espo/Core/Job/JobStatus.php
Normal file
43
application/Espo/Core/Job/JobStatus.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?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\Core\Job;
|
||||
|
||||
class JobStatus
|
||||
{
|
||||
public const PENDING = 'Pending';
|
||||
|
||||
public const READY = 'Ready';
|
||||
|
||||
public const RUNNING = 'Running';
|
||||
|
||||
public const SUCCESS = 'Success';
|
||||
|
||||
public const FAILED = 'Failed';
|
||||
}
|
||||
@@ -139,10 +139,10 @@ class QueueProcessor
|
||||
$job->set('startedAt', date('Y-m-d H:i:s'));
|
||||
|
||||
if ($useProcessPool) {
|
||||
$job->set('status', JobManager::READY);
|
||||
$job->set('status', JobStatus::READY);
|
||||
}
|
||||
else {
|
||||
$job->set('status', JobManager::RUNNING);
|
||||
$job->set('status', JobStatus::RUNNING);
|
||||
$job->set('pid', System::getPid());
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ class QueueUtil
|
||||
return false;
|
||||
}
|
||||
|
||||
return $job->get('status') === JobManager::PENDING;
|
||||
return $job->get('status') === JobStatus::PENDING;
|
||||
}
|
||||
|
||||
public function getPendingJobList(?string $queue = null, int $limit = 0): Collection
|
||||
@@ -94,7 +94,7 @@ class QueueUtil
|
||||
'data',
|
||||
])
|
||||
->where([
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
'executeTime<=' => date('Y-m-d H:i:s'),
|
||||
'queue' => $queue,
|
||||
])
|
||||
@@ -115,7 +115,7 @@ class QueueUtil
|
||||
|
||||
$where = [
|
||||
'scheduledJobId' => $scheduledJobId,
|
||||
'status' => [JobManager::RUNNING, JobManager::READY],
|
||||
'status' => [JobStatus::RUNNING, JobStatus::READY],
|
||||
];
|
||||
|
||||
if ($targetId && $targetType) {
|
||||
@@ -165,10 +165,10 @@ class QueueUtil
|
||||
->where([
|
||||
'scheduledJobId' => $scheduledJobId,
|
||||
'status' => [ // This forces usage of an appropriate index.
|
||||
JobManager::PENDING,
|
||||
JobManager::READY,
|
||||
JobManager::RUNNING,
|
||||
JobManager::SUCCESS,
|
||||
JobStatus::PENDING,
|
||||
JobStatus::READY,
|
||||
JobStatus::RUNNING,
|
||||
JobStatus::SUCCESS,
|
||||
],
|
||||
'executeTime>=' => $fromString,
|
||||
'executeTime<=' => $toString,
|
||||
@@ -184,7 +184,7 @@ class QueueUtil
|
||||
->getRDBRepository('Job')
|
||||
->where([
|
||||
'scheduledJobId' => $scheduledJobId,
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
])
|
||||
->count();
|
||||
|
||||
@@ -220,7 +220,7 @@ class QueueUtil
|
||||
'startedAt',
|
||||
])
|
||||
->where([
|
||||
'status' => JobManager::RUNNING,
|
||||
'status' => JobStatus::RUNNING,
|
||||
'startedAt<' => $dateTimeThreshold,
|
||||
])
|
||||
->find();
|
||||
@@ -255,7 +255,7 @@ class QueueUtil
|
||||
'startedAt',
|
||||
])
|
||||
->where([
|
||||
'status' => JobManager::READY,
|
||||
'status' => JobStatus::READY,
|
||||
'startedAt<' => $dateTimeThreshold,
|
||||
])
|
||||
->find();
|
||||
@@ -287,7 +287,7 @@ class QueueUtil
|
||||
'startedAt'
|
||||
])
|
||||
->where([
|
||||
'status' => JobManager::RUNNING,
|
||||
'status' => JobStatus::RUNNING,
|
||||
'executeTime<' => $dateTimeThreshold,
|
||||
])
|
||||
->find();
|
||||
@@ -323,7 +323,7 @@ class QueueUtil
|
||||
->update()
|
||||
->in('Job')
|
||||
->set([
|
||||
'status' => JobManager::FAILED,
|
||||
'status' => JobStatus::FAILED,
|
||||
'attempts' => 0,
|
||||
])
|
||||
->where([
|
||||
@@ -340,7 +340,7 @@ class QueueUtil
|
||||
|
||||
$this->scheduleUtil->addLogRecord(
|
||||
$job->get('scheduledJobId'),
|
||||
JobManager::FAILED,
|
||||
JobStatus::FAILED,
|
||||
$job->get('startedAt'),
|
||||
$job->get('targetId'),
|
||||
$job->get('targetType')
|
||||
@@ -358,7 +358,7 @@ class QueueUtil
|
||||
->select(['scheduledJobId'])
|
||||
->where([
|
||||
'scheduledJobId!=' => null,
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
'executeTime<=' => date('Y-m-d H:i:s'),
|
||||
'targetId' => null,
|
||||
])
|
||||
@@ -385,7 +385,7 @@ class QueueUtil
|
||||
->select(['id'])
|
||||
->where([
|
||||
'scheduledJobId' => $scheduledJobId,
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
])
|
||||
->order('executeTime')
|
||||
->limit(0, 1000)
|
||||
@@ -426,7 +426,7 @@ class QueueUtil
|
||||
'failedAttempts'
|
||||
])
|
||||
->where([
|
||||
'status' => JobManager::FAILED,
|
||||
'status' => JobStatus::FAILED,
|
||||
'executeTime<=' => date('Y-m-d H:i:s'),
|
||||
'attempts>' => 0,
|
||||
])
|
||||
@@ -437,7 +437,7 @@ class QueueUtil
|
||||
$attempts = $job->get('attempts');
|
||||
|
||||
$job->set([
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
'attempts' => $attempts - 1,
|
||||
'failedAttempts' => $failedAttempts + 1,
|
||||
]);
|
||||
|
||||
@@ -173,7 +173,7 @@ class ScheduleProcessor
|
||||
|
||||
$this->entityManager->createEntity('Job', [
|
||||
'name' => $scheduledJob->getName(),
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
'scheduledJobId' => $id,
|
||||
'executeTime' => $executeTime,
|
||||
]);
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Espo\Jobs;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
|
||||
use Espo\Core\{
|
||||
Job\JobManager,
|
||||
Job\JobStatus,
|
||||
Job\JobTargeted,
|
||||
ServiceFactory,
|
||||
ORM\EntityManager,
|
||||
@@ -97,7 +97,7 @@ class CheckEmailAccounts implements JobTargeted
|
||||
->getRepository('Job')
|
||||
->where([
|
||||
'scheduledJobId' => $scheduledJob->id,
|
||||
'status' => [JobManager::RUNNING, JobManager::READY],
|
||||
'status' => [JobStatus::RUNNING, JobStatus::READY],
|
||||
'targetType' => 'EmailAccount',
|
||||
'targetId' => $entity->id,
|
||||
])
|
||||
@@ -111,7 +111,7 @@ class CheckEmailAccounts implements JobTargeted
|
||||
->getRepository('Job')
|
||||
->where([
|
||||
'scheduledJobId' => $scheduledJob->id,
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
'targetType' => 'EmailAccount',
|
||||
'targetId' => $entity->id,
|
||||
])
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Espo\Jobs;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
|
||||
use Espo\Core\{
|
||||
Job\JobManager,
|
||||
Job\JobStatus,
|
||||
Job\JobTargeted,
|
||||
ServiceFactory,
|
||||
ORM\EntityManager,
|
||||
@@ -95,7 +95,7 @@ class CheckInboundEmails implements JobTargeted
|
||||
->getRepository('Job')
|
||||
->where([
|
||||
'scheduledJobId' => $scheduledJob->id,
|
||||
'status' => [JobManager::RUNNING, JobManager::READY],
|
||||
'status' => [JobStatus::RUNNING, JobStatus::READY],
|
||||
'targetType' => 'InboundEmail',
|
||||
'targetId' => $entity->id,
|
||||
])
|
||||
@@ -109,7 +109,7 @@ class CheckInboundEmails implements JobTargeted
|
||||
->getRepository('Job')
|
||||
->where([
|
||||
'scheduledJobId' => $scheduledJob->id,
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
'targetType' => 'InboundEmail',
|
||||
'targetId' => $entity->id,
|
||||
])
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace Espo\Repositories;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use Espo\Core\Job\JobManager;
|
||||
use Espo\Core\Job\JobStatus;
|
||||
|
||||
class ScheduledJob extends \Espo\Core\Repositories\Database
|
||||
{
|
||||
@@ -46,7 +46,7 @@ class ScheduledJob extends \Espo\Core\Repositories\Database
|
||||
->getRepository('Job')
|
||||
->where([
|
||||
'scheduledJobId' => $entity->id,
|
||||
'status' => JobManager::PENDING,
|
||||
'status' => JobStatus::PENDING,
|
||||
])
|
||||
->find();
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace tests\integration\Espo\Core\Job;
|
||||
|
||||
use Espo\Core\{
|
||||
Job\JobManager,
|
||||
Job\JobStatus,
|
||||
ORM\EntitManager,
|
||||
};
|
||||
|
||||
@@ -66,21 +67,21 @@ class JobTest extends \tests\integration\Core\BaseTestCase
|
||||
|
||||
$jobReloaded = $this->entityManager->getEntity('Job', $job->id);
|
||||
|
||||
$this->assertEquals(JobManager::SUCCESS, $jobReloaded->getStatus());
|
||||
$this->assertEquals(JobStatus::SUCCESS, $jobReloaded->getStatus());
|
||||
}
|
||||
|
||||
public function testRunJobById() : void
|
||||
{
|
||||
$job = $this->entityManager->createEntity('Job', [
|
||||
'job' => 'Dummy',
|
||||
'status' => JobManager::READY,
|
||||
'status' => JobStatus::READY,
|
||||
]);
|
||||
|
||||
$this->jobManager->runJobById($job->id);
|
||||
|
||||
$jobReloaded = $this->entityManager->getEntity('Job', $job->id);
|
||||
|
||||
$this->assertEquals(JobManager::SUCCESS, $jobReloaded->getStatus());
|
||||
$this->assertEquals(JobStatus::SUCCESS, $jobReloaded->getStatus());
|
||||
}
|
||||
|
||||
public function testRunJobByEntity() : void
|
||||
@@ -93,6 +94,6 @@ class JobTest extends \tests\integration\Core\BaseTestCase
|
||||
|
||||
$jobReloaded = $this->entityManager->getEntity('Job', $job->id);
|
||||
|
||||
$this->assertEquals(JobManager::SUCCESS, $jobReloaded->getStatus());
|
||||
$this->assertEquals(JobStatus::SUCCESS, $jobReloaded->getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user