job scheduler job data less support

This commit is contained in:
Yuri Kuznetsov
2023-07-13 10:56:16 +03:00
parent a23b28bee9
commit 237e39f495
5 changed files with 65 additions and 5 deletions
+11 -1
View File
@@ -212,9 +212,12 @@ class JobRunner
private function runJobWithClassName(JobEntity $jobEntity): void
{
/** @var class-string<Job|JobDataLess> $className */
$className = $jobEntity->getClassName();
if (!$className) {
throw new RuntimeException("No className in job {$jobEntity->getId()}.");
}
$job = $this->jobFactory->createByClassName($className);
$this->runJob($job, $jobEntity);
@@ -222,9 +225,16 @@ class JobRunner
/**
* @param Job|JobDataLess $job
* @internal Native type is not used for bc.
*/
private function runJob($job, JobEntity $jobEntity): void
{
if ($job instanceof JobDataLess) {
$job->run();
return;
}
$data = Data::create($jobEntity->getData())
->withTargetId($jobEntity->getTargetId())
->withTargetType($jobEntity->getTargetType());
+6 -3
View File
@@ -60,7 +60,7 @@ class JobScheduler
/**
* A class name of the job. Should implement the `Job` interface.
*
* @param class-string<Job> $className
* @param class-string<Job|JobDataLess> $className
*/
public function setClassName(string $className): self
{
@@ -70,8 +70,11 @@ class JobScheduler
$class = new ReflectionClass($className);
if (!$class->implementsInterface(Job::class)) {
throw new RuntimeException("Class '{$className}' does not implement 'Job' interface.");
if (
!$class->implementsInterface(Job::class) &&
!$class->implementsInterface(JobDataLess::class)
) {
throw new RuntimeException("Class '{$className}' does not implement 'Job' or 'JobDataLess' interface.");
}
$this->className = $className;
+4
View File
@@ -29,7 +29,9 @@
namespace Espo\Entities;
use Espo\Core\Job\Job as JobJob;
use Espo\Core\Job\Job\Status;
use Espo\Core\Job\JobDataLess;
use Espo\Core\ORM\Entity;
use Espo\Core\Utils\DateTime as DateTimeUtil;
@@ -113,6 +115,8 @@ class Job extends Entity
/**
* Get a class name.
*
* @return ?class-string<JobJob|JobDataLess>
*/
public function getClassName(): ?string
{
@@ -39,6 +39,7 @@ use Espo\ORM\EntityManager;
use Espo\Entities\Job as JobEntity;
use tests\unit\testClasses\Core\Job\TestJob;
use tests\unit\testClasses\Core\Job\TestJobDataLess;
use DateTimeImmutable;
use DateInterval;
@@ -161,7 +162,7 @@ class JobSchedulerTest extends \PHPUnit\Framework\TestCase
$jobEntityReturned = $scheduler
->setClassName(TestJob::class)
->setClassName(TestJobDataLess::class)
->schedule();
$this->assertSame($jobEntityReturned, $jobEntity);
@@ -0,0 +1,42 @@
<?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 tests\unit\testClasses\Core\Job;
use Espo\Core\Job\JobDataLess;
class TestJobDataLess implements JobDataLess
{
/**
* @inheritDoc
*/
public function run(): void
{}
}