Service Worker, corre metodos de servicios asincronicamente, requiere parametros escalares y no retornar mas que bool o void
This commit is contained in:
50
app/src/Service/Worker/Service.php
Normal file
50
app/src/Service/Worker/Service.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user