Reset job.

@param string $queue

@return bool

@throws JobConfigurationException
343887
public function reset($queue)
    {
        if (!function_exists('posix_getsid')) {
            throw JobConfigurationException::create('Function posix_getsid don\'t exists');
        }

        $configurations = $this->configurationManager->findPotentialDeadJobs($queue);

        foreach ($configurations as $configuration) {
            if ($this->restoreManager->reset($configuration)) {
                return true;
            }
        }

        return false;
    }
479086
public function deleteReserved($queue, $id)
    {
        $this->database->trans_begin();

        if ($this->getQueryBuilder()->from($this->table)->lockForUpdate()->find($id)) {
            $this->getQueryBuilder()->from($this->table)->where('id', $id)->delete();
        }

        $this->database->trans_commit();
    }
1170239
public function pop($queue = null)
    {
        $queue = $this->getQueue($queue);

        $job = $this->pheanstalk->watchOnly($queue)->reserve(0);

        if ($job instanceof PheanstalkJob) {
            return new BeanstalkdJob(
                $this->container, $this->pheanstalk, $job, $this->connectionName, $queue
            );
        }
    }
1010220
public function deleteReserved($queue, $job)
    {
        parent::deleteReserved($queue, $job);

        $this->event($this->getQueue($queue), new JobDeleted($job, $job->getReservedJob()));
    }
133083
protected function retrieveNextJob($queue)
    {
        if (!is_null($this->blockFor)) {
            return $this->blockingPop($queue);
        }

        $job      = $this->redis->lpop($queue);
        $reserved = false;

        if ($job) {
            $reserved = json_decode($job);
            $reserved['attempts']++;
            $reserved = json_encode($reserved);
            $this->redis->zAdd($queue . ':reserved', $this->availableAt($this->retryAfter), $reserved);
        }

        return [$job, $reserved];
    }
533596
protected function releaseJobsThatHaveBeenReservedTooLong($queue)
	{
		$expired = Carbon::now()->subSeconds($this->expire)->getTimestamp();

		$this->database->table($this->table)
					->where('queue', $this->getQueue($queue))
					->where('reserved', 1)
					->where('reserved_at', '<=', $expired)
					->update([
						'reserved' => 0,
						'reserved_at' => null,
						'attempts' => new Expression('attempts + 1'),
					]);
	}
684647
public function set(string $data, string $queue = 'default')
    {
        return $this->redis->rPush($queue, $data);
    }
741766
public function isPendingCountOk($queue)
    {
        if ($queue->alert_pending_jobs_greater_than < 1) {
            return true;
        }

        if ($this->getPendingJobsCount($queue) <= $queue->alert_pending_jobs_greater_than) {
            if ($this->eye->status()->isSick('queue_pending_jobs_'.$queue->id)) {
                $this->eye->notifier()->alert(new PendingCountOk(['queue' => $queue, 'pending_job_count' => $this->getPendingJobsCount($queue)]));
            }

            $this->eye->status()->setHealthy('queue_pending_jobs_'.$queue->id);

            return true;
        }

        if ($this->eye->status()->isHealthy('queue_pending_jobs_'.$queue->id)) {
            $this->eye->notifier()->alert(new PendingCountExceeded(['queue' => $queue, 'pending_job_count' => $this->getPendingJobsCount($queue)]));
        }

        $this->eye->status()->setSick('queue_pending_jobs_'.$queue->id);

        return false;
    }
1286780
public function addQueue($queue)
    {
        if(!in_array($queue, $this->queuesWorkedOn)) {
            $this->queuesWorkedOn[] = $queue;
        }

        return $this;
    }
269186
protected function getNextJob($connector, $queue)
    {
        try {
            foreach (explode(',', $queue) as $queue) {
                if (!is_null($job = $connector->pop($queue))) {
                    return $job;
                }
            }
        } catch (Exception | Throwable $e) {
            $this->handle->report($e);
            $this->sleep(1);
        }
    }
216350
protected function setQueue(Queue $queue)
    {
        $this->queue = $queue;
        $this->queue->setProducer($this);

        return $this;
    }
75134
public function getQueue(string $queueName): QueueInterface
    {
        if (isset($this->queues[$queueName])) {
            return $this->queues[$queueName];
        }

        $queueSettings = $this->getQueueSettings($queueName);

        if (!isset($queueSettings['className'])) {
            throw new JobQueueException(sprintf('Option className for queue "%s" is not configured', $queueName), 1334147126);
        }

        $queueObjectName = $queueSettings['className'];
        if (!class_exists($queueObjectName)) {
            throw new JobQueueException(sprintf('Configured class "%s" for queue "%s" does not exist', $queueObjectName, $queueName), 1445611607);
        }


        if (isset($queueSettings['queueNamePrefix'])) {
            $queueNameWithPrefix = $queueSettings['queueNamePrefix'] . $queueName;
        } else {
            $queueNameWithPrefix = $queueName;
        }
        $options = isset($queueSettings['options']) ? $queueSettings['options'] : [];
        $queue = new $queueObjectName($queueNameWithPrefix, $options);
        $this->queues[$queueName] = $queue;

        return $queue;
    }
