From 33dcf6f59e7b39b76205dfc881dd7674731f01c2 Mon Sep 17 00:00:00 2001 From: Mariano Iglesias Date: Thu, 16 Feb 2017 17:45:21 -0500 Subject: [PATCH] Adding job options to . Thanks @aleksraiden. Accounting for floating point in exception messages. --- CHANGELOG.md | 11 +++++++++++ README.md | 6 +----- composer.json | 3 --- src/Queue/Queue.php | 8 ++++---- tests/Command/AddJobTest.php | 12 ++++++------ tests/Command/GetJobTest.php | 4 ++-- tests/Command/JScanTest.php | 2 +- tests/Command/QPeekTest.php | 2 +- tests/Command/QScanTest.php | 6 +++--- tests/Queue/QueueTest.php | 21 +++++++++++++++++++++ 10 files changed, 50 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 405e696..3a70bba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,16 @@ All Notable changes will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [2.0.3] - 2017-02-16 + +### Added +- Added `nohang` option to `getJob()`. Thanks @kaecyra +- Added ability to specify job options when scheduling a job +via `schedule()`. Thanks @aleksraiden + +### Changed +- Removed `predis/predis` from list of suggested packages to install + ## [2.0.2] - 2016-05-10 ### Fixed @@ -153,6 +163,7 @@ parameters were specified, an `InvalidCommandArgumentException` was thrown. - Added support for Predis connections, and allowing adding new connection methods via `ConnectionInterface`. +[2.0.3]: https://github.com/mariano/disque-php/releases/tag/2.0.3 [2.0.2]: https://github.com/mariano/disque-php/releases/tag/2.0.2 [2.0.1]: https://github.com/mariano/disque-php/releases/tag/2.0.1 [2.0-alpha]: https://github.com/mariano/disque-php/releases/tag/2.0-alpha diff --git a/README.md b/README.md index b8f7d04..d0213ae 100644 --- a/README.md +++ b/README.md @@ -107,11 +107,7 @@ libraries for the inspiration. [The PHP League](https://thephpleague.com) for an awesome `README.md` skeleton, and tips about packaging PHP components. -A special acknolewdgment and appreciation for our amazing contributors: - -* [Revisor](https://github.com/Revisor) for his incredible work on this library. - -* [mvrhov](https://github.com/mvrhov) for helping us support Disque RC1 node ID new format. +A special acknolewdgment and appreciation for our []amazing contributors](https://github.com/mariano/disque-php/graphs/contributors)! ## License diff --git a/composer.json b/composer.json index ec16a72..38b31cd 100644 --- a/composer.json +++ b/composer.json @@ -24,9 +24,6 @@ "require": { "php": ">=5.5" }, - "suggest": { - "predis/predis": "Use Predis to handle the connection with Disque instead of the built in client" - }, "require-dev": { "phpunit/phpunit": "~4.6", "scrutinizer/ocular": "~1.1", diff --git a/src/Queue/Queue.php b/src/Queue/Queue.php index 9179658..28ba10a 100644 --- a/src/Queue/Queue.php +++ b/src/Queue/Queue.php @@ -72,10 +72,11 @@ public function setMarshaler(MarshalerInterface $marshaler) * * @param JobInterface $job Job * @param DateTime $when Date & time on when job should be ready for processing + * @param array $options ADDJOB options sent to the client * @return JobInterface Job pushed * @throws InvalidArgumentException */ - public function schedule(JobInterface $job, DateTime $when) + public function schedule(JobInterface $job, DateTime $when, array $options = []) { if (!isset($this->timeZone)) { $this->timeZone = new DateTimeZone(self::DEFAULT_JOB_TIMEZONE); @@ -88,9 +89,8 @@ public function schedule(JobInterface $job, DateTime $when) throw new InvalidArgumentException('Specified schedule time has passed'); } - return $this->push($job, [ - 'delay' => ($date->getTimestamp() - $now->getTimestamp()) - ]); + $options['delay'] = ($date->getTimestamp() - $now->getTimestamp()); + return $this->push($job, $options); } /** diff --git a/tests/Command/AddJobTest.php b/tests/Command/AddJobTest.php index c3aed3c..8c3f88c 100644 --- a/tests/Command/AddJobTest.php +++ b/tests/Command/AddJobTest.php @@ -116,7 +116,7 @@ public function testBuildInvalidOptionTimeoutNonNumeric() public function testBuildInvalidOptionTimeoutNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\AddJob: {"timeout":3.14}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\AddJob: {"timeout":3.14\d*}$/'); $c = new AddJob(); $c->setArguments(['q', 'j', ['timeout' => 3.14]]); } @@ -130,7 +130,7 @@ public function testBuildInvalidOptionReplicateNonNumeric() public function testBuildInvalidOptionReplicateNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\AddJob: {"replicate":3.14,"timeout":0}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\AddJob: {"replicate":3.14\d*,"timeout":0}$/'); $c = new AddJob(); $c->setArguments(['q', 'j', ['replicate' => 3.14]]); } @@ -144,7 +144,7 @@ public function testBuildInvalidOptionDelayNonNumeric() public function testBuildInvalidOptionDelayNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\AddJob: {"delay":3.14,"timeout":0}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\AddJob: {"delay":3.14\d*,"timeout":0}$/'); $c = new AddJob(); $c->setArguments(['q', 'j', ['delay' => 3.14]]); } @@ -158,7 +158,7 @@ public function testBuildInvalidOptionRetryNonNumeric() public function testBuildInvalidOptionRetryNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\AddJob: {"retry":3.14,"timeout":0}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\AddJob: {"retry":3.14\d*,"timeout":0}$/'); $c = new AddJob(); $c->setArguments(['q', 'j', ['retry' => 3.14]]); } @@ -172,7 +172,7 @@ public function testBuildInvalidOptionTtlNonNumeric() public function testBuildInvalidOptionTtlNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\AddJob: {"ttl":3.14,"timeout":0}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\AddJob: {"ttl":3.14\d*,"timeout":0}$/'); $c = new AddJob(); $c->setArguments(['q', 'j', ['ttl' => 3.14]]); } @@ -186,7 +186,7 @@ public function testBuildInvalidOptionMaxlenNonNumeric() public function testBuildInvalidOptionMaxlenNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\AddJob: {"maxlen":3.14,"timeout":0}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\AddJob: {"maxlen":3.14\d*,"timeout":0}$/'); $c = new AddJob(); $c->setArguments(['q', 'j', ['maxlen' => 3.14]]); } diff --git a/tests/Command/GetJobTest.php b/tests/Command/GetJobTest.php index 4ee8bdd..1209efc 100644 --- a/tests/Command/GetJobTest.php +++ b/tests/Command/GetJobTest.php @@ -77,7 +77,7 @@ public function testBuildInvalidOptionCountNonNumeric() public function testBuildInvalidOptionCountNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\GetJob: {"count":3.14}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\GetJob: {"count":3.14\d*}$/'); $c = new GetJob(); $c->setArguments(['q1', 'q2', ['count' => 3.14]]); } @@ -91,7 +91,7 @@ public function testBuildInvalidOptionTimeoutNonNumeric() public function testBuildInvalidOptionTimeoutNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\GetJob: {"timeout":3.14}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\GetJob: {"timeout":3.14\d*}$/'); $c = new GetJob(); $c->setArguments(['q1', 'q2', ['timeout' => 3.14]]); } diff --git a/tests/Command/JScanTest.php b/tests/Command/JScanTest.php index d00bc02..7d3fa71 100644 --- a/tests/Command/JScanTest.php +++ b/tests/Command/JScanTest.php @@ -88,7 +88,7 @@ public function testBuildInvalidOptionCountNonNumeric() public function testBuildInvalidOptionCountNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\JScan: {"count":3.14}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\JScan: {"count":3.14\d*}$/'); $c = new JScan(); $c->setArguments([0, ['count' => 3.14]]); } diff --git a/tests/Command/QPeekTest.php b/tests/Command/QPeekTest.php index ab5a16c..c4b1abb 100644 --- a/tests/Command/QPeekTest.php +++ b/tests/Command/QPeekTest.php @@ -80,7 +80,7 @@ public function testBuildInvalidArgumentsNonNumeric() public function testBuildInvalidArgumentsNonInt() { - $this->setExpectedException(InvalidCommandArgumentException::class, 'Invalid command arguments. Arguments for command Disque\\Command\\QPeek: ["test",3.14]'); + $this->setExpectedExceptionRegExp(InvalidCommandArgumentException::class, '/^Invalid command arguments. Arguments for command Disque\\\\Command\\\\QPeek: \["test",3.14\d*\]$/'); $c = new QPeek(); $c->setArguments(['test', 3.14]); } diff --git a/tests/Command/QScanTest.php b/tests/Command/QScanTest.php index 9bc8e1b..19cce39 100644 --- a/tests/Command/QScanTest.php +++ b/tests/Command/QScanTest.php @@ -88,7 +88,7 @@ public function testBuildInvalidOptionMinlenNonNumeric() public function testBuildInvalidOptionMinlenNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\QScan: {"minlen":3.14}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\QScan: {"minlen":3.14\d*}$/'); $c = new QScan(); $c->setArguments([0, ['minlen' => 3.14]]); } @@ -102,7 +102,7 @@ public function testBuildInvalidOptionMaxlenNonNumeric() public function testBuildInvalidOptionMaxlenNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\QScan: {"maxlen":3.14}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\QScan: {"maxlen":3.14\d*}$/'); $c = new QScan(); $c->setArguments([0, ['maxlen' => 3.14]]); } @@ -116,7 +116,7 @@ public function testBuildInvalidOptionImportrateNonNumeric() public function testBuildInvalidOptionImportrateNonInt() { - $this->setExpectedException(InvalidOptionException::class, 'Invalid command options. Options for command Disque\\Command\\QScan: {"importrate":3.14}'); + $this->setExpectedExceptionRegExp(InvalidOptionException::class, '/^Invalid command options. Options for command Disque\\\\Command\\\\QScan: {"importrate":3.14\d*}$/'); $c = new QScan(); $c->setArguments([0, ['importrate' => 3.14]]); } diff --git a/tests/Queue/QueueTest.php b/tests/Queue/QueueTest.php index 9d17f20..36c5b54 100644 --- a/tests/Queue/QueueTest.php +++ b/tests/Queue/QueueTest.php @@ -517,6 +517,27 @@ public function testScheduleWayInTheFuture() $this->assertSame($job, $result); } + public function testScheduleWithOptions() + { + $delay = 10; + + $job = new Job(); + $queue = m::mock(Queue::class.'[push]', [m::mock(Client::class), 'queue']) + ->shouldReceive('push') + ->with($job, + anyOf( + ['retry' => 1000, 'delay' => $delay], + ['retry' => 1000, 'delay' => $delay - 1] + )) + ->andReturn($job) + ->once() + ->mock(); + + $when = new DateTime('+' . $delay . ' seconds', new DateTimeZone(Queue::DEFAULT_JOB_TIMEZONE)); + $result = $queue->schedule($job, $when, ['retry' => 1000]); + $this->assertSame($job, $result); + } + public function testProcessingConnected() { $job = m::mock(JobInterface::class)