2010-04-18 13:58:43 +00:00
|
|
|
<?php
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
namespace Resque;
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
/**
|
|
|
|
* Resque statistic management (jobs processed, failed, etc)
|
|
|
|
*
|
2021-02-18 23:23:32 +00:00
|
|
|
* @package Resque
|
|
|
|
* @author Daniel Mason <daniel@m2.nz>
|
2018-05-25 07:26:54 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
class Stat
|
2010-04-18 13:58:43 +00:00
|
|
|
{
|
2018-05-25 07:26:54 +00:00
|
|
|
/**
|
|
|
|
* Get the value of the supplied statistic counter for the specified statistic.
|
|
|
|
*
|
|
|
|
* @param string $stat The name of the statistic to get the stats for.
|
2022-09-12 07:50:02 +00:00
|
|
|
*
|
|
|
|
* @return int Value of the statistic.
|
2018-05-25 07:26:54 +00:00
|
|
|
*/
|
2022-09-12 07:50:02 +00:00
|
|
|
public static function get(string $stat): int
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
|
|
|
return (int)Resque::redis()->get('stat:' . $stat);
|
|
|
|
}
|
2010-04-18 13:58:43 +00:00
|
|
|
|
2018-05-25 07:26:54 +00:00
|
|
|
/**
|
|
|
|
* Increment the value of the specified statistic by a certain amount (default is 1)
|
|
|
|
*
|
|
|
|
* @param string $stat The name of the statistic to increment.
|
|
|
|
* @param int $by The amount to increment the statistic by.
|
2023-03-19 20:26:45 +00:00
|
|
|
*
|
|
|
|
* @return bool True if successful, false if not.
|
2018-05-25 07:26:54 +00:00
|
|
|
*/
|
2023-03-19 20:26:45 +00:00
|
|
|
public static function incr(string $stat, int $by = 1): bool
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
2023-03-19 20:26:45 +00:00
|
|
|
// Make sure we set a TTL by default
|
|
|
|
$set = Resque::redis()->set(
|
|
|
|
'stat:' . $stat,
|
|
|
|
$by,
|
|
|
|
['ex' => time() + 86400, 'nx'],
|
|
|
|
);
|
|
|
|
|
|
|
|
// If it already exists, return the incrby value
|
|
|
|
if (!$set) {
|
|
|
|
return (bool)Resque::redis()->incrby('stat:' . $stat, $by);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
2010-04-18 13:58:43 +00:00
|
|
|
|
2018-05-25 07:26:54 +00:00
|
|
|
/**
|
|
|
|
* Decrement the value of the specified statistic by a certain amount (default is 1)
|
|
|
|
*
|
|
|
|
* @param string $stat The name of the statistic to decrement.
|
|
|
|
* @param int $by The amount to decrement the statistic by.
|
2023-03-19 20:26:45 +00:00
|
|
|
*
|
|
|
|
* @return bool True if successful, false if not.
|
2018-05-25 07:26:54 +00:00
|
|
|
*/
|
2023-03-19 20:26:45 +00:00
|
|
|
public static function decr(string $stat, int $by = 1): bool
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
|
|
|
return (bool)Resque::redis()->decrby('stat:' . $stat, $by);
|
|
|
|
}
|
2010-04-18 13:58:43 +00:00
|
|
|
|
2018-05-25 07:26:54 +00:00
|
|
|
/**
|
|
|
|
* Delete a statistic with the given name.
|
|
|
|
*
|
|
|
|
* @param string $stat The name of the statistic to delete.
|
2023-03-19 20:26:45 +00:00
|
|
|
*
|
|
|
|
* @return bool True if successful, false if not.
|
2018-05-25 07:26:54 +00:00
|
|
|
*/
|
2023-03-19 20:26:45 +00:00
|
|
|
public static function clear(string $stat): bool
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
2023-03-19 20:26:45 +00:00
|
|
|
return (bool)Resque::redis()->unlink('stat:' . $stat);
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
2021-02-18 23:23:32 +00:00
|
|
|
}
|