Skip to content

Commit

Permalink
Added a way to get the size of the queue (laravel#14662)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored and tillkruss committed Aug 30, 2016
1 parent e7489a2 commit 0fafba9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Illuminate/Contracts/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function laterOn($queue, $delay, $job, $data = '');
* @return \Illuminate\Contracts\Queue\Job|null
*/
public function pop($queue = null);

/**
* Get the size of the queue.
*
* @param string $queue
* @return int
*/
public function size($queue = null);
}
13 changes: 13 additions & 0 deletions src/Illuminate/Queue/BeanstalkdQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ public function pop($queue = null)
}
}

/**
* Get the size of the queue.
*
* @param string $queue
* @return int
*/
public function size($queue = null)
{
$queue = $this->getQueue($queue);

return (int) $this->pheanstalk->statsTube($queue)->total_jobs;
}

/**
* Delete a message from the Beanstalk queue.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ public function pop($queue = null)
$this->database->commit();
}

/**
* Get the size of the queue.
*
* @param string $queue
* @return int
*/
public function size($queue = null)
{
return $this->database->table($this->table)
->where('queue', $this->getQueue($queue))
->count();
}

/**
* Get the next available job for the queue.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Queue/NullQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ public function pop($queue = null)
{
//
}

/**
* Get the size of the queue.
*
* @param string $queue
* @return int
*/
public function size($queue = null)
{
return 0;
}
}
11 changes: 11 additions & 0 deletions src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ public function pop($queue = null)
}
}

/**
* Get the size of the queue.
*
* @param string $queue
* @return int
*/
public function size($queue = null)
{
return (int) $this->getConnection()->llen($this->getQueue($queue));
}

/**
* Delete a reserved job from the queue.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Queue/SqsQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ public function pop($queue = null)
}
}

/**
* Get the size of the queue.
*
* @param string $queue
* @return int
*/
public function size($queue = null)
{
return (int) $this->sqs->getQueueAttributes([
'QueueUrl' => $this->getQueue($queue),
])->get('ApproximateNumberOfMessages');
}

/**
* Get the queue or return the default.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Queue/SyncQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ public function pop($queue = null)
//
}

/**
* Get the size of the queue.
*
* @param string $queue
* @return int
*/
public function size($queue = null)
{
return 0;
}

/**
* Resolve a Sync job instance.
*
Expand Down

0 comments on commit 0fafba9

Please sign in to comment.