diff --git a/application/Espo/Core/Application/Runner/Params.php b/application/Espo/Core/Application/Runner/Params.php index 6b22e6f0e7..50675301cf 100644 --- a/application/Espo/Core/Application/Runner/Params.php +++ b/application/Espo/Core/Application/Runner/Params.php @@ -34,17 +34,15 @@ namespace Espo\Core\Application\Runner; */ class Params { - /** @var array */ + /** @var array */ private $data = []; public function __construct() {} /** * Get a parameter value. - * - * @return mixed */ - public function get(string $name) + public function get(string $name): mixed { return $this->data[$name] ?? null; } @@ -59,13 +57,10 @@ class Params /** * Clone with a parameter value. - * - * @param mixed $value */ - public function with(string $name, $value): self + public function with(string $name, mixed $value): self { $obj = clone $this; - $obj->data[$name] = $value; return $obj; @@ -74,12 +69,11 @@ class Params /** * Create from an associative array. * - * @param array $data + * @param array $data */ public static function fromArray(array $data): self { $obj = new self(); - $obj->data = $data; return $obj; diff --git a/application/Espo/Core/Application/RunnerRunner.php b/application/Espo/Core/Application/RunnerRunner.php index a947de4911..573ac19e51 100644 --- a/application/Espo/Core/Application/RunnerRunner.php +++ b/application/Espo/Core/Application/RunnerRunner.php @@ -42,19 +42,11 @@ use ReflectionClass; */ class RunnerRunner { - private Log $log; - private ApplicationUser $applicationUser; - private InjectableFactory $injectableFactory; - public function __construct( - Log $log, - ApplicationUser $applicationUser, - InjectableFactory $injectableFactory - ) { - $this->log = $log; - $this->applicationUser = $applicationUser; - $this->injectableFactory = $injectableFactory; - } + private Log $log, + private ApplicationUser $applicationUser, + private InjectableFactory $injectableFactory + ) {} /** * @param class-string $className @@ -72,7 +64,7 @@ class RunnerRunner if ( $class->getStaticPropertyValue('cli', false) && - substr(php_sapi_name() ?: '', 0, 3) !== 'cli' + !str_starts_with(php_sapi_name() ?: '', 'cli') ) { throw new RunnerException("Can be run only via CLI."); } @@ -83,19 +75,12 @@ class RunnerRunner $runner = $this->injectableFactory->create($className); - if ($runner instanceof Runner) { - $runner->run(); - - return; - } - if ($runner instanceof RunnerParameterized) { $runner->run($params ?? Params::create()); return; } - /** @phpstan-ignore-next-line */ - throw new RunnerException("Class should implement Runner or RunnerParameterized interface."); + $runner->run(); } } diff --git a/application/Espo/Core/Job/JobManager.php b/application/Espo/Core/Job/JobManager.php index 19276fe296..b2fbdf093a 100644 --- a/application/Espo/Core/Job/JobManager.php +++ b/application/Espo/Core/Job/JobManager.php @@ -129,9 +129,6 @@ class JobManager /** * Run a specific job by ID. A job status should be set to 'Ready'. - * - * @throws Error - * @throws Throwable */ public function runJobById(string $id): void { diff --git a/application/Espo/Core/Job/JobRunner.php b/application/Espo/Core/Job/JobRunner.php index a1d7174dd1..f3c8fb905d 100644 --- a/application/Espo/Core/Job/JobRunner.php +++ b/application/Espo/Core/Job/JobRunner.php @@ -33,7 +33,6 @@ use Espo\Core\Exceptions\Error; use Espo\Core\ORM\EntityManager; use Espo\Core\ServiceFactory; use Espo\Core\Utils\Config; -use Espo\Core\Utils\DateTime as DateTimeUtil; use Espo\Core\Utils\Log; use Espo\Core\Utils\System; use Espo\Core\Job\Job\Data; @@ -41,6 +40,7 @@ use Espo\Core\Job\Job\Status; use Espo\Entities\Job as JobEntity; use LogicException; +use RuntimeException; use Throwable; class JobRunner @@ -80,24 +80,22 @@ class JobRunner /** * Run a job by ID. A job must have status 'Ready'. * Used when running jobs in parallel processes. - * - * @throws Error */ public function runById(string $id): void { if ($id === '') { - throw new Error(); + throw new RuntimeException("Empty job ID."); } /** @var ?JobEntity $jobEntity */ $jobEntity = $this->entityManager->getEntityById(JobEntity::ENTITY_TYPE, $id); if (!$jobEntity) { - throw new Error("Job '{$id}' not found."); + throw new RuntimeException("Job '{$id}' not found."); } if ($jobEntity->getStatus() !== Status::READY) { - throw new Error("Can't run job '{$id}' with no status Ready."); + throw new RuntimeException("Can't run job '{$id}' with not Ready status."); } $this->setJobRunning($jobEntity); @@ -157,10 +155,10 @@ class JobRunner $status = $isSuccess ? Status::SUCCESS : Status::FAILED; - $jobEntity->set('status', $status); + $jobEntity->setStatus($status); if ($isSuccess) { - $jobEntity->set('executedAt', DateTimeUtil::getSystemNowString()); + $jobEntity->setExecutedAtNow(); } $this->entityManager->saveEntity($jobEntity); @@ -271,11 +269,11 @@ class JobRunner private function setJobRunning(JobEntity $jobEntity): void { if (!$jobEntity->getStartedAt()) { - $jobEntity->set('startedAt', DateTimeUtil::getSystemNowString()); + $jobEntity->setStartedAtNow(); } - $jobEntity->set('status', Status::RUNNING); - $jobEntity->set('pid', System::getPid()); + $jobEntity->setStatus(Status::RUNNING); + $jobEntity->setPid(System::getPid()); $this->entityManager->saveEntity($jobEntity); } diff --git a/application/Espo/Core/Job/JobTask.php b/application/Espo/Core/Job/JobTask.php index 08c7b8f127..954d2589d4 100644 --- a/application/Espo/Core/Job/JobTask.php +++ b/application/Espo/Core/Job/JobTask.php @@ -60,7 +60,7 @@ class JobTask extends AsyncTask { $app = new Application(); - $params = RunnerParams::fromArray(['id' => $this->jobId]); + $params = RunnerParams::create()->with('id', $this->jobId); try { $app->run(JobRunner::class, $params); diff --git a/application/Espo/Entities/Job.php b/application/Espo/Entities/Job.php index d9f2f13ba2..d7186fb54c 100644 --- a/application/Espo/Entities/Job.php +++ b/application/Espo/Entities/Job.php @@ -202,7 +202,17 @@ class Job extends Entity */ public function setStartedAtNow(): self { - $this->set('startedAt', date(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT)); + $this->set('startedAt', DateTimeUtil::getSystemNowString()); + + return $this; + } + + /** + * Set executed-at to now. + */ + public function setExecutedAtNow(): self + { + $this->set('executedAt', DateTimeUtil::getSystemNowString()); return $this; }