Skip to content

Commit

Permalink
Improved namespacing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Damjan Znidarsic authored and Damjan Znidarsic committed Jul 18, 2015
1 parent 4fc10dc commit 5d529f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions examples/advanced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
include __DIR__.'/../vendor/autoload.php';

// connect to database 0 on 127.0.0.1
$redis = new Predis\Client([
'host' => '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));
19 changes: 14 additions & 5 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
}

Expand All @@ -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'));
}
}

0 comments on commit 5d529f3

Please sign in to comment.