feature/cierres (#25)
Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #25
This commit is contained in:
49
cli/src/Service/Job.php
Normal file
49
cli/src/Service/Job.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use DateInvalidTimeZoneException;
|
||||
use DateMalformedStringException;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Exception\MQTT as MQTTException;
|
||||
use Incoviba\Service\MQTT\MQTTInterface;
|
||||
|
||||
class Job
|
||||
{
|
||||
public function __construct(protected LoggerInterface $logger, protected MQTTInterface $mqttService) {}
|
||||
protected string $redisKey;
|
||||
|
||||
public function getPending(): int
|
||||
{
|
||||
try {
|
||||
return $this->mqttService->pending();
|
||||
} catch (MQTTException $exception) {
|
||||
$this->logger->warning($exception->getMessage(), ['exception' => $exception]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public function push(array $jobConfiguration): array
|
||||
{
|
||||
try {
|
||||
$now = (new DateTimeImmutable('now', new DateTimeZone($_ENV['TZ'] ?? 'America/Santiago')));
|
||||
} catch (DateMalformedStringException | DateInvalidTimeZoneException) {
|
||||
$now = new DateTimeImmutable();
|
||||
}
|
||||
$data = [
|
||||
'id' => $now->format('Uu'),
|
||||
'configuration' => $jobConfiguration,
|
||||
'executed' => false,
|
||||
'created_at' => $now->format('Y-m-d H:i:s'),
|
||||
'updated_at' => null,
|
||||
'retries' => 0
|
||||
];
|
||||
try {
|
||||
$this->mqttService->set(json_encode($data));
|
||||
} catch (MQTTException $exception) {
|
||||
$this->logger->warning($exception->getMessage(), ['exception' => $exception]);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user