-
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #509: Add StatisticsProviderInterface to get statistics from queue
Co-authored-by: Kalmer Kaurson <[email protected]>
- Loading branch information
1 parent
aa4b167
commit 6df01d5
Showing
31 changed files
with
973 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
/** | ||
* Info about queue status. | ||
* | ||
* @deprecated Will be removed in 3.0. Use yii\queue\cli\InfoAction instead. | ||
* | ||
* @author Roman Zhuravlev <[email protected]> | ||
*/ | ||
class InfoAction extends Action | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
class Queue extends CliQueue | ||
class Queue extends CliQueue implements StatisticsProviderInterface | ||
{ | ||
/** | ||
* @var Connection|array|string | ||
|
@@ -233,6 +234,8 @@ protected function release($payload) | |
} | ||
} | ||
|
||
protected $reserveTime; | ||
|
||
/** | ||
* Moves expired messages into waiting list. | ||
*/ | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
/** | ||
* Info about queue status. | ||
* | ||
* @deprecated Will be removed in 3.0. Use yii\queue\cli\InfoAction instead. | ||
* | ||
* @author Roman Zhuravlev <[email protected]> | ||
*/ | ||
class InfoAction extends Action | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
class Queue extends CliQueue | ||
class Queue extends CliQueue implements StatisticsProviderInterface | ||
{ | ||
/** | ||
* @var string | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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 []; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
/** | ||
* Info about queue status. | ||
* | ||
* @deprecated Will be removed in 3.0. Use yii\queue\cli\InfoAction instead. | ||
* | ||
* @author Roman Zhuravlev <[email protected]> | ||
*/ | ||
class InfoAction extends Action | ||
|
Oops, something went wrong.