Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics provider interface #510

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Yii2 Queue Extension Change Log

2.3.7 under development
-----------------------

- Enh #509: Add StatisticsProviderInterface to get statistics from queue (kalmer)
- no changes in this release.


Expand Down
63 changes: 63 additions & 0 deletions src/cli/InfoAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

namespace yii\queue\cli;

use yii\base\NotSupportedException;
use yii\helpers\Console;
use yii\queue\interfaces\DelayedCountInterface;
use yii\queue\interfaces\DoneCountInterface;
use yii\queue\interfaces\ReservedCountInterface;
use yii\queue\interfaces\StatisticsProviderInterface;
use yii\queue\interfaces\WaitingCountInterface;

/**
* Info about queue status.
*
* @author Kalmer Kaurson <kalmerkaurson@gmail.com>
*/
class InfoAction extends Action
{
/**
* @var Queue
*/
public $queue;


/**
* Info about queue status.
*/
public function run()
{
if (!($this->queue instanceof StatisticsProviderInterface)) {
throw new NotSupportedException('Queue does not support ' . StatisticsProviderInterface::class);
}

$this->controller->stdout('Jobs' . PHP_EOL, Console::FG_GREEN);
$statisticsProvider = $this->queue->getStatisticsProvider();

if ($statisticsProvider instanceof WaitingCountInterface) {
$this->controller->stdout('- waiting: ', Console::FG_YELLOW);
$this->controller->stdout($statisticsProvider->getWaitingCount() . PHP_EOL);
}

if ($statisticsProvider instanceof DelayedCountInterface) {
$this->controller->stdout('- delayed: ', Console::FG_YELLOW);
$this->controller->stdout($statisticsProvider->getDelayedCount() . PHP_EOL);
}

if ($statisticsProvider instanceof ReservedCountInterface) {
$this->controller->stdout('- reserved: ', Console::FG_YELLOW);
$this->controller->stdout($statisticsProvider->getReservedCount() . PHP_EOL);
}

if ($statisticsProvider instanceof DoneCountInterface) {
$this->controller->stdout('- done: ', Console::FG_YELLOW);
$this->controller->stdout($statisticsProvider->getDoneCount() . PHP_EOL);
}
}
}
1 change: 1 addition & 0 deletions src/drivers/db/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use yii\console\Exception;
use yii\queue\cli\Command as CliCommand;
use yii\queue\cli\InfoAction;

/**
* Manages application db-queue.
Expand Down
93 changes: 0 additions & 93 deletions src/drivers/db/InfoAction.php

This file was deleted.

18 changes: 16 additions & 2 deletions src/drivers/db/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
use yii\di\Instance;
use yii\mutex\Mutex;
use yii\queue\cli\Queue as CliQueue;
use yii\queue\interfaces\StatisticsProviderInterface;

/**
* Db Queue.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class Queue extends CliQueue
class Queue extends CliQueue implements StatisticsProviderInterface
{
/**
* @var Connection|array|string
Expand Down Expand Up @@ -233,6 +234,8 @@ protected function release($payload)
}
}

protected $reserveTime;

/**
* Moves expired messages into waiting list.
*/
Expand All @@ -251,5 +254,16 @@ protected function moveExpired()
}
}

protected $reserveTime;
private $_statistcsProvider;

/**
* @return StatisticsProvider
*/
public function getStatisticsProvider()
{
if (!$this->_statistcsProvider) {
$this->_statistcsProvider = new StatisticsProvider($this);
}
return $this->_statistcsProvider;
}
}
82 changes: 82 additions & 0 deletions src/drivers/db/StatisticsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

namespace yii\queue\db;

use yii\base\BaseObject;
use yii\db\Query;
use yii\queue\interfaces\DelayedCountInterface;
use yii\queue\interfaces\DoneCountInterface;
use yii\queue\interfaces\ReservedCountInterface;
use yii\queue\interfaces\WaitingCountInterface;

/**
* Statistics Provider
*
* @author Kalmer Kaurson <kalmerkaurson@gmail.com>
*/
class StatisticsProvider extends BaseObject implements DoneCountInterface, WaitingCountInterface, DelayedCountInterface, ReservedCountInterface
{
/**
* @var Queue
*/
protected $queue;

public function __construct(Queue $queue, $config = [])
{
$this->queue = $queue;
parent::__construct($config);
}

/**
* @inheritdoc
*/
public function getWaitingCount()
{
return (new Query())
->from($this->queue->tableName)
->andWhere(['channel' => $this->queue->channel])
->andWhere(['reserved_at' => null])
->andWhere(['delay' => 0])->count('*', $this->queue->db);
}

/**
* @inheritdoc
*/
public function getDelayedCount()
{
return (new Query())
->from($this->queue->tableName)
->andWhere(['channel' => $this->queue->channel])
->andWhere(['reserved_at' => null])
->andWhere(['>', 'delay', 0])->count('*', $this->queue->db);
}

/**
* @inheritdoc
*/
public function getReservedCount()
{
return (new Query())
->from($this->queue->tableName)
->andWhere(['channel' => $this->queue->channel])
->andWhere('[[reserved_at]] is not null')
->andWhere(['done_at' => null])->count('*', $this->queue->db);
}

/**
* @inheritdoc
*/
public function getDoneCount()
{
return (new Query())
->from($this->queue->tableName)
->andWhere(['channel' => $this->queue->channel])
->andWhere('[[done_at]] is not null')->count('*', $this->queue->db);
}
}
1 change: 1 addition & 0 deletions src/drivers/file/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use yii\console\Exception;
use yii\queue\cli\Command as CliCommand;
use yii\queue\cli\InfoAction;

/**
* Manages application file-queue.
Expand Down
Loading
Loading