diff --git a/README.md b/README.md index 6d8c72f..31a7ee7 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,16 @@ Sidekiq job pusher for PHP ## Usage -``` +```php $redis = new Predis\Client('tcp://127.0.0.1:6379'); $client = new \SidekiqJob\Client($redis); -$client->push('ProcessImage', $args); +$client->push('ProcessImage', ['argument1']); ``` +More examples [here](https://github.com/spinx/sidekiq-job-php/tree/master/examples). + ## Misc ### Standards diff --git a/examples/advanced.php b/examples/advanced.php new file mode 100644 index 0000000..3de5170 --- /dev/null +++ b/examples/advanced.php @@ -0,0 +1,23 @@ + 'localhost', + 'port' => '6379', + 'database' => 0, +]); + +// Instantiate a new client within 'users' namespace +$client = new \SidekiqJob\Client($redis, 'users'); + +// push a job with three arguments - args array needs to be sequential (not associative) +$args = [ + ['url' => 'http://i.imgur.com/hlAsa4k.jpg'], + true, + 70 +]; + +$id = $client->push('ProcessImage', $args); + +var_dump(sprintf('Pushed job with id %s', $id)); \ No newline at end of file diff --git a/lib/Client.php b/lib/Client.php index 44a1cfc..2292e9f 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -34,7 +34,7 @@ class Client public function __construct(\Predis\Client $redis, $namespace = null, $serializer = null, $idGenerator = null) { $this->redis = $redis; - $this->namespace = ($namespace === null) ? '' : $namespace.':'; + $this->namespace = ($namespace === null) ? '' : (string) $namespace; $this->serializer = ($serializer === null) ? new Serializer() : $serializer; $this->idGenerator = ($idGenerator === null) ? new IdGenerator() : $idGenerator; } @@ -119,7 +119,7 @@ public function pushBulk($jobs = [], $queue = self::QUEUE) */ private function atomicPush($jobId, $class, $args = [], $queue = self::QUEUE, $doAt = null) { - if(array_values($args) !== $args){ + if (array_values($args) !== $args) { throw new Exception('Associative arrays in job args are not allowed'); } @@ -130,10 +130,19 @@ private function atomicPush($jobId, $class, $args = [], $queue = self::QUEUE, $d $job = $this->serializer->serialize($jobId, $class, $args); if ($doAt === null) { - $this->redis->sadd('queues', $queue); - $this->redis->lpush(sprintf('%squeue:%s', $this->namespace, $queue), $job); + $this->redis->sadd($this->name('queues'), $queue); + $this->redis->lpush($this->name('queue', $queue), $job); } else { - $this->redis->zadd(sprintf('%sschedule', $this->namespace), [$job => $doAt]); + $this->redis->zadd($this->name('schedule'), [$job => $doAt]); } } + + /** + * @param string ...$key + * @return string + */ + private function name(...$key) + { + return implode(':', array_filter(array_merge([$this->namespace], func_get_args()), 'strlen')); + } }