Added 48 hour TTL to worker:xxx:started keys to prevent db pollution

This commit is contained in:
Daniel Mason 2018-05-30 08:25:02 +12:00
parent b235fce1bd
commit 493d6dc6d8
17 changed files with 158 additions and 141 deletions

View File

@ -1,15 +1,17 @@
## 1.4.1 (2018-05-29) ## 1.4.1 (2018-05-29)
- Updated travis builds to run on PHP 7.0, 7.1 and 7.2. - Updated travis builds to run on PHP 7.0, 7.1 and 7.2.
- Added ability to specify multiple log levels. [DEBUG/INFO/NOTICE/WARNING/ERROR/CRITICAL/ALERT/EMERGENCY] - Added ability to specify multiple log levels. [DEBUG/INFO/NOTICE/WARNING/ERROR/CRITICAL/ALERT/EMERGENCY]
- Default `LOGLEVEL` is now `WARNING`. - Default `LOGLEVEL` is now `WARNING`.
- Removed VERBOSE / VVERBOSE flags. - Removed VERBOSE / VVERBOSE flags and always output timestamp in logs.
- Enabled date/time logging by default. - Added 48 hour TTL to the worker started timestamp to prevent db pollution on server termination.
## 1.4 (2018-05-25) ## 1.4 (2018-05-25)
- Forked from chrisboulton/php-resque.
- Replaced credis (rather unmaintained) in favour of phpredis. - Replaced credis (rather unmaintained) in favour of phpredis.
- Reformatted codebase to be PSR2 compliant. - Reformatted codebase to be PSR2 compliant.
## 1.3 (2013) ##
## 1.3 (2013)
**Note:** This release introduces backwards incompatible changes with all previous versions of php-resque. Please see below for details. **Note:** This release introduces backwards incompatible changes with all previous versions of php-resque. Please see below for details.

View File

@ -1,3 +1,4 @@
<!--suppress PhingDomInspection -->
<project name="php-resque" default="build"> <project name="php-resque" default="build">
<target name="clean"> <target name="clean">
<delete dir="${basedir}/build" /> <delete dir="${basedir}/build" />
@ -7,7 +8,7 @@
<mkdir dir="${basedir}/build/logs" /> <mkdir dir="${basedir}/build/logs" />
</target> </target>
<target name="phpunit"> <target name="phpunit">
<exec dir="${basedir}" executable="phpunit" failonerror="true"> <exec dir="${basedir}" executable="phpunit">
<arg line="--log-junit ${basedir}/build/logs/phpunit.xml <arg line="--log-junit ${basedir}/build/logs/phpunit.xml
--coverage-clover ${basedir}/build/logs/clover.xml --coverage-clover ${basedir}/build/logs/clover.xml
--coverage-html ${basedir}/build/coverage" /> --coverage-html ${basedir}/build/coverage" />

View File

