feature/cierres (#25)
Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: http://git.provm.cl/Incoviba/oficial/pulls/25
This commit is contained in:
51
app/src/Service/Worker/CheckExternal.php
Normal file
51
app/src/Service/Worker/CheckExternal.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Worker;
|
||||
|
||||
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;
|
||||
|
||||
class CheckExternal extends Ideal\Service implements Service\Worker
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
protected Service\Queue $queueService;
|
||||
|
||||
public function execute(Model\Job $job): bool
|
||||
{
|
||||
$configuration = $job->configuration;
|
||||
$serviceClass = $configuration['service'];
|
||||
if (!$this->container->has($serviceClass)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$service = $this->container->get($serviceClass);
|
||||
} catch (NotFoundExceptionInterface | ContainerExceptionInterface) {
|
||||
return false;
|
||||
}
|
||||
$method = $configuration['method'] ?? 'check';
|
||||
|
||||
if (!method_exists($service, $method)) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($this->queueService)) {
|
||||
$this->queueService = $this->container->get(Service\Queue::class);
|
||||
}
|
||||
if (isset($configuration['args'])) {
|
||||
$args = $configuration['args'];
|
||||
$queues = call_user_func_array([$service, $method], $args);
|
||||
} else {
|
||||
$queues = call_user_func([$service, $method]);
|
||||
}
|
||||
foreach ($queues as $queue) {
|
||||
$this->queueService->enqueue($queue);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
17
app/src/Service/Worker/Dummy.php
Normal file
17
app/src/Service/Worker/Dummy.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Worker;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Dummy extends Ideal\Service implements Service\Worker
|
||||
{
|
||||
|
||||
public function execute(Model\Job $job): bool
|
||||
{
|
||||
$configuration = $job->configuration;
|
||||
$this->logger->info('Dummy worker executed', ['configuration' => $configuration]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
62
app/src/Service/Worker/Request.php
Normal file
62
app/src/Service/Worker/Request.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Worker;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||
use Incoviba\Service\Worker;
|
||||
use Psr\Http\Client\ClientExceptionInterface;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Request extends Ideal\Service implements Worker
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected ClientInterface $client)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
protected RequestInterface $request;
|
||||
public function setRequest(RequestInterface $request): self
|
||||
{
|
||||
$this->request = $request;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model\Job $job
|
||||
* @return bool
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
public function execute(Model\Job $job): bool
|
||||
{
|
||||
$url = $job->configuration['url'] ?? $job->configuration['action'];
|
||||
$method = strtolower($job->configuration['method']) ?? 'get';
|
||||
$body = $job->configuration['body'];
|
||||
|
||||
try {
|
||||
$response = $this->client->{$method}($url, [
|
||||
'json' => $body,
|
||||
'headers' => [
|
||||
'Authorization' => $this->request->getHeaderLine('Authorization')
|
||||
]
|
||||
]);
|
||||
} catch (ClientExceptionInterface $exception) {
|
||||
throw new EmptyResponse($url, $exception);
|
||||
}
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
if ((int) floor($statusCode / 100) !== 2) {
|
||||
throw new EmptyResponse($url);
|
||||
}
|
||||
if ($statusCode !== 204) {
|
||||
$contents = $response->getBody()->getContents();
|
||||
$data = json_decode($contents, true);
|
||||
if (!isset($data['success']) or !$data['success']) {
|
||||
throw new EmptyResponse($url);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
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