Skip to content

Commit

Permalink
Support PHP 8.3 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tspencer244 authored Aug 28, 2024
1 parent 0f6c39b commit 10e215c
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 76 deletions.
52 changes: 48 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
version: 2
version: 2.1

jobs:
build:
executors:
php81:
docker:
- image: cimg/php:8.1
resource_class: small
php82:
docker:
- image: cimg/php:8.2
resource_class: small
php83:
docker:
- image: cimg/php:8.3
resource_class: small

jobs:
analyse:
executor: << parameters.php_executor >>
parameters:
php_executor:
type: string
steps:
- checkout
- run: sudo apt-get update -qq && sudo apt-get install --no-install-recommends --no-install-suggests -qq redis
- run: '{ yes "" | sudo pecl install redis; } || true'
- run: composer update
- run: composer analyse
lint:
executor: php83
steps:
- checkout
- run: sudo apt-get update -qq && sudo apt-get install --no-install-recommends --no-install-suggests -qq redis
- run: '{ yes "" | sudo pecl install redis; } || true'
- run: composer update
- run: composer lint
- run: composer test
test:
parameters:
php_executor:
type: string
executor: << parameters.php_executor >>
steps:
- checkout
- run: sudo apt-get update -qq && sudo apt-get install --no-install-recommends --no-install-suggests -qq redis
- run: '{ yes "" | sudo pecl install redis; } || true'
- run: composer update
- run: composer test

workflows:
analyse_lint_test:
jobs:
- analyse:
matrix:
parameters:
php_executor: [php81, php82, php83]
- lint
- test:
matrix:
parameters:
php_executor: [php81, php82, php83]
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "~8.1.0",
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"ext-pcntl": "*",
"ext-posix": "*",
"ext-redis": "*",
Expand Down
6 changes: 3 additions & 3 deletions demo/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// NOTE: You should NOT use this when developing against php-resque.
// The autoload code below is specifically for this demo.
$files = array(
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
);

$found = false;
Expand Down
12 changes: 6 additions & 6 deletions lib/Resque.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static function blpop(array $queues, $timeout)
$list[] = 'queue:' . $queue;
}

$item = self::redis()->blpop($list, (int)$timeout);
$item = self::redis()->blpop($list, (int) $timeout);

if(!$item) {
return;
Expand All @@ -189,8 +189,8 @@ public static function blpop(array $queues, $timeout)
$queue = substr($item[0], strlen(self::redis()->getPrefix() . 'queue:'));

return array(
'queue' => $queue,
'payload' => json_decode($item[1], true)
'queue' => $queue,
'payload' => json_decode($item[1], true),
);
}

Expand Down Expand Up @@ -334,14 +334,14 @@ private static function matchItem($string, $items)
if($decoded['class'] == $val) {
return true;
}
# class name with args , example: item[0] = ['class' => {'foo' => 1, 'bar' => 2}]
# class name with args , example: item[0] = ['class' => {'foo' => 1, 'bar' => 2}]
} elseif (is_array($val)) {
$decodedArgs = (array)$decoded['args'][0];
$decodedArgs = (array) $decoded['args'][0];
if ($decoded['class'] == $key &&
count($decodedArgs) > 0 && count(array_diff($decodedArgs, $val)) == 0) {
return true;
}
# class name with ID, example: item[0] = ['class' => 'id']
# class name with ID, example: item[0] = ['class' => 'id']
} else {
if ($decoded['class'] == $key && $decoded['id'] == $val) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion lib/Resque/Failure/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct($payload, $exception, $worker, $queue)
$data->exception = get_class($exception);
$data->error = $exception->getMessage();
$data->backtrace = explode("\n", $exception->getTraceAsString());
$data->worker = (string)$worker;
$data->worker = (string) $worker;
$data->queue = $queue;
$data = json_encode($data);
Resque::redis()->rpush('failed', $data);
Expand Down
6 changes: 3 additions & 3 deletions lib/Resque/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function create($queue, $class, $args = null, $monitor = false, $i

if($args !== null && !is_array($args)) {
throw new InvalidArgumentException(
'Supplied $args must be an array.'
'Supplied $args must be an array.',
);
}
Resque::push($queue, array(
Expand Down Expand Up @@ -224,7 +224,7 @@ public function fail($exception)
$this->payload,
$exception,
$this->worker,
$this->queue
$this->queue,
);
Resque_Stat::incr('failed');
Resque_Stat::incr('failed:' . $this->worker);
Expand Down Expand Up @@ -253,7 +253,7 @@ public function recreate()
public function __toString()
{
$name = array(
'Job{' . $this->queue . '}'
'Job{' . $this->queue . '}',
);
if(!empty($this->payload['id'])) {
$name[] = 'ID: ' . $this->payload['id'];
Expand Down
4 changes: 2 additions & 2 deletions lib/Resque/Job/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public function create($className, $args, $queue)
{
if (!class_exists($className)) {
throw new Resque_Exception(
'Could not find job class ' . $className . '.'
'Could not find job class ' . $className . '.',
);
}

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.',
);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/Resque/Job/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Resque_Job_Status
*/
private static $completeStatuses = array(
self::STATUS_FAILED,
self::STATUS_COMPLETE
self::STATUS_COMPLETE,
);

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ public function isTracking()
return false;
}

if(!Resque::redis()->exists((string)$this)) {
if(!Resque::redis()->exists((string) $this)) {
$this->isTracking = false;
return false;
}
Expand All @@ -94,11 +94,11 @@ public function update($status)
'status' => $status,
'updated' => time(),
);
Resque::redis()->set((string)$this, json_encode($statusPacket));
Resque::redis()->set((string) $this, json_encode($statusPacket));

// Expire the status for completed jobs after 24 hours
if(in_array($status, self::$completeStatuses)) {
Resque::redis()->expire((string)$this, 86400);
Resque::redis()->expire((string) $this, 86400);
}
}

Expand All @@ -114,7 +114,7 @@ public function get()
return false;
}

$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
$statusPacket = json_decode(Resque::redis()->get((string) $this), true);
if(!$statusPacket) {
return false;
}
Expand All @@ -127,7 +127,7 @@ public function get()
*/
public function stop()
{
Resque::redis()->del((string)$this);
Resque::redis()->del((string) $this);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/Resque/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public function log($level, string|\Stringable $message, array $context = []): v
if ($this->verbose) {
fwrite(
STDOUT,
'[' . $level . '] [' . date('H:i:s Y-m-d') . '] ' . $this->interpolate($message, $context) . PHP_EOL
'[' . $level . '] [' . date('H:i:s Y-m-d') . '] ' . $this->interpolate($message, $context) . PHP_EOL,
);
return;
}

if (!($level === Psr\Log\LogLevel::INFO || $level === Psr\Log\LogLevel::DEBUG)) {
fwrite(
STDOUT,
'[' . $level . '] ' . $this->interpolate($message, $context) . PHP_EOL
'[' . $level . '] ' . $this->interpolate($message, $context) . PHP_EOL,
);
}
}
Expand Down
11 changes: 8 additions & 3 deletions lib/Resque/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Resque_Redis
'zremrangebyscore',
'sort',
'rename',
'rpoplpush'
'rpoplpush',
);
// sinterstore
// sunion
Expand All @@ -92,6 +92,11 @@ class Resque_Redis
// mset
// renamenx

/**
* @var Credis_Client|Credis_Cluster
*/
public $driver;

/**
* Set Redis namespace (prefix) default: resque
* @param string $namespace
Expand All @@ -108,14 +113,14 @@ public static function prefix($namespace)
* @param string|array $server A DSN or array
* @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_Cluster or Credis_Client instance instantiated by you
* @param Credis_Client|Credis_Cluster $client Optional Credis_Cluster or Credis_Client instance instantiated by you
*/
public function __construct($server, $database = null, $client = null)
{
try {
if (is_array($server)) {
$this->driver = new Credis_Cluster($server);
} elseif (is_object($client)) {
} elseif ($client instanceof Credis_Client || $client instanceof Credis_Cluster) {
$this->driver = $client;
} else {
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
Expand Down
8 changes: 4 additions & 4 deletions lib/Resque/Stat.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Resque_Stat
*/
public static function get($stat)
{
return (int)Resque::redis()->get('stat:' . $stat);
return (int) Resque::redis()->get('stat:' . $stat);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function get($stat)
*/
public static function incr($stat, $by = 1)
{
return (bool)Resque::redis()->incrby('stat:' . $stat, $by);
return (bool) Resque::redis()->incrby('stat:' . $stat, $by);
}

/**
Expand All @@ -40,7 +40,7 @@ public static function incr($stat, $by = 1)
*/
public static function decr($stat, $by = 1)
{
return (bool)Resque::redis()->decrby('stat:' . $stat, $by);
return (bool) Resque::redis()->decrby('stat:' . $stat, $by);
}

/**
Expand All @@ -51,6 +51,6 @@ public static function decr($stat, $by = 1)
*/
public static function clear($stat)
{
return (bool)Resque::redis()->del('stat:' . $stat);
return (bool) Resque::redis()->del('stat:' . $stat);
}
}
20 changes: 10 additions & 10 deletions lib/Resque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function all()
*/
public static function exists($workerId)
{
return (bool)Resque::redis()->sismember('workers', $workerId);
return (bool) Resque::redis()->sismember('workers', $workerId);
}

/**
Expand Down Expand Up @@ -217,7 +217,7 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
$exitStatus = pcntl_wexitstatus($status);
if($exitStatus !== 0) {
$job->fail(new Resque_Job_DirtyExitException(
'Job exited with exit code ' . $exitStatus
'Job exited with exit code ' . $exitStatus,
));
}
}
Expand Down Expand Up @@ -429,11 +429,11 @@ public function pruneDeadWorkers()
$workers = self::all();
foreach($workers as $worker) {
if (is_object($worker)) {
list($host, $pid, $queues) = explode(':', (string)$worker, 3);
list($host, $pid, $queues) = explode(':', (string) $worker, 3);
if($host != $this->hostname || in_array($pid, $workerPids) || $pid == getmypid()) {
continue;
}
$this->logger->log(Psr\Log\LogLevel::INFO, 'Pruning dead worker: {worker}', array('worker' => (string)$worker));
$this->logger->log(Psr\Log\LogLevel::INFO, 'Pruning dead worker: {worker}', array('worker' => (string) $worker));
$worker->unregisterWorker();
}
}
Expand All @@ -460,8 +460,8 @@ public function workerPids()
*/
public function registerWorker()
{
Resque::redis()->sadd('workers', (string)$this);
Resque::redis()->set('worker:' . (string)$this . ':started', date('D M d H:i:s T Y'));
Resque::redis()->sadd('workers', (string) $this);
Resque::redis()->set('worker:' . (string) $this . ':started', date('D M d H:i:s T Y'));
}

/**
Expand All @@ -473,7 +473,7 @@ public function unregisterWorker()
$this->currentJob->fail(new Resque_Job_DirtyExitException());
}

$id = (string)$this;
$id = (string) $this;
Resque::redis()->srem('workers', $id);
Resque::redis()->del('worker:' . $id);
Resque::redis()->del('worker:' . $id . ':started');
Expand All @@ -494,7 +494,7 @@ public function workingOn(Resque_Job $job)
$data = json_encode(array(
'queue' => $job->queue,
'run_at' => date('D M d H:i:s T Y'),
'payload' => $job->payload
'payload' => $job->payload,
));
Resque::redis()->set('worker:' . $job->worker, $data);
}
Expand All @@ -507,8 +507,8 @@ public function doneWorking()
{
$this->currentJob = null;
Resque_Stat::incr('processed');
Resque_Stat::incr('processed:' . (string)$this);
Resque::redis()->del('worker:' . (string)$this);
Resque_Stat::incr('processed:' . (string) $this);
Resque::redis()->del('worker:' . (string) $this);
}

/**
Expand Down
Loading

0 comments on commit 10e215c

Please sign in to comment.