123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
-
- namespace Illuminate\Queue;
-
- use Illuminate\Support\Str;
- use Illuminate\Queue\Jobs\RedisJob;
- use Illuminate\Contracts\Redis\Factory as Redis;
- use Illuminate\Contracts\Queue\Queue as QueueContract;
-
- class RedisQueue extends Queue implements QueueContract
- {
- /**
- * The Redis factory implementation.
- *
- * @var \Illuminate\Contracts\Redis\Factory
- */
- protected $redis;
-
- /**
- * The connection name.
- *
- * @var string
- */
- protected $connection;
-
- /**
- * The name of the default queue.
- *
- * @var string
- */
- protected $default;
-
- /**
- * The expiration time of a job.
- *
- * @var int|null
- */
- protected $retryAfter = 60;
-
- /**
- * The maximum number of seconds to block for a job.
- *
- * @var int|null
- */
- protected $blockFor = null;
-
- /**
- * Create a new Redis queue instance.
- *
- * @param \Illuminate\Contracts\Redis\Factory $redis
- * @param string $default
- * @param string $connection
- * @param int $retryAfter
- * @param int|null $blockFor
- * @return void
- */
- public function __construct(Redis $redis, $default = 'default', $connection = null, $retryAfter = 60, $blockFor = null)
- {
- $this->redis = $redis;
- $this->default = $default;
- $this->blockFor = $blockFor;
- $this->connection = $connection;
- $this->retryAfter = $retryAfter;
- }
-
- /**
- * Get the size of the queue.
- *
- * @param string $queue
- * @return int
- */
- public function size($queue = null)
- {
- $queue = $this->getQueue($queue);
-
- return $this->getConnection()->eval(
- LuaScripts::size(), 3, $queue, $queue.':delayed', $queue.':reserved'
- );
- }
-
- /**
- * Push a new job onto the queue.
- *
- * @param object|string $job
- * @param mixed $data
- * @param string $queue
- * @return mixed
- */
- public function push($job, $data = '', $queue = null)
- {
- return $this->pushRaw($this->createPayload($job, $data), $queue);
- }
-
- /**
- * Push a raw payload onto the queue.
- *
- * @param string $payload
- * @param string $queue
- * @param array $options
- * @return mixed
- */
- public function pushRaw($payload, $queue = null, array $options = [])
- {
- $this->getConnection()->rpush($this->getQueue($queue), $payload);
-
- return json_decode($payload, true)['id'] ?? null;
- }
-
- /**
- * Push a new job onto the queue after a delay.
- *
- * @param \DateTimeInterface|\DateInterval|int $delay
- * @param object|string $job
- * @param mixed $data
- * @param string $queue
- * @return mixed
- */
- public function later($delay, $job, $data = '', $queue = null)
- {
- return $this->laterRaw($delay, $this->createPayload($job, $data), $queue);
- }
-
- /**
- * Push a raw job onto the queue after a delay.
- *
- * @param \DateTimeInterface|\DateInterval|int $delay
- * @param string $payload
- * @param string $queue
- * @return mixed
- */
- protected function laterRaw($delay, $payload, $queue = null)
- {
- $this->getConnection()->zadd(
- $this->getQueue($queue).':delayed', $this->availableAt($delay), $payload
- );
-
- return json_decode($payload, true)['id'] ?? null;
- }
-
- /**
- * Create a payload string from the given job and data.
- *
- * @param string $job
- * @param mixed $data
- * @return string
- */
- protected function createPayloadArray($job, $data = '')
- {
- return array_merge(parent::createPayloadArray($job, $data), [
- 'id' => $this->getRandomId(),
- 'attempts' => 0,
- ]);
- }
-
- /**
- * Pop the next job off of the queue.
- *
- * @param string $queue
- * @return \Illuminate\Contracts\Queue\Job|null
- */
- public function pop($queue = null)
- {
- $this->migrate($prefixed = $this->getQueue($queue));
-
- list($job, $reserved) = $this->retrieveNextJob($prefixed);
-
- if ($reserved) {
- return new RedisJob(
- $this->container, $this, $job,
- $reserved, $this->connectionName, $queue ?: $this->default
- );
- }
- }
-
- /**
- * Migrate any delayed or expired jobs onto the primary queue.
- *
- * @param string $queue
- * @return void
- */
- protected function migrate($queue)
- {
- $this->migrateExpiredJobs($queue.':delayed', $queue);
-
- if (! is_null($this->retryAfter)) {
- $this->migrateExpiredJobs($queue.':reserved', $queue);
- }
- }
-
- /**
- * Migrate the delayed jobs that are ready to the regular queue.
- *
- * @param string $from
- * @param string $to
- * @return array
- */
- public function migrateExpiredJobs($from, $to)
- {
- return $this->getConnection()->eval(
- LuaScripts::migrateExpiredJobs(), 2, $from, $to, $this->currentTime()
- );
- }
-
- /**
- * Retrieve the next job from the queue.
- *
- * @param string $queue
- * @return array
- */
- protected function retrieveNextJob($queue)
- {
- if (! is_null($this->blockFor)) {
- return $this->blockingPop($queue);
- }
-
- return $this->getConnection()->eval(
- LuaScripts::pop(), 2, $queue, $queue.':reserved',
- $this->availableAt($this->retryAfter)
- );
- }
-
- /**
- * Retrieve the next job by blocking-pop.
- *
- * @param string $queue
- * @return array
- */
- protected function blockingPop($queue)
- {
- $rawBody = $this->getConnection()->blpop($queue, $this->blockFor);
-
- if (! empty($rawBody)) {
- $payload = json_decode($rawBody[1], true);
-
- $payload['attempts']++;
-
- $reserved = json_encode($payload);
-
- $this->getConnection()->zadd($queue.':reserved', [
- $reserved => $this->availableAt($this->retryAfter),
- ]);
-
- return [$rawBody[1], $reserved];
- }
-
- return [null, null];
- }
-
- /**
- * Delete a reserved job from the queue.
- *
- * @param string $queue
- * @param \Illuminate\Queue\Jobs\RedisJob $job
- * @return void
- */
- public function deleteReserved($queue, $job)
- {
- $this->getConnection()->zrem($this->getQueue($queue).':reserved', $job->getReservedJob());
- }
-
- /**
- * Delete a reserved job from the reserved queue and release it.
- *
- * @param string $queue
- * @param \Illuminate\Queue\Jobs\RedisJob $job
- * @param int $delay
- * @return void
- */
- public function deleteAndRelease($queue, $job, $delay)
- {
- $queue = $this->getQueue($queue);
-
- $this->getConnection()->eval(
- LuaScripts::release(), 2, $queue.':delayed', $queue.':reserved',
- $job->getReservedJob(), $this->availableAt($delay)
- );
- }
-
- /**
- * Get a random ID string.
- *
- * @return string
- */
- protected function getRandomId()
- {
- return Str::random(32);
- }
-
- /**
- * Get the queue or return the default.
- *
- * @param string|null $queue
- * @return string
- */
- public function getQueue($queue)
- {
- return 'queues:'.($queue ?: $this->default);
- }
-
- /**
- * Get the connection for the queue.
- *
- * @return \Illuminate\Redis\Connections\Connection
- */
- protected function getConnection()
- {
- return $this->redis->connection($this->connection);
- }
-
- /**
- * Get the underlying Redis instance.
- *
- * @return \Illuminate\Contracts\Redis\Factory
- */
- public function getRedis()
- {
- return $this->redis;
- }
- }
|