EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
job creator
Yuri Kuznetsovd466756520f8
diff --git a/application/Espo/Binding.php b/application/Espo/Binding.phpindex 03df41f07a..a02449bb8d 100644--- a/application/Espo/Binding.php+++ b/application/Espo/Binding.php@@ -255,6 +255,11 @@ class Binding implements BindingProcessor'Espo\\Core\\Utils\\Database\\ConfigDataProvider','Espo\\Core\\Utils\\Database\\DefaultConfigDataProvider');++ $binder->bindImplementation(+ 'Espo\\Core\\Job\\JobSchedulerCreator',+ 'Espo\\Core\\Job\\JobScheduler\\Creators\\EntityJobSchedulerCreator',+ );}private function bindMisc(Binder $binder): voiddiff --git a/application/Espo/Core/Job/JobScheduler.php b/application/Espo/Core/Job/JobScheduler.phpindex 2afcae86eb..d7afe127ed 100644--- a/application/Espo/Core/Job/JobScheduler.php+++ b/application/Espo/Core/Job/JobScheduler.php@@ -29,10 +29,8 @@namespace Espo\Core\Job;-use Espo\ORM\EntityManager;-use Espo\Core\Utils\DateTime;+use Espo\Core\Field\DateTime as DateTimeField;use Espo\Core\Job\Job\Data;-use Espo\Entities\Job as JobEntity;use ReflectionClass;use DateTimeInterface;@@ -42,11 +40,11 @@ use RuntimeException;use TypeError;/**- * Creates a job record in database.+ * Creates jobs in a queue.*/class JobScheduler{- /** @var ?class-string */+ /** @var ?class-string<Job|JobDataLess> */private ?string $className = null;private ?string $queue = null;private ?string $group = null;@@ -54,8 +52,9 @@ class JobSchedulerprivate ?DateTimeImmutable $time = null;private ?DateInterval $delay = null;- public function __construct(private EntityManager $entityManager)- {}+ public function __construct(+ private JobSchedulerCreator $creator,+ ) {}/*** A class name of the job. Should implement the `Job` interface.@@ -158,7 +157,7 @@ class JobSchedulerreturn $this;}- public function schedule(): JobEntity+ public function schedule(): void{if (!$this->className) {throw new RuntimeException("Class name is not set.");@@ -168,24 +167,26 @@ class JobSchedulerthrow new RuntimeException("Can't have both queue and group.");}- $time = $this->time ?? new DateTimeImmutable();+ $time = $this->time;- if ($this->delay) {+ if (!$this->time && $this->delay) {+ $time = new DateTimeImmutable();+ }++ if ($time && $this->delay) {$time = $time->add($this->delay);}$data = $this->data ?? Data::create();- /** @var JobEntity */- return $this->entityManager->createEntity(JobEntity::ENTITY_TYPE, [- 'name' => $this->className,- 'className' => $this->className,- 'queue' => $this->queue,- 'group' => $this->group,- 'targetType' => $data->getTargetType(),- 'targetId' => $data->getTargetId(),- 'data' => $data->getRaw(),- 'executeTime' => $time->format(DateTime::SYSTEM_DATE_TIME_FORMAT),- ]);+ $creatorData = new JobScheduler\Data(+ className: $this->className,+ queue: $this->queue,+ group: $this->group,+ data: $data,+ time: $time ? DateTimeField::fromDateTime($time) : null,+ );++ $this->creator->create($creatorData);}}diff --git a/application/Espo/Core/Job/JobScheduler/Creators/EntityJobSchedulerCreator.php b/application/Espo/Core/Job/JobScheduler/Creators/EntityJobSchedulerCreator.phpnew file mode 100644index 0000000000..def2504727--- /dev/null+++ b/application/Espo/Core/Job/JobScheduler/Creators/EntityJobSchedulerCreator.php@@ -0,0 +1,59 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://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 Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Job\JobScheduler\Creators;++use Espo\Core\Field\DateTime;+use Espo\Core\Job\JobScheduler;+use Espo\Core\Job\JobSchedulerCreator;+use Espo\Entities\Job;+use Espo\ORM\EntityManager;++class EntityJobSchedulerCreator implements JobSchedulerCreator+{+ public function __construct(+ private EntityManager $entityManager,+ ) {}++ public function create(JobScheduler\Data $data): void+ {+ $time = $data->time ?? DateTime::createNow();++ $this->entityManager->createEntity(Job::ENTITY_TYPE, [+ 'name' => $data->className,+ 'className' => $data->className,+ 'queue' => $data->queue,+ 'group' => $data->group,+ 'targetType' => $data->data->getTargetType(),+ 'targetId' => $data->data->getTargetId(),+ 'data' => $data->data->getRaw(),+ 'executeTime' => $time->toString(),+ ]);+ }+}diff --git a/application/Espo/Core/Job/JobScheduler/Data.php b/application/Espo/Core/Job/JobScheduler/Data.phpnew file mode 100644index 0000000000..c7788b603c--- /dev/null+++ b/application/Espo/Core/Job/JobScheduler/Data.php@@ -0,0 +1,49 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://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 Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Job\JobScheduler;++use Espo\Core\Field\DateTime;+use Espo\Core\Job\Job;+use Espo\Core\Job\Job\Data as JobData;+use Espo\Core\Job\JobDataLess;++readonly class Data+{+ /**+ * @param class-string<Job|JobDataLess> $className+ */+ public function __construct(+ public string $className,+ public ?string $queue,+ public ?string $group,+ public JobData $data,+ public ?DateTime $time,+ ) {}+}diff --git a/application/Espo/Core/Job/JobSchedulerCreator.php b/application/Espo/Core/Job/JobSchedulerCreator.phpnew file mode 100644index 0000000000..a42dae7f30--- /dev/null+++ b/application/Espo/Core/Job/JobSchedulerCreator.php@@ -0,0 +1,38 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://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 Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Job;++/**+ * @since 9.2.0+ */+interface JobSchedulerCreator+{+ public function create(JobScheduler\Data $data): void;+}diff --git a/tests/unit/Espo/Core/Job/JobSchedulerTest.php b/tests/unit/Espo/Core/Job/JobSchedulerTest.phpindex ac05daa0a4..52c3e024cc 100644--- a/tests/unit/Espo/Core/Job/JobSchedulerTest.php+++ b/tests/unit/Espo/Core/Job/JobSchedulerTest.php@@ -29,61 +29,48 @@namespace tests\unit\Espo\Core\Job;+use Espo\Core\Field\DateTime as DateTimeField;use Espo\Core\Job\JobScheduler;+use Espo\Core\Job\JobSchedulerCreator;use Espo\Core\Job\QueueName;-use Espo\Core\Utils\DateTime;use Espo\Core\Job\Job\Data;--use Espo\ORM\EntityManager;--use Espo\Entities\Job as JobEntity;-+use PHPUnit\Framework\TestCase;use tests\unit\testClasses\Core\Job\TestJob;-use tests\unit\testClasses\Core\Job\TestJobDataLess;use DateTimeImmutable;use DateInterval;-class JobSchedulerTest extends \PHPUnit\Framework\TestCase+class JobSchedulerTest extends TestCase{- private $entityManager;+ private ?JobSchedulerCreator $creator = null;protected function setUp(): void{- $this->entityManager = $this->createMock(EntityManager::class);+ $this->creator = $this->createMock(JobSchedulerCreator::class);}public function testSchedule1(): void{- $scheduler = new JobScheduler($this->entityManager);-- $jobEntity = $this->createMock(JobEntity::class);+ $scheduler = new JobScheduler($this->creator);$time = new DateTimeImmutable();$delay = DateInterval::createFromDateString('1 minute');- $this->entityManager+ $expectedData = new JobScheduler\Data(+ className: TestJob::class,+ queue: QueueName::Q0,+ group: null,+ data: new Data((object) ['test' => '1']),+ time: DateTimeField::fromDateTime($time)->addMinutes(1),+ );++ $this->creator->expects($this->once())- ->method('createEntity')- ->with(- JobEntity::ENTITY_TYPE,- [- 'name' => TestJob::class,- 'className' => TestJob::class,- 'queue' => QueueName::Q0,- 'group' => null,- 'data' => (object) [- 'test' => '1',- ],- 'executeTime' => $time->modify('+1 minute')->format(DateTime::SYSTEM_DATE_TIME_FORMAT),- 'targetId' => null,- 'targetType' => null,- ]- )- ->willReturn($jobEntity);-- $jobEntityReturned = $scheduler+ ->method('create')+ ->with($expectedData);++ $scheduler->setClassName(TestJob::class)->setQueue(QueueName::Q0)->setData([@@ -92,37 +79,26 @@ class JobSchedulerTest extends \PHPUnit\Framework\TestCase->setTime($time)->setDelay($delay)->schedule();-- $this->assertSame($jobEntityReturned, $jobEntity);}public function testSchedule2(): void{- $scheduler = new JobScheduler($this->entityManager);-- $jobEntity = $this->createMock(JobEntity::class);+ $scheduler = new JobScheduler($this->creator);$time = new DateTimeImmutable();- $this->entityManager+ $expectedData = new JobScheduler\Data(+ className: TestJob::class,+ queue: null,+ group: 'g-1',+ data: (new Data((object) ['test' => '1']))->withTargetType('TestType')->withTargetId('test-id'),+ time: DateTimeField::fromDateTime($time),+ );++ $this->creator->expects($this->once())- ->method('createEntity')- ->with(- JobEntity::ENTITY_TYPE,- [- 'name' => TestJob::class,- 'className' => TestJob::class,- 'queue' => null,- 'group' => 'g-1',- 'data' => (object) [- 'test' => '1',- ],- 'executeTime' => $time->format(DateTime::SYSTEM_DATE_TIME_FORMAT),- 'targetId' => 'test-id',- 'targetType' => 'TestType',- ]- )- ->willReturn($jobEntity);+ ->method('create')+ ->with($expectedData);$data = Data::create([@@ -131,40 +107,11 @@ class JobSchedulerTest extends \PHPUnit\Framework\TestCase->withTargetId('test-id')->withTargetType('TestType');- $jobEntityReturned = $scheduler+ $scheduler->setClassName(TestJob::class)->setGroup('g-1')->setData($data)->setTime($time)->schedule();-- $this->assertSame($jobEntityReturned, $jobEntity);- }-- public function testSchedule3(): void- {- $scheduler = new JobScheduler($this->entityManager);-- $jobEntity = $this->createMock(JobEntity::class);-- $this->entityManager- ->expects($this->once())- ->method('createEntity')- ->with(- JobEntity::ENTITY_TYPE,- $this->callback(- function (array $data): bool {- return is_string($data['executeTime']);- }- )- )- ->willReturn($jobEntity);--- $jobEntityReturned = $scheduler- ->setClassName(TestJobDataLess::class)- ->schedule();-- $this->assertSame($jobEntityReturned, $jobEntity);}}