View source
<?php
namespace Drupal\advancedqueue\Plugin\AdvancedQueue\Backend;
use Drupal\advancedqueue\Job;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Database extends BackendBase implements SupportsDeletingJobsInterface, SupportsListingJobsInterface, SupportsReleasingJobsInterface {
protected $connection;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TimeInterface $time, Connection $connection) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $time);
$this->connection = $connection;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('datetime.time'), $container
->get('database'));
}
public function createQueue() {
}
public function deleteQueue() {
$this->connection
->delete('advancedqueue')
->condition('queue_id', $this->queueId)
->execute();
}
public function cleanupQueue() {
$this->connection
->update('advancedqueue')
->fields([
'state' => Job::STATE_QUEUED,
'expires' => 0,
])
->condition('expires', 0, '<>')
->condition('expires', $this->time
->getCurrentTime(), '<')
->execute();
}
public function countJobs() {
$jobs = [
Job::STATE_QUEUED => 0,
Job::STATE_PROCESSING => 0,
Job::STATE_SUCCESS => 0,
Job::STATE_FAILURE => 0,
];
$query = 'SELECT state, COUNT(job_id) FROM {advancedqueue} WHERE queue_id = :queue_id GROUP BY state';
$counts = $this->connection
->query($query, [
':queue_id' => $this->queueId,
])
->fetchAllKeyed();
foreach ($counts as $state => $count) {
$jobs[$state] = $count;
}
return $jobs;
}
public function enqueueJob(Job $job, $delay = 0) {
$this
->enqueueJobs([
$job,
], $delay);
}
public function enqueueJobs(array $jobs, $delay = 0) {
if (count($jobs) > 1) {
$this->connection
->startTransaction();
}
foreach ($jobs as $job) {
$job
->setQueueId($this->queueId);
$job
->setState(Job::STATE_QUEUED);
if (!$job
->getAvailableTime()) {
$job
->setAvailableTime($this->time
->getCurrentTime() + $delay);
}
$fields = $job
->toArray();
unset($fields['id']);
$fields['payload'] = json_encode($fields['payload']);
$query = $this->connection
->insert('advancedqueue')
->fields($fields);
$job_id = $query
->execute();
$job
->setId($job_id);
}
}
public function retryJob(Job $job, $delay = 0) {
if ($job
->getState() != Job::STATE_FAILURE) {
throw new \InvalidArgumentException('Only failed jobs can be retried.');
}
$job
->setNumRetries($job
->getNumRetries() + 1);
$job
->setAvailableTime($this->time
->getCurrentTime() + $delay);
$job
->setState(Job::STATE_QUEUED);
$this
->updateJob($job);
}
public function claimJob() {
while (TRUE) {
$query = 'SELECT * FROM {advancedqueue}
WHERE queue_id = :queue_id AND state = :state AND available <= :now AND expires = 0
ORDER BY available, job_id ASC';
$params = [
':queue_id' => $this->queueId,
':state' => Job::STATE_QUEUED,
':now' => $this->time
->getCurrentTime(),
];
$job_definition = $this->connection
->queryRange($query, 0, 1, $params)
->fetchAssoc();
if (!$job_definition) {
return NULL;
}
$state = Job::STATE_PROCESSING;
$expires = $this->time
->getCurrentTime() + $this->configuration['lease_time'];
$update = $this->connection
->update('advancedqueue')
->fields([
'state' => $state,
'expires' => $expires,
])
->condition('job_id', $job_definition['job_id'])
->condition('expires', 0);
if ($update
->execute()) {
$job_definition['id'] = $job_definition['job_id'];
unset($job_definition['job_id']);
$job_definition['payload'] = json_decode($job_definition['payload'], TRUE);
$job_definition['state'] = $state;
$job_definition['expires'] = $expires;
return new Job($job_definition);
}
}
}
public function onSuccess(Job $job) {
$job
->setProcessedTime($this->time
->getCurrentTime());
$this
->updateJob($job);
}
public function onFailure(Job $job) {
$job
->setProcessedTime($this->time
->getCurrentTime());
$this
->updateJob($job);
}
public function releaseJob($job_id) {
$this->connection
->update('advancedqueue')
->fields([
'state' => Job::STATE_QUEUED,
'expires' => 0,
])
->condition('job_id', $job_id)
->execute();
}
public function deleteJob($job_id) {
$this->connection
->delete('advancedqueue')
->condition('job_id', $job_id)
->execute();
}
protected function updateJob(Job $job) {
$this->connection
->update('advancedqueue')
->fields([
'payload' => json_encode($job
->getPayload()),
'state' => $job
->getState(),
'message' => $job
->getMessage(),
'num_retries' => $job
->getNumRetries(),
'available' => $job
->getAvailableTime(),
'processed' => $job
->getProcessedTime(),
'expires' => $job
->getExpiresTime(),
])
->condition('job_id', $job
->getId())
->execute();
}
}