* Determine number of items remaining in the queue.
*
* @return int
- */
- abstract public function numberOfItems();
+ * @deprecated
+ * Use `getStatistic(string $name)` instead.
+ * The definition of `numberOfItems()` has become conflicted among different subclasses.
+ */
+ public function numberOfItems() {
+ // This is the statistic traditionally reported by core queue implementations.
+ // However, it may not be as useful, and subclasses may have different interpretations.
+ return $this->getStatistic('total');
+ }
+
+ /**
+ * Get summary information about items in the queue.
+ *
+ * @param string $name
+ * The desired statistic. Ex:
+ * - 'ready': The number of items ready for execution (not currently claimed, not scheduled for future).
+ * - 'blocked': The number of items that may be runnable in the future, but cannot be run right now.
+ * - 'total': The total number of items known to the queue, regardless of whether their current status.
+ * @return int|float|null
+ * The value of the statistic, or NULL if the queue backend does not unsupport this statistic.
+ */
+ abstract public function getStatistic(string $name);
/**
* Get the next item.
}
/**
- * Determine number of items remaining in the queue.
- *
- * @return int
+ * @param string $name
+ * @return int|float|null
+ * @see \CRM_Queue_Queue::getStatistic()
*/
- public function numberOfItems(): int {
- return count($this->items);
+ public function getStatistic(string $name) {
+ $ready = function(): int {
+ $now = CRM_Utils_Time::time();
+ $ready = 0;
+ foreach ($this->items as $id => $item) {
+ if (empty($this->releaseTimes[$id]) || $this->releaseTimes[$id] <= $now) {
+ $ready++;
+ }
+ }
+ return $ready;
+ };
+
+ switch ($name) {
+ case 'ready':
+ return $ready();
+
+ case 'blocked':
+ return count($this->items) - $ready();
+
+ case 'total':
+ return count($this->items);
+
+ default:
+ return NULL;
+ }
}
/**
* @return bool
*/
public function existsQueue() {
- return ($this->numberOfItems() > 0);
+ return ($this->getStatistic('total') > 0);
}
/**
- * Determine number of items remaining in the queue.
- *
- * @return int
+ * @param string $name
+ * @return int|float|null
+ * @see \CRM_Queue_Queue::getStatistic()
*/
- public function numberOfItems() {
- return (int) CRM_Core_DAO::singleValueQuery('
- SELECT count(*)
- FROM civicrm_queue_item
- WHERE queue_name = %1
- ', [
- 1 => [$this->getName(), 'String'],
- ]);
+ public function getStatistic(string $name) {
+ switch ($name) {
+ case 'ready':
+ return (int) CRM_Core_DAO::singleValueQuery(
+ 'SELECT count(*) FROM civicrm_queue_item WHERE queue_name = %1 AND (release_time is null OR release_time <= FROM_UNIXTIME(%2))',
+ [1 => [$this->getName(), 'String'], 2 => [CRM_Utils_Time::time(), 'Int']]);
+
+ case 'blocked':
+ return (int) CRM_Core_DAO::singleValueQuery(
+ 'SELECT count(*) FROM civicrm_queue_item WHERE queue_name = %1 AND release_time > FROM_UNIXTIME(%2)',
+ [1 => [$this->getName(), 'String'], 2 => [CRM_Utils_Time::time(), 'Int']]);
+
+ case 'total':
+ return (int) CRM_Core_DAO::singleValueQuery(
+ 'SELECT count(*) FROM civicrm_queue_item WHERE queue_name = %1',
+ [1 => [$this->getName(), 'String']]);
+
+ default:
+ return NULL;
+ }
}
/**