mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
ref
This commit is contained in:
@@ -34,17 +34,15 @@ namespace Espo\Core\Application\Runner;
|
||||
*/
|
||||
class Params
|
||||
{
|
||||
/** @var array<string,mixed> */
|
||||
/** @var array<string, mixed> */
|
||||
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<string,mixed> $data
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$obj = new self();
|
||||
|
||||
$obj->data = $data;
|
||||
|
||||
return $obj;
|
||||
|
||||
@@ -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<Runner|RunnerParameterized> $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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user