@ -2,11 +2,11 @@
// Find and initialize Composer // Find and initialize Composer
// NOTE: You should NOT use this when developing against php-resque. // NOTE: You should NOT use this when developing against php-resque.
// The autoload code below is specifically for this demo. // The autoload code below is specifically for this demo.
$files = array( $files = [
__DIR__ . '/../../vendor/autoload.php', __DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php', __DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../vendor/autoload.php',
); ];
$found = false; $found = false;
foreach ($files as $file) { foreach ($files as $file) {

View File

@ -1,4 +1,4 @@
<?php <?php /** @noinspection PhpUndefinedFunctionInspection */
class PHP_Error_Job class PHP_Error_Job
{ {

View File

@ -1,12 +1,12 @@
<?php <?php
// Somewhere in our application, we need to register: // Somewhere in our application, we need to register:
Resque_Event::listen('afterEnqueue', array('My_Resque_Plugin', 'afterEnqueue')); Resque_Event::listen('afterEnqueue', ['My_Resque_Plugin', 'afterEnqueue']);
Resque_Event::listen('beforeFirstFork', array('My_Resque_Plugin', 'beforeFirstFork')); Resque_Event::listen('beforeFirstFork', ['My_Resque_Plugin', 'beforeFirstFork']);
Resque_Event::listen('beforeFork', array('My_Resque_Plugin', 'beforeFork')); Resque_Event::listen('beforeFork', ['My_Resque_Plugin', 'beforeFork']);
Resque_Event::listen('afterFork', array('My_Resque_Plugin', 'afterFork')); Resque_Event::listen('afterFork', ['My_Resque_Plugin', 'afterFork']);
Resque_Event::listen('beforePerform', array('My_Resque_Plugin', 'beforePerform')); Resque_Event::listen('beforePerform', ['My_Resque_Plugin', 'beforePerform']);
Resque_Event::listen('afterPerform', array('My_Resque_Plugin', 'afterPerform')); Resque_Event::listen('afterPerform', ['My_Resque_Plugin', 'afterPerform']);
Resque_Event::listen('onFailure', array('My_Resque_Plugin', 'onFailure')); Resque_Event::listen('onFailure', ['My_Resque_Plugin', 'onFailure']);
class Sample_Resque_Plugin class Sample_Resque_Plugin
{ {

View File

@ -1,4 +1,4 @@
<?php <?php /** @noinspection ALL */
/** /**
* Base Resque class. * Base Resque class.
@ -51,6 +51,7 @@ class Resque
* Return an instance of the Resque_Redis class instantiated for Resque. * Return an instance of the Resque_Redis class instantiated for Resque.
* *
* @return Resque_Redis Instance of Resque_Redis. * @return Resque_Redis Instance of Resque_Redis.
* @throws Resque_RedisException
*/ */
public static function redis() public static function redis()
{ {
@ -169,7 +170,7 @@ class Resque
* *
* @param array $queues * @param array $queues
* @param int $timeout * @param int $timeout
* @return null|array Decoded item from the queue. * @return array|null|void
*/ */
public static function blpop(array $queues, $timeout) public static function blpop(array $queues, $timeout)
{ {
@ -181,7 +182,7 @@ class Resque
$item = self::redis()->blpop($list, (int)$timeout); $item = self::redis()->blpop($list, (int)$timeout);
if (!$item) { if (!$item) {
return; return false;
} }
/** /**
@ -244,7 +245,7 @@ class Resque
* Reserve and return the next available job in the specified queue. * Reserve and return the next available job in the specified queue.
* *
* @param string $queue Queue to fetch next available job from. * @param string $queue Queue to fetch next available job from.
* @return Resque_Job Instance of Resque_Job to be processed, false if none or error. * @return false|object|Resque_Job
*/ */
public static function reserve($queue) public static function reserve($queue)
{ {
@ -325,7 +326,9 @@ class Resque
* @params string $string redis result in json * @params string $string redis result in json
* @params $items * @params $items
* *
* @return (bool) * @param $string
* @param $items
* @return bool (bool)
*/ */
private static function matchItem($string, $items) private static function matchItem($string, $items)
{ {
@ -360,7 +363,9 @@ class Resque
* @private * @private
* *
* @params string $queue the name of the queue * @params string $queue the name of the queue
* @param $queue
* @return integer number of deleted items belongs to this list * @return integer number of deleted items belongs to this list
* @throws Resque_RedisException
*/ */
private static function removeList($queue) private static function removeList($queue)
{ {

View File

@ -18,7 +18,7 @@ class Resque_Failure
/** /**
* Create a new failed job on the backend. * Create a new failed job on the backend.
* *
* @param object $payload The contents of the job that has just failed. * @param array $payload The contents of the job that has just failed.
* @param \Exception $exception The exception generated when the job failed to run. * @param \Exception $exception The exception generated when the job failed to run.
* @param \Resque_Worker $worker Instance of Resque_Worker that was running this job when it failed. * @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. * @param string $queue The name of the queue that this job was fetched from.
@ -32,7 +32,7 @@ class Resque_Failure
/** /**
* Return an instance of the backend for saving job failures. * Return an instance of the backend for saving job failures.
* *
* @return object Instance of backend object. * @return object|string
*/ */
public static function getBackend() public static function getBackend()
{ {

View File

@ -70,12 +70,12 @@ class Resque_Job
'Supplied $args must be an array.' 'Supplied $args must be an array.'
); );
} }
Resque::push($queue, array( Resque::push($queue, [
'class' => $class, 'class' => $class,
'args' => array($args), 'args' => [$args],
'id' => $id, 'id' => $id,
'queue_time' => microtime(true), 'queue_time' => microtime(true),
)); ]);
if ($monitor) { if ($monitor) {
Resque_Job_Status::create($id); Resque_Job_Status::create($id);
@ -153,7 +153,7 @@ class Resque_Job
public function getArguments() public function getArguments()
{ {
if (!isset($this->payload['args'])) { if (!isset($this->payload['args'])) {
return array(); return [];
} }
return $this->payload['args'][0]; return $this->payload['args'][0];
@ -162,7 +162,6 @@ class Resque_Job
/** /**
* Get the instantiated object for this job that will be performing work. * Get the instantiated object for this job that will be performing work.
* @return Resque_JobInterface Instance of the object that this job belongs to. * @return Resque_JobInterface Instance of the object that this job belongs to.
* @throws Resque_Exception
*/ */
public function getInstance() public function getInstance()
{ {
@ -200,7 +199,7 @@ class Resque_Job
Resque_Event::trigger('afterPerform', $this); Resque_Event::trigger('afterPerform', $this);
} // beforePerform/setUp have said don't perform this job. Return. } // beforePerform/setUp have said don't perform this job. Return.
catch (Resque_Job_DontPerform $e) { /** @noinspection PhpRedundantCatchClauseInspection */ catch (Resque_Job_DontPerform $e) {
return false; return false;
} }
@ -214,10 +213,10 @@ class Resque_Job
*/ */
public function fail($exception) public function fail($exception)
{ {
Resque_Event::trigger('onFailure', array( Resque_Event::trigger('onFailure', [
'exception' => $exception, 'exception' => $exception,
'job' => $this, 'job' => $this,
)); ]);
$this->updateStatus(Resque_Job_Status::STATUS_FAILED); $this->updateStatus(Resque_Job_Status::STATUS_FAILED);
Resque_Failure::create( Resque_Failure::create(
@ -252,9 +251,9 @@ class Resque_Job
*/ */
public function __toString() public function __toString()
{ {
$name = array( $name = [
'Job{' . $this->queue . '}' 'Job{' . $this->queue . '}'
); ];
if (!empty($this->payload['id'])) { if (!empty($this->payload['id'])) {
$name[] = 'ID: ' . $this->payload['id']; $name[] = 'ID: ' . $this->payload['id'];
} }

View File

@ -29,10 +29,10 @@ class Resque_Job_Status
/** /**
* @var array Array of statuses that are considered final/complete. * @var array Array of statuses that are considered final/complete.
*/ */
private static $completeStatuses = array( private static $completeStatuses = [
self::STATUS_FAILED, self::STATUS_FAILED,
self::STATUS_COMPLETE self::STATUS_COMPLETE
); ];
/** /**
* Setup a new instance of the job monitor class for the supplied job ID. * Setup a new instance of the job monitor class for the supplied job ID.
@ -52,11 +52,11 @@ class Resque_Job_Status
*/ */
public static function create($id) public static function create($id)
{ {
$statusPacket = array( $statusPacket = [
'status' => self::STATUS_WAITING, 'status' => self::STATUS_WAITING,
'updated' => time(), 'updated' => time(),
'started' => time(), 'started' => time(),
); ];
Resque::redis()->set('job:' . $id . ':status', json_encode($statusPacket)); Resque::redis()->set('job:' . $id . ':status', json_encode($statusPacket));
} }

View File

@ -119,6 +119,7 @@ class Resque_Redis
if (is_object($client)) { if (is_object($client)) {
$this->redisConnection = $client; $this->redisConnection = $client;
} else { } else {
/** @noinspection PhpUnusedLocalVariableInspection */
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server); list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
// $user is not used, only $password // $user is not used, only $password
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null; $timeout = isset($options['timeout']) ? intval($options['timeout']) : null;

View File

@ -101,6 +101,7 @@ class Resque_Worker
* *
* @param string $workerId ID of the worker. * @param string $workerId ID of the worker.
* @return boolean True if the worker exists, false if not. * @return boolean True if the worker exists, false if not.
* @throws Resque_RedisException
*/ */
public static function exists($workerId) public static function exists($workerId)
{ {
@ -111,14 +112,15 @@ class Resque_Worker
* Given a worker ID, find it and return an instantiated worker class for it. * Given a worker ID, find it and return an instantiated worker class for it.
* *
* @param string $workerId The ID of the worker. * @param string $workerId The ID of the worker.
* @return Resque_Worker Instance of the worker. False if the worker does not exist. * @return bool|Resque_Worker
* @throws Resque_RedisException
*/ */
public static function find($workerId) public static function find($workerId)
{ {
if (!self::exists($workerId) || false === strpos($workerId, ":")) { if (!self::exists($workerId) || false === strpos($workerId, ":")) {
return false; return false;
} }
/** @noinspection PhpUnusedLocalVariableInspection */
list($hostname, $pid, $queues) = explode(':', $workerId, 3); list($hostname, $pid, $queues) = explode(':', $workerId, 3);
$queues = explode(',', $queues); $queues = explode(',', $queues);
$worker = new self($queues); $worker = new self($queues);
@ -143,6 +145,8 @@ class Resque_Worker
* Queues are checked every $interval (seconds) for new jobs. * Queues are checked every $interval (seconds) for new jobs.
* *
* @param int $interval How often to check for new jobs across the queues. * @param int $interval How often to check for new jobs across the queues.
* @param bool $blocking
* @throws Resque_RedisException
*/ */
public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false) public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
{ {
@ -199,6 +203,7 @@ class Resque_Worker
$status = 'Processing ' . $job->queue . ' since ' . strftime('%F %T'); $status = 'Processing ' . $job->queue . ' since ' . strftime('%F %T');
$this->updateProcLine($status); $this->updateProcLine($status);
$this->logger->log(Psr\Log\LogLevel::INFO, $status); $this->logger->log(Psr\Log\LogLevel::INFO, $status);
/** @noinspection PhpParamsInspection */
$this->perform($job); $this->perform($job);
if ($this->child === 0) { if ($this->child === 0) {
exit(0); exit(0);
@ -456,11 +461,12 @@ class Resque_Worker
/** /**
* Register this worker in Redis. * Register this worker in Redis.
* 48 hour TTL so we don't pollute the db on server termination.
*/ */
public function registerWorker() public function registerWorker()
{ {
Resque::redis()->sadd('workers', (string)$this); Resque::redis()->sadd('workers', (string)$this);
Resque::redis()->set('worker:' . (string)$this . ':started', strftime('%a %b %d %H:%M:%S %Z %Y')); Resque::redis()->setex('worker:' . (string)$this . ':started', 172800, strftime('%a %b %d %H:%M:%S %Z %Y'));
} }
/** /**
@ -483,7 +489,8 @@ class Resque_Worker
/** /**
* Tell Redis which job we're currently working on. * Tell Redis which job we're currently working on.
* *
* @param object $job Resque_Job instance containing the job we're working on. * @param Resque_Job $job Resque_Job instance containing the job we're working on.
* @throws Resque_RedisException
*/ */
public function workingOn(Resque_Job $job) public function workingOn(Resque_Job $job)
{ {

View File

@ -10,7 +10,7 @@
class Resque_Tests_EventTest extends Resque_Tests_TestCase class Resque_Tests_EventTest extends Resque_Tests_TestCase
{ {
private $callbacksHit = array(); private $callbacksHit = [];
private $worker; private $worker;
public function setUp() public function setUp()
@ -26,17 +26,17 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
public function tearDown() public function tearDown()
{ {
Resque_Event::clearListeners(); Resque_Event::clearListeners();
$this->callbacksHit = array(); $this->callbacksHit = [];
} }
public function getEventTestJob() public function getEventTestJob()
{ {
$payload = array( $payload = [
'class' => 'Test_Job', 'class' => 'Test_Job',
'args' => array( 'args' => [
array('somevar'), ['somevar'],
), ],
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$job->worker = $this->worker; $job->worker = $this->worker;
return $job; return $job;
@ -44,19 +44,21 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
public function eventCallbackProvider() public function eventCallbackProvider()
{ {
return array( return [
array('beforePerform', 'beforePerformEventCallback'), ['beforePerform', 'beforePerformEventCallback'],
array('afterPerform', 'afterPerformEventCallback'), ['afterPerform', 'afterPerformEventCallback'],
array('afterFork', 'afterForkEventCallback'), ['afterFork', 'afterForkEventCallback'],
); ];
} }
/** /**
* @dataProvider eventCallbackProvider * @dataProvider eventCallbackProvider
* @param $event
* @param $callback
*/ */
public function testEventCallbacksFire($event, $callback) public function testEventCallbacksFire($event, $callback)
{ {
Resque_Event::listen($event, array($this, $callback)); Resque_Event::listen($event, [$this, $callback]);
$job = $this->getEventTestJob(); $job = $this->getEventTestJob();
$this->worker->perform($job); $this->worker->perform($job);
@ -70,11 +72,11 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
$event = 'beforeFork'; $event = 'beforeFork';
$callback = 'beforeForkEventCallback'; $callback = 'beforeForkEventCallback';
Resque_Event::listen($event, array($this, $callback)); Resque_Event::listen($event, [$this, $callback]);
Resque::enqueue('jobs', 'Test_Job', array( Resque::enqueue('jobs', 'Test_Job', [
'somevar' 'somevar'
)); ]);
$job = $this->getEventTestJob(); $this->getEventTestJob();
$this->worker->work(0); $this->worker->work(0);
$this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called'); $this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called');
} }
@ -84,17 +86,17 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
$event = 'beforeEnqueue'; $event = 'beforeEnqueue';
$callback = 'beforeEnqueueEventCallback'; $callback = 'beforeEnqueueEventCallback';
Resque_Event::listen($event, array($this, $callback)); Resque_Event::listen($event, [$this, $callback]);
Resque::enqueue('jobs', 'Test_Job', array( Resque::enqueue('jobs', 'Test_Job', [
'somevar' 'somevar'
)); ]);
$this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called'); $this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called');
} }
public function testBeforePerformEventCanStopWork() public function testBeforePerformEventCanStopWork()
{ {
$callback = 'beforePerformEventDontPerformCallback'; $callback = 'beforePerformEventDontPerformCallback';
Resque_Event::listen('beforePerform', array($this, $callback)); Resque_Event::listen('beforePerform', [$this, $callback]);
$job = $this->getEventTestJob(); $job = $this->getEventTestJob();
@ -106,8 +108,8 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
public function testBeforeEnqueueEventStopsJobCreation() public function testBeforeEnqueueEventStopsJobCreation()
{ {
$callback = 'beforeEnqueueEventDontCreateCallback'; $callback = 'beforeEnqueueEventDontCreateCallback';
Resque_Event::listen('beforeEnqueue', array($this, $callback)); Resque_Event::listen('beforeEnqueue', [$this, $callback]);
Resque_Event::listen('afterEnqueue', array($this, 'afterEnqueueEventCallback')); Resque_Event::listen('afterEnqueue', [$this, 'afterEnqueueEventCallback']);
$result = Resque::enqueue('test_job', 'TestClass'); $result = Resque::enqueue('test_job', 'TestClass');
$this->assertContains($callback, $this->callbacksHit, $callback . ' callback was not called'); $this->assertContains($callback, $this->callbacksHit, $callback . ' callback was not called');
@ -120,10 +122,10 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
$callback = 'afterEnqueueEventCallback'; $callback = 'afterEnqueueEventCallback';
$event = 'afterEnqueue'; $event = 'afterEnqueue';
Resque_Event::listen($event, array($this, $callback)); Resque_Event::listen($event, [$this, $callback]);
Resque::enqueue('jobs', 'Test_Job', array( Resque::enqueue('jobs', 'Test_Job', [
'somevar' 'somevar'
)); ]);
$this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called'); $this->assertContains($callback, $this->callbacksHit, $event . ' callback (' . $callback . ') was not called');
} }
@ -132,8 +134,8 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
$callback = 'beforePerformEventCallback'; $callback = 'beforePerformEventCallback';
$event = 'beforePerform'; $event = 'beforePerform';
Resque_Event::listen($event, array($this, $callback)); Resque_Event::listen($event, [$this, $callback]);
Resque_Event::stopListening($event, array($this, $callback)); Resque_Event::stopListening($event, [$this, $callback]);
$job = $this->getEventTestJob(); $job = $this->getEventTestJob();
$this->worker->perform($job); $this->worker->perform($job);
@ -144,13 +146,13 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
); );
} }
public function beforePerformEventDontPerformCallback($instance) public function beforePerformEventDontPerformCallback()
{ {
$this->callbacksHit[] = __FUNCTION__; $this->callbacksHit[] = __FUNCTION__;
throw new Resque_Job_DontPerform(); throw new Resque_Job_DontPerform();
} }
public function beforeEnqueueEventDontCreateCallback($queue, $class, $args, $track = false) public function beforeEnqueueEventDontCreateCallback()
{ {
$this->callbacksHit[] = __FUNCTION__; $this->callbacksHit[] = __FUNCTION__;
throw new Resque_Job_DontCreate(); throw new Resque_Job_DontCreate();
@ -170,9 +172,9 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
{ {
$this->callbacksHit[] = __FUNCTION__; $this->callbacksHit[] = __FUNCTION__;
$this->assertEquals('Test_Job', $class); $this->assertEquals('Test_Job', $class);
$this->assertEquals(array( $this->assertEquals([
'somevar', 'somevar',
), $args); ], $args);
} }
public function beforeEnqueueEventCallback($job) public function beforeEnqueueEventCallback($job)

View File

@ -33,7 +33,7 @@ class Resque_Tests_JobStatusTest extends Resque_Tests_TestCase
public function testJobStatusIsReturnedViaJobInstance() public function testJobStatusIsReturnedViaJobInstance()
{ {
$token = Resque::enqueue('jobs', 'Test_Job', null, true); Resque::enqueue('jobs', 'Test_Job', null, true);
$job = Resque_Job::reserve('jobs'); $job = Resque_Job::reserve('jobs');
$this->assertEquals(Resque_Job_Status::STATUS_WAITING, $job->getStatus()); $this->assertEquals(Resque_Job_Status::STATUS_WAITING, $job->getStatus());

View File

@ -51,17 +51,17 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testQueuedJobReturnsExactSamePassedInArguments() public function testQueuedJobReturnsExactSamePassedInArguments()
{ {
$args = array( $args = [
'int' => 123, 'int' => 123,
'numArray' => array( 'numArray' => [
1, 1,
2, 2,
), ],
'assocArray' => array( 'assocArray' => [
'key1' => 'value1', 'key1' => 'value1',
'key2' => 'value2' 'key2' => 'value2'
), ],
); ];
Resque::enqueue('jobs', 'Test_Job', $args); Resque::enqueue('jobs', 'Test_Job', $args);
$job = Resque_Job::reserve('jobs'); $job = Resque_Job::reserve('jobs');
@ -77,17 +77,17 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testRecreatedJobMatchesExistingJob() public function testRecreatedJobMatchesExistingJob()
{ {
$args = array( $args = [
'int' => 123, 'int' => 123,
'numArray' => array( 'numArray' => [
1, 1,
2, 2,
), ],
'assocArray' => array( 'assocArray' => [
'key1' => 'value1', 'key1' => 'value1',
'key2' => 'value2' 'key2' => 'value2'
), ],
); ];
Resque::enqueue('jobs', 'Test_Job', $args); Resque::enqueue('jobs', 'Test_Job', $args);
$job = Resque_Job::reserve('jobs'); $job = Resque_Job::reserve('jobs');
@ -103,10 +103,10 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testFailedJobExceptionsAreCaught() public function testFailedJobExceptionsAreCaught()
{ {
$payload = array( $payload = [
'class' => 'Failing_Job', 'class' => 'Failing_Job',
'args' => null 'args' => null
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$job->worker = $this->worker; $job->worker = $this->worker;
@ -140,13 +140,13 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testJobWithSetUpCallbackFiresSetUp() public function testJobWithSetUpCallbackFiresSetUp()
{ {
$payload = array( $payload = [
'class' => 'Test_Job_With_SetUp', 'class' => 'Test_Job_With_SetUp',
'args' => array( 'args' => [
'somevar', 'somevar',
'somevar2', 'somevar2',
), ],
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$job->perform(); $job->perform();
@ -155,13 +155,13 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testJobWithTearDownCallbackFiresTearDown() public function testJobWithTearDownCallbackFiresTearDown()
{ {
$payload = array( $payload = [
'class' => 'Test_Job_With_TearDown', 'class' => 'Test_Job_With_TearDown',
'args' => array( 'args' => [
'somevar', 'somevar',
'somevar2', 'somevar2',
), ],
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$job->perform(); $job->perform();
@ -170,12 +170,12 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testNamespaceNaming() public function testNamespaceNaming()
{ {
$fixture = array( $fixture = [
array('test' => 'more:than:one:with:', 'assertValue' => 'more:than:one:with:'), ['test' => 'more:than:one:with:', 'assertValue' => 'more:than:one:with:'],
array('test' => 'more:than:one:without', 'assertValue' => 'more:than:one:without:'), ['test' => 'more:than:one:without', 'assertValue' => 'more:than:one:without:'],
array('test' => 'resque', 'assertValue' => 'resque:'), ['test' => 'resque', 'assertValue' => 'resque:'],
array('test' => 'resque:', 'assertValue' => 'resque:'), ['test' => 'resque:', 'assertValue' => 'resque:'],
); ];
foreach ($fixture as $item) { foreach ($fixture as $item) {
Resque_Redis::prefix($item['test']); Resque_Redis::prefix($item['test']);
@ -187,10 +187,10 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
{ {
Resque_Redis::prefix('php'); Resque_Redis::prefix('php');
$queue = 'jobs'; $queue = 'jobs';
$payload = array('another_value'); $payload = ['another_value'];
Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload); Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload);
$this->assertEquals(Resque::queues(), array('jobs')); $this->assertEquals(Resque::queues(), ['jobs']);
$this->assertEquals(Resque::size($queue), 1); $this->assertEquals(Resque::size($queue), 1);
Resque_Redis::prefix('resque'); Resque_Redis::prefix('resque');
@ -228,7 +228,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
Resque::enqueue($queue, 'Test_Job_Dequeue1'); Resque::enqueue($queue, 'Test_Job_Dequeue1');
Resque::enqueue($queue, 'Test_Job_Dequeue2'); Resque::enqueue($queue, 'Test_Job_Dequeue2');
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
$test = array('Test_Job_Dequeue2'); $test = ['Test_Job_Dequeue2'];
$this->assertEquals(Resque::dequeue($queue, $test), 1); $this->assertEquals(Resque::dequeue($queue, $test), 1);
$this->assertEquals(Resque::size($queue), 1); $this->assertEquals(Resque::size($queue), 1);
} }
@ -240,7 +240,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
Resque::enqueue($queue, 'Test_Job_Dequeue2'); Resque::enqueue($queue, 'Test_Job_Dequeue2');
Resque::enqueue($queue, 'Test_Job_Dequeue3'); Resque::enqueue($queue, 'Test_Job_Dequeue3');
$this->assertEquals(Resque::size($queue), 3); $this->assertEquals(Resque::size($queue), 3);
$test = array('Test_Job_Dequeue2', 'Test_Job_Dequeue3'); $test = ['Test_Job_Dequeue2', 'Test_Job_Dequeue3'];
$this->assertEquals(Resque::dequeue($queue, $test), 2); $this->assertEquals(Resque::dequeue($queue, $test), 2);
$this->assertEquals(Resque::size($queue), 1); $this->assertEquals(Resque::size($queue), 1);
} }
@ -252,7 +252,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
Resque::enqueue($queue, 'Test_Job_Dequeue2'); Resque::enqueue($queue, 'Test_Job_Dequeue2');
Resque::enqueue($queue, 'Test_Job_Dequeue3'); Resque::enqueue($queue, 'Test_Job_Dequeue3');
$this->assertEquals(Resque::size($queue), 3); $this->assertEquals(Resque::size($queue), 3);
$test = array('Test_Job_Dequeue4'); $test = ['Test_Job_Dequeue4'];
$this->assertEquals(Resque::dequeue($queue, $test), 0); $this->assertEquals(Resque::dequeue($queue, $test), 0);
$this->assertEquals(Resque::size($queue), 3); $this->assertEquals(Resque::size($queue), 3);
} }
@ -264,7 +264,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
Resque::enqueue($queue, 'Test_Job_Dequeue2'); Resque::enqueue($queue, 'Test_Job_Dequeue2');
Resque::enqueue($queue, 'Test_Job_Dequeue3'); Resque::enqueue($queue, 'Test_Job_Dequeue3');
$this->assertEquals(Resque::size($queue), 3); $this->assertEquals(Resque::size($queue), 3);
$test = array('Test_Job_Dequeue4', 'Test_Job_Dequeue1'); $test = ['Test_Job_Dequeue4', 'Test_Job_Dequeue1'];
$this->assertEquals(Resque::dequeue($queue, $test), 1); $this->assertEquals(Resque::dequeue($queue, $test), 1);
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
} }
@ -275,7 +275,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
Resque::enqueue($queue, 'Test_Job_Dequeue'); Resque::enqueue($queue, 'Test_Job_Dequeue');
$qid = Resque::enqueue($queue, 'Test_Job_Dequeue'); $qid = Resque::enqueue($queue, 'Test_Job_Dequeue');
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
$test = array('Test_Job_Dequeue' => $qid); $test = ['Test_Job_Dequeue' => $qid];
$this->assertEquals(Resque::dequeue($queue, $test), 1); $this->assertEquals(Resque::dequeue($queue, $test), 1);
$this->assertEquals(Resque::size($queue), 1); $this->assertEquals(Resque::size($queue), 1);
} }
@ -287,7 +287,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
$qid = Resque::enqueue($queue, 'Test_Job_Dequeue'); $qid = Resque::enqueue($queue, 'Test_Job_Dequeue');
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
#qid right but class name is wrong #qid right but class name is wrong
$test = array('Test_Job_Dequeue1' => $qid); $test = ['Test_Job_Dequeue1' => $qid];
$this->assertEquals(Resque::dequeue($queue, $test), 0); $this->assertEquals(Resque::dequeue($queue, $test), 0);
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
} }
@ -296,9 +296,9 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
{ {
$queue = 'jobs'; $queue = 'jobs';
Resque::enqueue($queue, 'Test_Job_Dequeue'); Resque::enqueue($queue, 'Test_Job_Dequeue');
$qid = Resque::enqueue($queue, 'Test_Job_Dequeue'); Resque::enqueue($queue, 'Test_Job_Dequeue');
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
$test = array('Test_Job_Dequeue' => 'r4nD0mH4sh3dId'); $test = ['Test_Job_Dequeue' => 'r4nD0mH4sh3dId'];
$this->assertEquals(Resque::dequeue($queue, $test), 0); $this->assertEquals(Resque::dequeue($queue, $test), 0);
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
} }
@ -328,7 +328,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
// WHEN // WHEN
$test = ['Test_Job_Dequeue9' => $removeArgs]; $test = ['Test_Job_Dequeue9' => $removeArgs];
$removedItems = Resque::dequeue($queue, $test, "Dequeue one failed!"); $removedItems = Resque::dequeue($queue, $test);
// THEN // THEN
$this->assertEquals($removedItems, 2); $this->assertEquals($removedItems, 2);
@ -341,12 +341,12 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testDequeueItemWithUnorderedArg() public function testDequeueItemWithUnorderedArg()
{ {
$queue = 'jobs'; $queue = 'jobs';
$arg = array('foo' => 1, 'bar' => 2); $arg = ['foo' => 1, 'bar' => 2];
$arg2 = array('bar' => 2, 'foo' => 1); $arg2 = ['bar' => 2, 'foo' => 1];
Resque::enqueue($queue, 'Test_Job_Dequeue'); Resque::enqueue($queue, 'Test_Job_Dequeue');
Resque::enqueue($queue, 'Test_Job_Dequeue', $arg); Resque::enqueue($queue, 'Test_Job_Dequeue', $arg);
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
$test = array('Test_Job_Dequeue' => $arg2); $test = ['Test_Job_Dequeue' => $arg2];
$this->assertEquals(Resque::dequeue($queue, $test), 1); $this->assertEquals(Resque::dequeue($queue, $test), 1);
$this->assertEquals(Resque::size($queue), 1); $this->assertEquals(Resque::size($queue), 1);
} }
@ -354,22 +354,22 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testDequeueItemWithiWrongArg() public function testDequeueItemWithiWrongArg()
{ {
$queue = 'jobs'; $queue = 'jobs';
$arg = array('foo' => 1, 'bar' => 2); $arg = ['foo' => 1, 'bar' => 2];
$arg2 = array('foo' => 2, 'bar' => 3); $arg2 = ['foo' => 2, 'bar' => 3];
Resque::enqueue($queue, 'Test_Job_Dequeue'); Resque::enqueue($queue, 'Test_Job_Dequeue');
Resque::enqueue($queue, 'Test_Job_Dequeue', $arg); Resque::enqueue($queue, 'Test_Job_Dequeue', $arg);
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
$test = array('Test_Job_Dequeue' => $arg2); $test = ['Test_Job_Dequeue' => $arg2];
$this->assertEquals(Resque::dequeue($queue, $test), 0); $this->assertEquals(Resque::dequeue($queue, $test), 0);
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
} }
public function testUseDefaultFactoryToGetJobInstance() public function testUseDefaultFactoryToGetJobInstance()
{ {
$payload = array( $payload = [
'class' => 'Some_Job_Class', 'class' => 'Some_Job_Class',
'args' => null 'args' => null
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$instance = $job->getInstance(); $instance = $job->getInstance();
$this->assertInstanceOf('Some_Job_Class', $instance); $this->assertInstanceOf('Some_Job_Class', $instance);
@ -377,10 +377,10 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
public function testUseFactoryToGetJobInstance() public function testUseFactoryToGetJobInstance()
{ {
$payload = array( $payload = [
'class' => 'Some_Job_Class', 'class' => 'Some_Job_Class',
'args' => array(array()) 'args' => [[]]
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$factory = new Some_Stub_Factory(); $factory = new Some_Stub_Factory();
$job->setJobFactory($factory); $job->setJobFactory($factory);

View File

@ -13,7 +13,7 @@ class Resque_Tests_LogTest extends Resque_Tests_TestCase
public function testLogInterpolate() public function testLogInterpolate()
{ {
$logger = new Resque_Log(); $logger = new Resque_Log();
$actual = $logger->interpolate('string {replace}', array('replace' => 'value')); $actual = $logger->interpolate('string {replace}', ['replace' => 'value']);
$expected = 'string value'; $expected = 'string value';
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
@ -24,7 +24,7 @@ class Resque_Tests_LogTest extends Resque_Tests_TestCase
$logger = new Resque_Log(); $logger = new Resque_Log();
$actual = $logger->interpolate( $actual = $logger->interpolate(
'string {replace1} {replace2}', 'string {replace1} {replace2}',
array('replace1' => 'value1', 'replace2' => 'value2') ['replace1' => 'value1', 'replace2' => 'value2']
); );
$expected = 'string value1 value2'; $expected = 'string value1 value2';

View File

@ -10,9 +10,6 @@
class Resque_Tests_RedisTest extends Resque_Tests_TestCase class Resque_Tests_RedisTest extends Resque_Tests_TestCase
{ {
/**
* @expectedException Test basic redis functionality.
*/
public function testRedisGetSet() public function testRedisGetSet()
{ {
$this->redis->set("testKey", 24); $this->redis->set("testKey", 24);
@ -173,6 +170,8 @@ class Resque_Tests_RedisTest extends Resque_Tests_TestCase
/** /**
* @dataProvider validDsnStringProvider * @dataProvider validDsnStringProvider
* @param $dsn
* @param $expected
*/ */
public function testParsingValidDsnString($dsn, $expected) public function testParsingValidDsnString($dsn, $expected)
{ {
@ -183,6 +182,7 @@ class Resque_Tests_RedisTest extends Resque_Tests_TestCase
/** /**
* @dataProvider bogusDsnStringProvider * @dataProvider bogusDsnStringProvider
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
* @param $dsn
*/ */
public function testParsingBogusDsnStringThrowsException($dsn) public function testParsingBogusDsnStringThrowsException($dsn)
{ {

View File

@ -57,8 +57,8 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
$worker->unregisterWorker(); $worker->unregisterWorker();
$this->assertFalse(Resque_Worker::exists((string)$worker)); $this->assertFalse(Resque_Worker::exists((string)$worker));
$this->assertEquals(array(), Resque_Worker::all()); $this->assertEquals([], Resque_Worker::all());
$this->assertEquals(array(), $this->redis->smembers('resque:workers')); $this->assertEquals([], $this->redis->smembers('resque:workers'));
} }
public function testPausedWorkerDoesNotPickUpJobs() public function testPausedWorkerDoesNotPickUpJobs()
@ -87,10 +87,10 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
public function testWorkerCanWorkOverMultipleQueues() public function testWorkerCanWorkOverMultipleQueues()
{ {
$worker = new Resque_Worker(array( $worker = new Resque_Worker([
'queue1', 'queue1',
'queue2' 'queue2'
)); ]);
$worker->setLogger(new Resque_Log()); $worker->setLogger(new Resque_Log());
$worker->registerWorker(); $worker->registerWorker();
Resque::enqueue('queue1', 'Test_Job_1'); Resque::enqueue('queue1', 'Test_Job_1');
@ -105,11 +105,11 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
public function testWorkerWorksQueuesInSpecifiedOrder() public function testWorkerWorksQueuesInSpecifiedOrder()
{ {
$worker = new Resque_Worker(array( $worker = new Resque_Worker([
'high', 'high',
'medium', 'medium',
'low' 'low'
)); ]);
$worker->setLogger(new Resque_Log()); $worker->setLogger(new Resque_Log());
$worker->registerWorker(); $worker->registerWorker();
@ -163,7 +163,7 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
$job = $worker->reserve(); $job = $worker->reserve();
$worker->workingOn($job); $worker->workingOn($job);
$worker->doneWorking(); $worker->doneWorking();
$this->assertEquals(array(), $worker->job()); $this->assertEquals([], $worker->job());
} }
public function testWorkerRecordsWhatItIsWorkingOn() public function testWorkerRecordsWhatItIsWorkingOn()
@ -172,9 +172,9 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
$worker->setLogger(new Resque_Log()); $worker->setLogger(new Resque_Log());
$worker->registerWorker(); $worker->registerWorker();
$payload = array( $payload = [
'class' => 'Test_Job' 'class' => 'Test_Job'
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$worker->workingOn($job); $worker->workingOn($job);
@ -214,7 +214,7 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
$worker->setId($workerId[0] . ':1:jobs'); $worker->setId($workerId[0] . ':1:jobs');
$worker->registerWorker(); $worker->registerWorker();
$worker = new Resque_Worker(array('high', 'low')); $worker = new Resque_Worker(['high', 'low']);
$worker->setLogger(new Resque_Log()); $worker->setLogger(new Resque_Log());
$worker->setId($workerId[0] . ':2:high,low'); $worker->setId($workerId[0] . ':2:high,low');
$worker->registerWorker(); $worker->registerWorker();
@ -258,9 +258,9 @@ class Resque_Tests_WorkerTest extends Resque_Tests_TestCase
$worker->setLogger(new Resque_Log()); $worker->setLogger(new Resque_Log());
$worker->registerWorker(); $worker->registerWorker();
$payload = array( $payload = [
'class' => 'Test_Job' 'class' => 'Test_Job'
); ];
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$worker->workingOn($job); $worker->workingOn($job);