mirror of
https://github.com/idanoo/php-resque
synced 2025-07-01 21:52:21 +00:00
2.0.3 (2022-09-12)
- Update composer packages - Added WoodpeckerCI tests - Updated links in composer package - Stricter typing
This commit is contained in:
parent
f9a22e7b8a
commit
05cae402b5
21 changed files with 215 additions and 524 deletions
|
@ -25,8 +25,12 @@ class Failure
|
|||
* @param \Resque\Worker $worker Instance of Resque_Worker that was running this job when it failed.
|
||||
* @param string $queue The name of the queue that this job was fetched from.
|
||||
*/
|
||||
public static function create($payload, \Exception $exception, \Resque\Worker $worker, $queue)
|
||||
{
|
||||
public static function create(
|
||||
$payload,
|
||||
\Exception $exception,
|
||||
\Resque\Worker $worker,
|
||||
$queue
|
||||
) {
|
||||
$backend = self::getBackend();
|
||||
new $backend($payload, $exception, $worker, $queue);
|
||||
}
|
||||
|
@ -34,11 +38,11 @@ class Failure
|
|||
/**
|
||||
* Return an instance of the backend for saving job failures.
|
||||
*
|
||||
* @return object|string
|
||||
* @return string
|
||||
*/
|
||||
public static function getBackend()
|
||||
{
|
||||
if (self::$backend === null) {
|
||||
if (is_null(self::$backend)) {
|
||||
self::$backend = '\\Resque\\Failure\\ResqueFailureRedis';
|
||||
}
|
||||
|
||||
|
@ -51,8 +55,10 @@ class Failure
|
|||
* It is your responsibility to have the backend class loaded (or autoloaded)
|
||||
*
|
||||
* @param string $backend The class name of the backend to pipe failures to.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setBackend($backend)
|
||||
public static function setBackend(string $backend): void
|
||||
{
|
||||
self::$backend = $backend;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,9 @@ class Factory implements FactoryInterface
|
|||
* @param $className
|
||||
* @param array $args
|
||||
* @param $queue
|
||||
*
|
||||
* @return \Resque\Job\JobInterface
|
||||
*
|
||||
* @throws \Resque\Exception
|
||||
*/
|
||||
public function create($className, $args, $queue)
|
||||
|
@ -28,13 +30,14 @@ class Factory implements FactoryInterface
|
|||
|
||||
if (!method_exists($className, 'perform')) {
|
||||
throw new \Resque\Exception(
|
||||
'Job class ' . $className . ' does not contain a perform method.'
|
||||
'Job class ' . $className . ' does not contain a perform() method.'
|
||||
);
|
||||
}
|
||||
|
||||
$instance = new $className();
|
||||
$instance->args = $args;
|
||||
$instance->queue = $queue;
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ interface FactoryInterface
|
|||
* @param $className
|
||||
* @param array $args
|
||||
* @param $queue
|
||||
*
|
||||
* @return \Resque\Job\JobInterface
|
||||
*/
|
||||
public function create($className, $args, $queue);
|
||||
|
|
|
@ -62,7 +62,7 @@ class Job
|
|||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function create($queue, $class, $args = null, $monitor = false, $id = null)
|
||||
public static function create($queue, $class, $args = null, $monitor = false, $id = null): string
|
||||
{
|
||||
if (is_null($id)) {
|
||||
$id = \Resque\Resque::generateJobId();
|
||||
|
@ -93,13 +93,13 @@ class Job
|
|||
*
|
||||
* @param string $queue The name of the queue to check for a job in.
|
||||
*
|
||||
* @return false|object Null when there aren't any waiting jobs, instance of \Resque\Job\Job when a job was found.
|
||||
* @return Job|null Null when there aren't any waiting jobs, instance of \Resque\Job\Job when a job was found.
|
||||
*/
|
||||
public static function reserve($queue)
|
||||
public static function reserve($queue): ?Job
|
||||
{
|
||||
$payload = \Resque\Resque::pop($queue);
|
||||
if (!is_array($payload)) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Job($queue, $payload);
|
||||
|
@ -112,13 +112,13 @@ class Job
|
|||
* @param array $queues
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return false|object Null when there aren't any waiting jobs, instance of \Resque\Job\Job when a job was found.
|
||||
* @return Job|null Null when there aren't any waiting jobs, instance of \Resque\Job\Job when a job was found.
|
||||
*/
|
||||
public static function reserveBlocking(array $queues, $timeout = null)
|
||||
public static function reserveBlocking(array $queues, $timeout = null): ?Job
|
||||
{
|
||||
$item = \Resque\Resque::blpop($queues, $timeout);
|
||||
if (!is_array($item)) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Job($item['queue'], $item['payload']);
|
||||
|
@ -292,9 +292,10 @@ class Job
|
|||
*/
|
||||
public function getJobFactory()
|
||||
{
|
||||
if ($this->jobFactory === null) {
|
||||
if (is_null($this->jobFactory)) {
|
||||
$this->jobFactory = new Factory();
|
||||
}
|
||||
|
||||
return $this->jobFactory;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ class Status
|
|||
'updated' => time(),
|
||||
'started' => time(),
|
||||
];
|
||||
\Resque\Resque::redis()->setex('job:' . $id . ':status', 172800, json_encode($statusPacket));
|
||||
\Resque\Resque::redis()->setex('job:' . $id . ':status', 86400, json_encode($statusPacket));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,7 +98,7 @@ class Status
|
|||
'status' => $status,
|
||||
'updated' => time(),
|
||||
];
|
||||
\Resque\Resque::redis()->setex((string)$this, 172800, json_encode($statusPacket));
|
||||
\Resque\Resque::redis()->setex((string)$this, 86400, json_encode($statusPacket));
|
||||
|
||||
// Expire the status for completed jobs after 24 hours
|
||||
if (in_array($status, self::$completeStatuses)) {
|
||||
|
@ -128,8 +128,10 @@ class Status
|
|||
|
||||
/**
|
||||
* Stop tracking the status of a job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function stop()
|
||||
public function stop(): void
|
||||
{
|
||||
\Resque\Resque::redis()->del((string)$this);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ class Log extends \Psr\Log\AbstractLogger
|
|||
{
|
||||
public $logLevel;
|
||||
|
||||
public function __construct($logLevel = "warning")
|
||||
public function __construct($logLevel = 'warning')
|
||||
{
|
||||
$this->logLevel = strtolower($logLevel);
|
||||
}
|
||||
|
@ -30,14 +30,14 @@ class Log extends \Psr\Log\AbstractLogger
|
|||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$logLevels = [
|
||||
"emergency",
|
||||
"alert",
|
||||
"critical",
|
||||
"error",
|
||||
"warning",
|
||||
"notice",
|
||||
"info",
|
||||
"debug",
|
||||
'emergency',
|
||||
'alert',
|
||||
'critical',
|
||||
'error',
|
||||
'warning',
|
||||
'notice',
|
||||
'info',
|
||||
'debug',
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
@ -104,9 +104,12 @@ class Redis
|
|||
|
||||
/**
|
||||
* Set Redis namespace (prefix) default: resque
|
||||
*
|
||||
* @param string $namespace
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function prefix($namespace)
|
||||
public static function prefix(string $namespace): void
|
||||
{
|
||||
if (substr($namespace, -1) !== ':' && $namespace != '') {
|
||||
$namespace .= ':';
|
||||
|
@ -119,6 +122,7 @@ class Redis
|
|||
* @param int $database A database number to select. However, if we find a valid database number in the DSN the
|
||||
* DSN-supplied value will be used instead and this parameter is ignored.
|
||||
* @param object $client Optional \Credis_Client instance instantiated by you
|
||||
*
|
||||
* @throws \Resque\RedisException
|
||||
*/
|
||||
public function __construct($server, $database = null, $client = null)
|
||||
|
@ -167,7 +171,7 @@ class Redis
|
|||
* @return array An array of DSN compotnents, with 'false' values for any unknown components. e.g.
|
||||
* [host, port, db, user, pass, options]
|
||||
*/
|
||||
public static function parseDsn($dsn)
|
||||
public static function parseDsn($dsn): array
|
||||
{
|
||||
if ($dsn == '') {
|
||||
// Use a sensible default for an empty DNS string
|
||||
|
@ -234,7 +238,9 @@ class Redis
|
|||
*
|
||||
* @param string $name The name of the method called.
|
||||
* @param array $args Array of supplied arguments to the method.
|
||||
*
|
||||
* @return mixed Return value from Resident::call() based on the command.
|
||||
*
|
||||
* @throws Resque_RedisException
|
||||
*/
|
||||
public function __call($name, $args)
|
||||
|
@ -250,23 +256,18 @@ class Redis
|
|||
}
|
||||
try {
|
||||
return $this->driver->__call($name, $args);
|
||||
} catch (\CredisException $e) {
|
||||
} catch (\Exception $e) {
|
||||
throw new RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns redis prefix
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getPrefix(): string
|
||||
{
|
||||
return self::$defaultNamespace;
|
||||
}
|
||||
|
||||
public static function removePrefix($string): string
|
||||
{
|
||||
$prefix = self::getPrefix();
|
||||
|
||||
if (substr($string, 0, strlen($prefix)) == $prefix) {
|
||||
$string = substr($string, strlen($prefix), strlen($string));
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Resque;
|
||||
|
||||
/**
|
||||
* Redis related exceptions
|
||||
* Redis exceptions
|
||||
*
|
||||
* @package Resque
|
||||
* @author Daniel Mason <daniel@m2.nz>
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Resque;
|
|||
|
||||
class Resque
|
||||
{
|
||||
public const VERSION = '2.0.2';
|
||||
public const VERSION = '2.0.3';
|
||||
|
||||
public const DEFAULT_INTERVAL = 5;
|
||||
|
||||
|
@ -58,7 +58,7 @@ class Resque
|
|||
*/
|
||||
public static function redis()
|
||||
{
|
||||
if (self::$redis !== null) {
|
||||
if (!is_null(self::$redis)) {
|
||||
return self::$redis;
|
||||
}
|
||||
|
||||
|
@ -113,11 +113,14 @@ class Resque
|
|||
if ($encodedItem === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self::redis()->sadd('queues', $queue);
|
||||
|
||||
$length = self::redis()->rpush('queue:' . $queue, $encodedItem);
|
||||
if ($length < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -253,9 +256,9 @@ class Resque
|
|||
*
|
||||
* @param string $queue Queue to fetch next available job from.
|
||||
*
|
||||
* @return false|object|\Resque\Job\Job
|
||||
* @return \Resque\Job\Job|null
|
||||
*/
|
||||
public static function reserve($queue)
|
||||
public static function reserve($queue): ?\Resque\Job\Job
|
||||
{
|
||||
return \Resque\Job\Job::reserve($queue);
|
||||
}
|
||||
|
|
|
@ -16,9 +16,10 @@ class Stat
|
|||
* 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.
|
||||
* @return mixed Value of the statistic.
|
||||
*
|
||||
* @return int Value of the statistic.
|
||||
*/
|
||||
public static function get($stat): int
|
||||
public static function get(string $stat): int
|
||||
{
|
||||
return (int)Resque::redis()->get('stat:' . $stat);
|
||||
}
|
||||
|
|
|
@ -284,7 +284,7 @@ class Worker
|
|||
|
||||
if ($blocking === true) {
|
||||
$job = \Resque\Job\Job::reserveBlocking($queues, $timeout);
|
||||
if ($job) {
|
||||
if (!is_null($job)) {
|
||||
$this->logger->log(\Psr\Log\LogLevel::INFO, 'Found job on {queue}', ['queue' => $job->queue]);
|
||||
return $job;
|
||||
}
|
||||
|
@ -292,7 +292,7 @@ class Worker
|
|||
foreach ($queues as $queue) {
|
||||
$this->logger->log(\Psr\Log\LogLevel::INFO, 'Checking {queue} for jobs', ['queue' => $queue]);
|
||||
$job = \Resque\Job\Job::reserve($queue);
|
||||
if ($job) {
|
||||
if (!is_null($job)) {
|
||||
$this->logger->log(\Psr\Log\LogLevel::INFO, 'Found job on {queue}', ['queue' => $job->queue]);
|
||||
return $job;
|
||||
}
|
||||
|
@ -491,7 +491,7 @@ class Worker
|
|||
public function registerWorker()
|
||||
{
|
||||
Resque::redis()->sadd('workers', (string)$this);
|
||||
Resque::redis()->setex('worker:' . (string)$this . ':started', 172800, date('D M d H:i:s T Y'));
|
||||
Resque::redis()->setex('worker:' . (string)$this . ':started', 86400, date('D M d H:i:s T Y'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -527,7 +527,7 @@ class Worker
|
|||
'run_at' => date('D M d H:i:s T Y'),
|
||||
'payload' => $job->payload
|
||||
]);
|
||||
Resque::redis()->setex('worker:' . $job->worker, 172800, $data);
|
||||
Resque::redis()->setex('worker:' . $job->worker, 86400, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -567,9 +567,10 @@ class Worker
|
|||
* Get a statistic belonging to this worker.
|
||||
*
|
||||
* @param string $stat Statistic to fetch.
|
||||
*
|
||||
* @return int Statistic value.
|
||||
*/
|
||||
public function getStat($stat)
|
||||
public function getStat(string $stat): int
|
||||
{
|
||||
return \Resque\Stat::get($stat . ':' . $this);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue