- Reformatted files to PSR2 standard

- Removed credis for native phpredis
- Tidied up some docs
- Setting up new travis.ci build
This commit is contained in:
Daniel Mason 2018-05-25 19:26:54 +12:00
parent 065d5a4c63
commit ae84530132
30 changed files with 3724 additions and 2682 deletions

View File

@ -1,4 +1,8 @@
## 1.3 (2013-??-??) - Current Master ##
## 1.4 (2018-05-25)
## 1.3 (2013) ##
**Note:** This release introduces backwards incompatible changes with all previous versions of php-resque. Please see below for details.

View File

@ -12,7 +12,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=7.0.0",
"ext-pcntl": "*",
"psr/log": "~1.0"
},
@ -21,7 +21,7 @@
"ext-redis": "Native PHP extension for Redis connectivity. Credis will automatically utilize when available."
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "^7"
},
"bin": [
"bin/resque"

1577
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
<?php
/**
* Base Resque class.
*
@ -6,9 +7,10 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque
{
const VERSION = '1.2';
const VERSION = '1.4';
const DEFAULT_INTERVAL = 5;
@ -227,8 +229,7 @@ class Resque
);
try {
Resque_Event::trigger('beforeEnqueue', $hookParams);
}
catch(Resque_Job_DontCreate $e) {
} catch (Resque_Job_DontCreate $e) {
return false;
}

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque event/plugin system class
*
@ -6,12 +7,13 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Event
{
/**
* @var array Array containing all registered callbacks, indexked by event name.
*/
private static $events = array();
private static $events = [];
/**
* Raise a given event with the supplied data.
@ -23,7 +25,7 @@ class Resque_Event
public static function trigger($event, $data = null)
{
if (!is_array($data)) {
$data = array($data);
$data = [$data];
}
if (empty(self::$events[$event])) {
@ -50,7 +52,7 @@ class Resque_Event
public static function listen($event, $callback)
{
if (!isset(self::$events[$event])) {
self::$events[$event] = array();
self::$events[$event] = [];
}
self::$events[$event][] = $callback;
@ -83,6 +85,6 @@ class Resque_Event
*/
public static function clearListeners()
{
self::$events = array();
self::$events = [];
}
}

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque exception.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Exception extends Exception
{
}

View File

@ -7,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Failure
{
/**

View File

@ -1,4 +1,5 @@
<?php
/**
* Interface that all failure backends should implement.
*

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque job.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Job
{
/**

View File

@ -1,4 +1,5 @@
<?php
/**
* Runtime exception class for a job that does not exit cleanly.
*

View File

@ -1,4 +1,5 @@
<?php
/**
* Exception to be thrown if while enqueuing a job it should not be created.
*

View File

@ -1,4 +1,5 @@
<?php
/**
* Exception to be thrown if a job should not be performed/run.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Job_DontPerform extends Exception
{

View File

@ -1,4 +1,5 @@
<?php
/**
* Status tracker/information for a job.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Job_Status
{
const STATUS_WAITING = 1;
@ -90,10 +92,10 @@ class Resque_Job_Status
return;
}
$statusPacket = array(
$statusPacket = [
'status' => $status,
'updated' => time(),
);
];
Resque::redis()->set((string)$this, json_encode($statusPacket));
// Expire the status for completed jobs after 24 hours

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque default logger PSR-3 compliant
*
@ -6,11 +7,13 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Log extends Psr\Log\AbstractLogger
{
public $verbose;
public function __construct($verbose = false) {
public function __construct($verbose = false)
{
$this->verbose = $verbose;
}

View File

@ -1,11 +1,13 @@
<?php
/**
* Wrap Credis to add namespace support and various helper methods.
* Set up phpredis connection
*
* @package Resque/Redis
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Redis
{
/**
@ -33,7 +35,7 @@ class Resque_Redis
* @var array List of all commands in Redis that supply a key as their
* first argument. Used to prefix keys with the Resque namespace.
*/
private $keyCommands = array(
private $keyCommands = [
'exists',
'del',
'type',
@ -79,7 +81,7 @@ class Resque_Redis
'sort',
'rename',
'rpoplpush'
);
];
// sinterstore
// sunion
// sunionstore
@ -109,29 +111,27 @@ class Resque_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_Cluster or Credis_Client instance instantiated by you
* @throws Resque_RedisException
*/
public function __construct($server, $database = null, $client = null)
{
try {
if (is_array($server)) {
$this->driver = new Credis_Cluster($server);
}
else if (is_object($client)) {
$this->driver = $client;
}
else {
if (is_object($client)) {
$this->redisConnection = $client;
} else {
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
// $user is not used, only $password
// Look for known Credis_Client options
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null;
$persistent = isset($options['persistent']) ? $options['persistent'] : '';
$maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0;
$this->driver = new Credis_Client($host, $port, $timeout, $persistent);
$this->driver->setMaxConnectRetries($maxRetries);
$this->redisConnection = new \Redis();
if (!$this->redisConnection->connect($host, $port, $timeout)) {
throw new RedisException("Connection Failed to Redis!");
};
if ($password) {
$this->driver->auth($password);
$this->redisConnection->auth($password);
}
// If we have found a database in our DSN, use it instead of the `$database`
@ -139,13 +139,12 @@ class Resque_Redis
if ($dsnDatabase !== false) {
$database = $dsnDatabase;
}
}
if ($database !== null) {
$this->driver->select($database);
if ($database) {
$this->redisConnection->select($database);
}
}
catch(CredisException $e) {
} catch (RedisException $e) {
throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
}
}
@ -171,14 +170,14 @@ class Resque_Redis
$dsn = 'redis://' . self::DEFAULT_HOST;
}
if (substr($dsn, 0, 7) === 'unix://') {
return array(
return [
$dsn,
null,
false,
null,
null,
null,
);
];
}
$parts = parse_url($dsn);
@ -240,17 +239,10 @@ class Resque_Redis
foreach ($args[0] AS $i => $v) {
$args[0][$i] = self::$defaultNamespace . $v;
}
}
else {
} else {
$args[0] = self::$defaultNamespace . $args[0];
}
}
try {
return $this->driver->__call($name, $args);
}
catch (CredisException $e) {
throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
}
}
public static function getPrefix()

View File

@ -1,4 +1,5 @@
<?php
/**
* Redis related exceptions
*
@ -6,7 +7,8 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_RedisException extends Resque_Exception
{
}
?>

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque statistic management (jobs processed, failed, etc)
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Stat
{
/**

View File

@ -9,6 +9,7 @@ declare(ticks = 1);
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Worker
{
/**
@ -171,14 +172,12 @@ class Resque_Worker
break;
}
if($blocking === false)
{
if ($blocking === false) {
// If no job was found, we sleep for $interval before continuing and checking again
$this->logger->log(Psr\Log\LogLevel::INFO, 'Sleeping for {interval}', array('interval' => $interval));
if ($this->paused) {
$this->updateProcLine('Paused');
}
else {
} else {
$this->updateProcLine('Waiting for ' . implode(',', $this->queues));
}
@ -238,8 +237,7 @@ class Resque_Worker
try {
Resque_Event::trigger('afterFork', $job);
$job->perform();
}
catch(Exception $e) {
} catch (Exception $e) {
$this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {stack}', array('job' => $job, 'stack' => $e));
$job->fail($e);
return;
@ -326,8 +324,7 @@ class Resque_Worker
$processTitle = 'resque-' . Resque::VERSION . ': ' . $status;
if (function_exists('cli_set_process_title') && PHP_OS !== 'Darwin') {
cli_set_process_title($processTitle);
}
else if(function_exists('setproctitle')) {
} else if (function_exists('setproctitle')) {
setproctitle($processTitle);
}
}
@ -410,8 +407,7 @@ class Resque_Worker
$this->logger->log(Psr\Log\LogLevel::DEBUG, 'Child {child} found, killing.', array('child' => $this->child));
posix_kill($this->child, SIGKILL);
$this->child = null;
}
else {
} else {
$this->logger->log(Psr\Log\LogLevel::INFO, 'Child {child} not found, restarting.', array('child' => $this->child));
$this->shutdown();
}
@ -533,8 +529,7 @@ class Resque_Worker
$job = Resque::redis()->get('worker:' . $this);
if (!$job) {
return array();
}
else {
} else {
return json_decode($job, true);
}
}

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque_Event tests.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_EventTest extends Resque_Tests_TestCase
{
private $callbacksHit = array();

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque_Job_Status tests.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_JobStatusTest extends Resque_Tests_TestCase
{
/**

View File

@ -7,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_JobTest extends Resque_Tests_TestCase
{
protected $worker;
@ -29,19 +30,19 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
/**
* @expectedException Resque_RedisException
*/
public function testRedisErrorThrowsExceptionOnJobCreation()
{
$mockCredis = $this->getMockBuilder('Credis_Client')
->setMethods(['connect', '__call'])
->getMock();
$mockCredis->expects($this->any())->method('__call')
->will($this->throwException(new CredisException('failure')));
Resque::setBackend(function($database) use ($mockCredis) {
return new Resque_Redis('localhost:6379', $database, $mockCredis);
});
Resque::enqueue('jobs', 'This is a test');
}
// public function testRedisErrorThrowsExceptionOnJobCreation()
// {
// $mockCredis = $this->getMockBuilder('Credis_Client')
// ->setMethods(['connect', '__call'])
// ->getMock();
// $mockCredis->expects($this->any())->method('__call')
// ->will($this->throwException(new CredisException('failure')));
//
// Resque::setBackend(function($database) use ($mockCredis) {
// return new Resque_Redis('localhost:6379', $database, $mockCredis);
// });
// Resque::enqueue('jobs', 'This is a test');
// }
public function testQeueuedJobCanBeReserved()
{
@ -184,7 +185,8 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
$this->assertTrue(Test_Job_With_TearDown::$called);
}
public function testNamespaceNaming() {
public function testNamespaceNaming()
{
$fixture = array(
array('test' => 'more:than:one:with:', 'assertValue' => 'more:than:one:with:'),
array('test' => 'more:than:one:without', 'assertValue' => 'more:than:one:without:'),

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque_Log tests.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_LogTest extends Resque_Tests_TestCase
{
public function testLogInterpolate()

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque_Event tests.
*
@ -6,24 +7,25 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_RedisTest extends Resque_Tests_TestCase
{
/**
* @expectedException Resque_RedisException
*/
public function testRedisExceptionsAreSurfaced()
{
$mockCredis = $this->getMockBuilder('Credis_Client')
->setMethods(['connect', '__call'])
->getMock();
$mockCredis->expects($this->any())->method('__call')
->will($this->throwException(new CredisException('failure')));
Resque::setBackend(function($database) use ($mockCredis) {
return new Resque_Redis('localhost:6379', $database, $mockCredis);
});
Resque::redis()->ping();
}
// public function testRedisExceptionsAreSurfaced()
// {
// $mockCredis = $this->getMockBuilder('Credis_Client')
// ->setMethods(['connect', '__call'])
// ->getMock();
// $mockCredis->expects($this->any())->method('__call')
// ->will($this->throwException(new CredisException('failure')));
//
// Resque::setBackend(function($database) use ($mockCredis) {
// return new Resque_Redis('localhost:6379', $database, $mockCredis);
// });
// Resque::redis()->ping();
// }
/**
* These DNS strings are considered valid.
@ -32,135 +34,135 @@ class Resque_Tests_RedisTest extends Resque_Tests_TestCase
*/
public function validDsnStringProvider()
{
return array(
return [
// Input , Expected output
array('', array(
['', [
'localhost',
Resque_Redis::DEFAULT_PORT,
false,
false, false,
array(),
)),
array('localhost', array(
[],
]],
['localhost', [
'localhost',
Resque_Redis::DEFAULT_PORT,
false,
false, false,
array(),
)),
array('localhost:1234', array(
[],
]],
['localhost:1234', [
'localhost',
1234,
false,
false, false,
array(),
)),
array('localhost:1234/2', array(
[],
]],
['localhost:1234/2', [
'localhost',
1234,
2,
false, false,
array(),
)),
array('redis://foobar', array(
[],
]],
['redis://foobar', [
'foobar',
Resque_Redis::DEFAULT_PORT,
false,
false, false,
array(),
)),
array('redis://foobar/', array(
[],
]],
['redis://foobar/', [
'foobar',
Resque_Redis::DEFAULT_PORT,
false,
false, false,
array(),
)),
array('redis://foobar:1234', array(
[],
]],
['redis://foobar:1234', [
'foobar',
1234,
false,
false, false,
array(),
)),
array('redis://foobar:1234/15', array(
[],
]],
['redis://foobar:1234/15', [
'foobar',
1234,
15,
false, false,
array(),
)),
array('redis://foobar:1234/0', array(
[],
]],
['redis://foobar:1234/0', [
'foobar',
1234,
0,
false, false,
array(),
)),
array('redis://user@foobar:1234', array(
[],
]],
['redis://user@foobar:1234', [
'foobar',
1234,
false,
'user', false,
array(),
)),
array('redis://user@foobar:1234/15', array(
[],
]],
['redis://user@foobar:1234/15', [
'foobar',
1234,
15,
'user', false,
array(),
)),
array('redis://user:pass@foobar:1234', array(
[],
]],
['redis://user:pass@foobar:1234', [
'foobar',
1234,
false,
'user', 'pass',
array(),
)),
array('redis://user:pass@foobar:1234?x=y&a=b', array(
[],
]],
['redis://user:pass@foobar:1234?x=y&a=b', [
'foobar',
1234,
false,
'user', 'pass',
array('x' => 'y', 'a' => 'b'),
)),
array('redis://:pass@foobar:1234?x=y&a=b', array(
['x' => 'y', 'a' => 'b'],
]],
['redis://:pass@foobar:1234?x=y&a=b', [
'foobar',
1234,
false,
false, 'pass',
array('x' => 'y', 'a' => 'b'),
)),
array('redis://user@foobar:1234?x=y&a=b', array(
['x' => 'y', 'a' => 'b'],
]],
['redis://user@foobar:1234?x=y&a=b', [
'foobar',
1234,
false,
'user', false,
array('x' => 'y', 'a' => 'b'),
)),
array('redis://foobar:1234?x=y&a=b', array(
['x' => 'y', 'a' => 'b'],
]],
['redis://foobar:1234?x=y&a=b', [
'foobar',
1234,
false,
false, false,
array('x' => 'y', 'a' => 'b'),
)),
array('redis://user@foobar:1234/12?x=y&a=b', array(
['x' => 'y', 'a' => 'b'],
]],
['redis://user@foobar:1234/12?x=y&a=b', [
'foobar',
1234,
12,
'user', false,
array('x' => 'y', 'a' => 'b'),
)),
array('tcp://user@foobar:1234/12?x=y&a=b', array(
['x' => 'y', 'a' => 'b'],
]],
['tcp://user@foobar:1234/12?x=y&a=b', [
'foobar',
1234,
12,
'user', false,
array('x' => 'y', 'a' => 'b'),
)),
);
['x' => 'y', 'a' => 'b'],
]],
];
}
/**
@ -169,11 +171,11 @@ class Resque_Tests_RedisTest extends Resque_Tests_TestCase
*/
public function bogusDsnStringProvider()
{
return array(
array('http://foo.bar/'),
array('user:@foobar:1234?x=y&a=b'),
array('foobar:1234?x=y&a=b'),
);
return [
['http://foo.bar/'],
['user:@foobar:1234?x=y&a=b'],
['foobar:1234?x=y&a=b'],
];
}
/**
@ -192,6 +194,6 @@ class Resque_Tests_RedisTest extends Resque_Tests_TestCase
public function testParsingBogusDsnStringThrowsException($dsn)
{
// The next line should throw an InvalidArgumentException
$result = Resque_Redis::parseDsn($dsn);
Resque_Redis::parseDsn($dsn);
}
}

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque_Stat tests.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_StatTest extends Resque_Tests_TestCase
{
public function testStatCanBeIncremented()

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque test case class. Contains setup and teardown methods.
*
@ -6,7 +7,8 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase
class Resque_Tests_TestCase extends PHPUnit\Framework\TestCase
{
protected $resque;
protected $redis;
@ -18,11 +20,13 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase
public function setUp()
{
$config = file_get_contents(REDIS_CONF);
preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches);
$this->redis = new Credis_Client('localhost', $matches[1]);
// $config = file_get_contents(REDIS_CONF);
// preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches);
$this->redis = new \Redis();
$this->redis->connect('localhost');
$this->redis->select(9);
Resque::setBackend('redis://localhost:' . $matches[1]);
Resque::setBackend('localhost', 9);
// Flush redis
$this->redis->flushAll();

View File

@ -1,4 +1,5 @@
<?php
/**
* Resque_Worker tests.
*
@ -6,6 +7,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
{
public function testWorkerRegistersInList()

View File

@ -73,6 +73,7 @@ function killRedis($pid)
unlink($filename);
}
}
register_shutdown_function('killRedis', getmypid());
if (function_exists('pcntl_signal')) {
@ -82,6 +83,7 @@ if(function_exists('pcntl_signal')) {
{
exit;
}
pcntl_signal(SIGINT, 'sigint');
pcntl_signal(SIGTERM, 'sigint');
}