66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
<?php
|
|
namespace Incoviba\Service\MQTT;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Pheanstalk as PBA;
|
|
use Incoviba\Service;
|
|
|
|
class Pheanstalk extends Service implements MQTTInterface
|
|
{
|
|
public function __construct(LoggerInterface $logger, protected PBA\Pheanstalk $client, string $tubeName = 'default')
|
|
{
|
|
parent::__construct($logger);
|
|
$this->tube = new PBA\Values\TubeName($tubeName);
|
|
}
|
|
|
|
protected PBA\Values\TubeName $tube;
|
|
|
|
public function set(string $payload, int $delay = 0): self
|
|
{
|
|
$this->client->useTube($this->tube);
|
|
$this->client->put($payload, $delay);
|
|
return $this;
|
|
}
|
|
|
|
public function pending(): int
|
|
{
|
|
$stats = $this->client->statsTube($this->tube);
|
|
return $stats->currentJobsReady;
|
|
}
|
|
|
|
public function exists(?int $jobId = null): bool
|
|
{
|
|
return $this->pending() > 0;
|
|
}
|
|
protected int $currentJobId;
|
|
public function get(?int $jobId = null): string
|
|
{
|
|
$this->client->watch($this->tube);
|
|
if ($jobId !== null) {
|
|
$jobId = new PBA\Values\JobId($jobId);
|
|
$job = $this->client->reserveJob($jobId);
|
|
} else {
|
|
$job = $this->client->reserve();
|
|
}
|
|
$this->currentJobId = $job->getId();
|
|
return $job->getData();
|
|
}
|
|
|
|
public function update(string $newPayload, ?int $jobId = null): self
|
|
{
|
|
$this->remove($jobId);
|
|
$this->set($newPayload);
|
|
return $this;
|
|
}
|
|
|
|
public function remove(?int $jobId = null): self
|
|
{
|
|
if ($jobId === null) {
|
|
$jobId = $this->currentJobId;
|
|
}
|
|
$this->client->watch($this->tube);
|
|
$this->client->delete(new PBA\Values\JobId($jobId));
|
|
return $this;
|
|
}
|
|
}
|