Service Worker, corre metodos de servicios asincronicamente, requiere parametros escalares y no retornar mas que bool o void

This commit is contained in:
Juan Pablo Vial
2025-06-24 11:17:16 -04:00
parent 64791d1fc5
commit ecc67a43c8

View File

@ -0,0 +1,50 @@
<?php
namespace Incoviba\Service\Worker;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Model;
use Incoviba\Service\Worker;
class Service extends Ideal\Service implements Worker
{
public function __construct(protected ContainerInterface $container ,LoggerInterface $logger)
{
parent::__construct($logger);
}
public function execute(Model\Job $job): bool
{
$configuration = $job->configuration;
$serviceClass = $configuration['service'];
$method = $configuration['method'];
$params = $configuration['params'] ?? [];
try {
$service = $this->container->get($serviceClass);
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
$this->logger->error($exception->getMessage());
return false;
}
try {
$result = call_user_func_array([$service, $method], $params);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage(), [
'Worker' => __CLASS__,
'job_id' => $job->id,
'service' => $serviceClass,
'method' => $method,
'params' => $params,
'exception' => $exception
]);
return false;
}
if (is_bool($result)) {
return $result;
}
return true;
}
}