From 237e39f495a05417bd0c175164a3f67d7cdea5c1 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 13 Jul 2023 10:56:16 +0300 Subject: [PATCH] job scheduler job data less support --- application/Espo/Core/Job/JobRunner.php | 12 +++++- application/Espo/Core/Job/JobScheduler.php | 9 ++-- application/Espo/Entities/Job.php | 4 ++ tests/unit/Espo/Core/Job/JobSchedulerTest.php | 3 +- .../testClasses/Core/Job/TestJobDataLess.php | 42 +++++++++++++++++++ 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tests/unit/testClasses/Core/Job/TestJobDataLess.php diff --git a/application/Espo/Core/Job/JobRunner.php b/application/Espo/Core/Job/JobRunner.php index f3c8fb905d..fa9ebc54c0 100644 --- a/application/Espo/Core/Job/JobRunner.php +++ b/application/Espo/Core/Job/JobRunner.php @@ -212,9 +212,12 @@ class JobRunner private function runJobWithClassName(JobEntity $jobEntity): void { - /** @var class-string $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()); diff --git a/application/Espo/Core/Job/JobScheduler.php b/application/Espo/Core/Job/JobScheduler.php index ef67e48a25..14f2bfd594 100644 --- a/application/Espo/Core/Job/JobScheduler.php +++ b/application/Espo/Core/Job/JobScheduler.php @@ -60,7 +60,7 @@ class JobScheduler /** * A class name of the job. Should implement the `Job` interface. * - * @param class-string $className + * @param class-string $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; diff --git a/application/Espo/Entities/Job.php b/application/Espo/Entities/Job.php index d7186fb54c..187b24fda6 100644 --- a/application/Espo/Entities/Job.php +++ b/application/Espo/Entities/Job.php @@ -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 */ public function getClassName(): ?string { diff --git a/tests/unit/Espo/Core/Job/JobSchedulerTest.php b/tests/unit/Espo/Core/Job/JobSchedulerTest.php index 99a9e94dca..ea09df4c8a 100644 --- a/tests/unit/Espo/Core/Job/JobSchedulerTest.php +++ b/tests/unit/Espo/Core/Job/JobSchedulerTest.php @@ -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); diff --git a/tests/unit/testClasses/Core/Job/TestJobDataLess.php b/tests/unit/testClasses/Core/Job/TestJobDataLess.php new file mode 100644 index 0000000000..137ec03eeb --- /dev/null +++ b/tests/unit/testClasses/Core/Job/TestJobDataLess.php @@ -0,0 +1,42 @@ +