1175101
public function reserve()
    {
        $this->refreshQueues();

        $this->logger->debug('Attempting to reserve job from {queues}', array(
            'queues' => empty($this->queues) ? 'empty queue list' : implode(', ', $this->queues)
        ));

        foreach ($this->queues as $queue) {
            $payload = $this->resque->pop($queue);

            if (!is_array($payload)) {
                continue;
            }

            $payload['queue'] = $queue;

            try {
                $job = $this->createJobInstance($queue, $payload);
            } catch (ResqueException $exception) {
                $this->failJob($payload, $exception);
                return null;
            }

            if ($job) {
                $this->logger->info('Found job on {queue}', array('queue' => $queue));
                return $job;
            }
        }

        return null;
    }
1342420
public function run($queue = 'queue')
    {   
        // notify its up
        echo ' * Worker online. waiting for tasks.', "\n";

        // define job
        $callback = function($message) {
            // notify once a task is received
            echo " * a task is received", "\n";

            // get the data
            $data = json_decode($message->body, true);

            // get application
            $app = ucfirst($data['APPLICATION']);

            //remove starting \\ and append the Job holder
            $app = substr($app, 1).'\\Job\\';

            // extract the job to perform
            $task = str_replace(' ', '\\', ucwords(str_replace('-', ' ', $data['TASK'])));
            $task = $app.$task;

            $attempts = isset($data['RETRY']) ? $data['RETRY'] : 0;

            try {

                // if there's not a class
                if(!class_exists($task)) {
                    //throw
                    throw new Exception('task do not exist', 404);
                }

                //instantiate the job
                $job = new $task();

                // set data and run the process
                $job->setData($data)->run();

                // once done, notify again, that it is done
                echo " * task is done", "\n";

                // set or flag that the worker is free
                $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
            } catch (Exception $e) {
                // once an exception is encountered, notify that task is not done
                echo " * task not done", "\n";
                echo $e->getMessage()."\n";

                if ($attempts && $message->delivery_info['redelivered'] > $attempts) {
                    echo " * Requeueing ....", "\n";
                    // set or flag that the task is not done and the worker is free and requeue task
                    $message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag'], false, true);
                }

                // set or flag that the task is not done and the worker is free
                $message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag']);
            }
        };

        // worker consuming tasks from queue
        $this->channel->basic_qos(null, 1, null);
        
        // now we need to catch the channel exception
        // when task does not exists in our queue
        try {
            // comsume messages on queue
            $this->channel->basic_consume($queue, '', false, false, false, false, $callback);
        } catch(\PhpAmqpLib\Exception\AMQPProtocolChannelException $e) {
            // notify that task does not exists
            echo " * Task does not exists, creating task. Please re-run the worker. \n";
                
            // create the init queue
            eve()->queue('init', array())->save();
        }
        
        while(count($this->channel->callbacks)) {
            $this->channel->wait();
        }

        $this->channel->close();
        $this->connection->close();
        echo 'Closing Dispatcher'. PHP_EOL;
    }
485161
public function pop($queue = null)
    {
        $queue = $queue ?: $this->queue;

        $now = new \DateTimeImmutable('now');

        $sql = 'SELECT * FROM ' . $this->table .
            ' WHERE queue = :queue AND visibility < :visibility' .
            ' AND (reserved IS NULL OR reserved < :reserved)' .
            ' FOR UPDATE';

        $this->pdo->beginTransaction();

        $stat = $this->pdo->prepare($sql);
        $stat->bindValue(':queue', $queue);
        $stat->bindValue(':visibility', $now->format('Y-m-d H:i:s'), \PDO::PARAM_STR);
        $stat->bindValue(
            ':reserved',
            $now->modify('-' . $this->timeout . 'seconds')->format('Y-m-d H:i:s'),
            \PDO::PARAM_STR
        );

        try {
            $stat->execute();

            $data = $stat->fetch(\PDO::FETCH_ASSOC);

            if (!$data) {
                $this->pdo->commit();

                return null;
            }

            $data['attempts']++;

            $sql = 'UPDATE ' . $this->table . ' SET reserved = :reserved, attempts = :attempts WHERE id = :id';

            $stat = $this->pdo->prepare($sql);
            $stat->bindValue(':reserved', $now->format('Y-m-d H:i:s'));
            $stat->bindValue(':attempts', $data['attempts'] + 1);
            $stat->bindValue(':id', $data['id']);

            $stat->execute();

            $this->pdo->commit();
        } catch (\Throwable $t) {
            $this->pdo->rollBack();
            throw $t;
        }

        $message = new QueueMessage();

        $message->setId($data['id']);
        $message->setAttempts($data['attempts']);
        $message->setBody(json_decode($data['body'], true));
        $message->setRawBody($data['body']);
        $message->setQueueName($queue);

        return $message;
    }
427833
protected function getNextAvailableJob($queue)
    {
        $job = $this->database->table($this->table)
            ->lockForUpdate()
            ->where('queue', $this->getQueue($queue))
            ->where(function ($query)
            {
                $this->isAvailable($query);

                $this->isReservedButExpired($query);
            })
            ->orderBy('id', 'asc')
            ->first();

        if (! is_null($job)) {
            return (object) $job;
        }
    }
678443
protected function migrate($queue)
    {
        $this->migrateExpiredJobs($queue.':delayed', $queue);

        if (! is_null($this->retryAfter)) {
            $this->migrateExpiredJobs($queue.':reserved', $queue);
        }
    }