Skip to content

Commit

Permalink
Merge pull request #5 from bigcommerce/final-test-for-before-enqueue
Browse files Browse the repository at this point in the history
TOOLING-255: Finalizing resque pause
  • Loading branch information
gabelimon committed Mar 11, 2015
2 parents 09738e0 + 66ef02c commit ae49fc4
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 564 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ php:
- 5.6
env:
- REDIS_STANDALONE=0
- REDIS_STANDALONE=1
before_script:
- sh -c "if [ $REDIS_STANDALONE -eq 0 ]; then wget https://github.com/nicolasff/phpredis/archive/2.2.3.zip -O php-redis.zip && unzip php-redis.zip; fi"
- sh -c "if [ $REDIS_STANDALONE -eq 0 ]; then cd phpredis-2.2.3/ && phpize && ./configure && make && make install; fi"
- sh -c "if [ $REDIS_STANDALONE -eq 0 ]; then echo \"extension=redis.so\" >> `php --ini | grep \"Loaded Configuration\" | sed -e \"s|.*:\s*||\"`; fi"
- composer install
script:
- composer lint
- composer test
5 changes: 3 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(c) 2014 Wedy Chainy <[email protected]>
Copyright (C) Bigcommerce, 2014
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand All @@ -17,4 +18,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 changes: 48 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
php-resque-pause: PHP Resque Pause
php-resque-pause: PHP Resque Pause [![Build Status](https://secure.travis-ci.org/bigcommerce/php-resque-pause.png?branch=master)](https://travis-ci.org/bigcommerce/php-resque-pause)
==================================

A [PHP-Resque](http://github.com/chrisboulton/php-resque) plugin.
Expand All @@ -7,8 +7,51 @@ resque-pause adds functionality to pause resque jobs

Using a pause allows you to stop the worker without stop the enqueue

TBA
TBA
TBA
For further information re: php-resque, visit this official repo: <http://github.com/chrisboulton/php-resque>

WIP WIP WIP
## Requirements ##
* PHP 5.3+
* Redis 2.2+
* Composer

## Getting Started ##
resque-pause is installed via composer. To install:

```bash
$ # Add php-resque-pause to your project's composer.json
$ composer require "bigcommerce/php-resque-pause"
$ # Install composer dependencies
$ composer install
```

## Usage ##

To use Resque Pause in your application you'll need to create a globally used instance, we use Pimple but you can use
globals, a static variable, or whatever else you like. Upon instantiation `Pause` will add a Resque listener to make
sure that any jobs pushed to a paused queue will be paused as well. On destruction `Pause` will remove said listener.

```php
// Let's put it in a global since that's easy/familiar
$GLOBALS['ResquePause'] = new \Resque\Plugins\Pause(); // Your enqueues are now being listened to
```

### Pause it! ###
```php
$GLOBALS['ResquePause']->pause('My_Queue');
```

### Resume it! ###
```php
$GLOBALS['ResquePause']->resume('My_Queue');
```

### Is it Paused? ###
```php
$GLOBALS['ResquePause']->isPaused('My_Queue');
```

## Contributing ##

This repo is fairly thoroughly tested so please add tests for any feature you add. We use PSR-4 conventions and have a
linter in place. To run the linter simply run `composer lint` and to run the tests locally run `composer test`. To have
your code reviewed please tag @gabelimon and @wedy.
118 changes: 118 additions & 0 deletions Tests/Integration/PauseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
namespace Resque\Integration\Plugins\Tests;

use Resque;
use Resque\Plugins\Pause;
use PHPUnit_Framework_TestCase;

/**
* Pause tests.
*
* @package PHP Resque Pause
* @author Wedy Chainy <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class PauseTest extends PHPUnit_Framework_TestCase
{
/** @var Pause */
protected $pauser = null;

public static function setUpBeforeClass()
{
$testMisc = realpath(__DIR__ . '/misc/');
$redisConf = "$testMisc/redis.conf";

// Attempt to start our own redis instance for tesitng.
exec('which redis-server', $output, $returnVar);
if ($returnVar != 0) {
echo "Cannot find redis-server in path. Please make sure redis is installed.\n";
exit(1);
}

exec("cd $testMisc; redis-server $redisConf", $output, $returnVar);
usleep(500000);
if ($returnVar != 0) {
echo "Cannot start redis-server.\n";
exit(1);
}

// Get redis port from conf
$config = file_get_contents($redisConf);
if (!preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches)) {
echo "Could not determine redis port from redis.conf";
exit(1);
}

Resque::setBackend('localhost:' . $matches[1]);
}

public function setUp()
{
Resque::redis()->flushAll();
$this->pauser = new Pause();
}

public function testPause()
{
// Pause non-paused queue
Resque::enqueue('upgrade:test', 'test');
Resque::enqueue('upgrade:test', 'test');
$this->assertEquals(2, Resque::size('upgrade:test'));
$this->assertTrue($this->pauser->pause('upgrade:test'));
$this->assertEquals(0, Resque::size('upgrade:test'));

// Pause paused queue
$this->assertFalse($this->pauser->pause('upgrade:test'));

// Pause non-existent queue
$this->assertFalse($this->pauser->pause('upgrade:test2'));
}

public function testResume()
{
// Resume paused queue
Resque::enqueue('upgrade:test3', 'test');
Resque::enqueue('upgrade:test3', 'test');
$this->pauser->pause('upgrade:test3');
$this->assertTrue($this->pauser->resume('upgrade:test3'));
$this->assertEquals(2, Resque::size('upgrade:test3'));

// Resume non-paused queue
Resque::enqueue('upgrade:test4', 'test');
$this->assertFalse($this->pauser->resume('upgrade:test4'));

// Resume non-existent queue
$this->assertFalse($this->pauser->resume('upgrade:idontexist'));
}

public function testIsPaused()
{
// Paused queue
Resque::enqueue('upgrade:test5', 'test');
$this->pauser->pause('upgrade:test5');
$this->assertTrue($this->pauser->isPaused('upgrade:test5'));

// Non-paused queue
Resque::enqueue('upgrade:test6', 'test');
$this->assertFalse($this->pauser->isPaused('upgrade:test6'));

// Paused and resumed queue
Resque::enqueue('upgrade:test7', 'test');
$this->pauser->pause('upgrade:test7');
$this->assertTrue($this->pauser->isPaused('upgrade:test7'));
$this->pauser->resume('upgrade:test7');
$this->assertFalse($this->pauser->isPaused('upgrade:test7'));

// Non-existent queue
$this->assertFalse($this->pauser->isPaused('upgrade:istilldontexist'));
}

public function testPauseCallback()
{
$this->pauser->pause('upgrade:test8');
Resque::enqueue('upgrade:test8', 'test');

$this->assertEquals(0, Resque::redis()->llen('queue:upgrade:test8'));
$this->assertEquals(1, Resque::redis()->llen('temp:upgrade:test8'));
}
}
File renamed without changes.
147 changes: 147 additions & 0 deletions Tests/Unit/JobPauserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
namespace Unit\Resque\Plugins\Tests;

use Resque\Plugins\JobPauser;
use PHPUnit_Framework_TestCase;

/**
* Job tests.
*
* @package Resque/Tests
* @author Wedy Chainy <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class JobPauserTest extends PHPUnit_Framework_TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $redisMock = null;

public function setUp()
{
$this->redisMock = $this->getMockBuilder('Resque_Redis')
->disableOriginalConstructor()
->setMethods(array(
'sadd',
'srem',
'getPrefix',
'rename',
'sismember',
))
->getMock();
}

public function createDataProvider()
{
return array(
array(true, true),
array(false, false),
);
}

/**
* @dataProvider createDataProvider
* @param bool $saddSuccess
*/
public function testCreate($saddSuccess, $returnSuccess)
{
$this->redisMock
->expects($this->once())
->method('sadd')
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test'))
->willReturn($saddSuccess);

$pauser = new JobPauser($this->redisMock, 'resqueFaker:');
$this->assertEquals($returnSuccess, $pauser->pause('upgrade:test'));
}

public function removeDataProvider()
{
return array(
array(true, true),
array(false, false),
);
}

/**
* @dataProvider removeDataProvider
* @param bool $sremSuccess
*/
public function testRemove($sremSuccess, $returnSuccess)
{
$this->redisMock
->expects($this->once())
->method('srem')
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test2'))
->willReturn($sremSuccess);

$pauser = new JobPauser($this->redisMock, 'resqueFaker:');
$this->assertEquals($returnSuccess, $pauser->resume('upgrade:test2'));
}

public function renameDataprovider()
{
return array(
array(true, true),
array(false, false),
);
}

/**
* @dataProvider renameDataProvider
* @param bool $renameSuccess
* @param bool $expectedResult
*/
public function testRenameToTemp($renameSuccess, $expectedResult)
{
$this->redisMock
->expects($this->once())
->method('rename')
->with($this->equalTo('queue:upgrade:test3'), $this->equalTo('resqueFaker:temp:upgrade:test3'))
->willReturn($renameSuccess);

$pauser = new JobPauser($this->redisMock, 'resqueFaker:');
$this->assertEquals($expectedResult, $pauser->renameToTemp('upgrade:test3'));
}

/**
* @dataProvider renameDataProvider Reuses the renaming provider because the cases are the same
* @param bool $renameSuccess
* @param bool $expectedResult
*/
public function testRenameBackFromTemp($renameSuccess, $expectedResult)
{
$this->redisMock
->expects($this->once())
->method('rename')
->with($this->equalTo('temp:upgrade:test3'), $this->equalTo('resqueFaker:queue:upgrade:test3'))
->willReturn($renameSuccess);

$pauser = new JobPauser($this->redisMock, 'resqueFaker:');
$this->assertEquals($expectedResult, $pauser->renameBackFromTemp('upgrade:test3'));
}

public function existsDataProvider()
{
return array(
array(true, true),
array(false, false),
);
}

/**
* @dataProvider existsDataProvider
* @param bool $existsSuccess
* @param bool $expectedResult
*/
public function testExists($existsSuccess, $expectedResult)
{
$this->redisMock
->expects($this->once())
->method('sismember')
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test8'))
->willReturn($existsSuccess);

$pauser = new JobPauser($this->redisMock, 'resqueFaker:');
$this->assertEquals($expectedResult, $pauser->isPaused('upgrade:test8'));
}
}
Loading

0 comments on commit ae49fc4

Please sign in to comment.