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 all 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
2 changes: 2 additions & 0 deletions src/drivers/db/InfoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/**
* Info about queue status.
*
* @deprecated Will be removed in 3.0. Use yii\queue\cli\InfoAction instead.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class InfoAction extends Action
Expand Down
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
2 changes: 2 additions & 0 deletions src/drivers/file/InfoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/**
* Info about queue status.
*
* @deprecated Will be removed in 3.0. Use yii\queue\cli\InfoAction instead.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class InfoAction extends Action
Expand Down
16 changes: 15 additions & 1 deletion src/drivers/file/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
use yii\base\NotSupportedException;
use yii\helpers\FileHelper;
use yii\queue\cli\Queue as CliQueue;
use yii\queue\interfaces\StatisticsProviderInterface;

/**
* File Queue.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class Queue extends CliQueue
class Queue extends CliQueue implements StatisticsProviderInterface
{
/**
* @var string
Expand Down Expand Up @@ -304,4 +305,17 @@ private function touchIndex($callback)
fclose($file);
}
}

private $_statistcsProvider;

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

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

namespace yii\queue\file;

use yii\base\BaseObject;
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()
{
$data = $this->getIndexData();
return !empty($data['waiting']) ? count($data['waiting']) : 0;
}

/**
* @inheritdoc
*/
public function getDelayedCount()
{
$data = $this->getIndexData();
return !empty($data['delayed']) ? count($data['delayed']) : 0;
}

/**
* @inheritdoc
*/
public function getReservedCount()
{
$data = $this->getIndexData();
return !empty($data['reserved']) ? count($data['reserved']) : 0;
}

/**
* @inheritdoc
*/
public function getDoneCount()
{
$data = $this->getIndexData();
$total = isset($data['lastId']) ? $data['lastId'] : 0;
return $total - $this->getDelayedCount() - $this->getWaitingCount();
}

protected function getIndexData()
{
$fileName = $this->queue->path . '/index.data';
if (file_exists($fileName)) {
return call_user_func($this->queue->indexDeserializer, file_get_contents($fileName));
} else {
return [];
}
}
}
1 change: 1 addition & 0 deletions src/drivers/redis/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 redis-queue.
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/redis/InfoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/**
* Info about queue status.
*
* @deprecated Will be removed in 3.0. Use yii\queue\cli\InfoAction instead.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class InfoAction extends Action
Expand Down
Loading
Loading