Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #25
50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?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;
|
|
}
|
|
}
|