Skip to content
This repository has been archived by the owner on Nov 15, 2019. It is now read-only.

Commit

Permalink
Add option to disable queueing
Browse files Browse the repository at this point in the history
  • Loading branch information
bramdevries committed Sep 15, 2015
1 parent db2bbd4 commit 894b1cc
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 172 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## [1.0.2] - 15-09-2015

### Added

- Added `disabled()` method to disable queueing completely

## [1.0.1] - 27-08-2015

### Fixed
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Via Composer
$ composer require arrounded/queues
```

A Laravel 4.2 version also availabe: `composer require arrounded/queues:dev-laravel/4.2`

## Usage

First add the module's service provider and facade to config/app.php:
Expand Down Expand Up @@ -62,7 +64,7 @@ __Delaying execution__
Queues::on('foo')->uses(Foobar::class)->delay(10)->push();
```

This will delay the execution of the job by 10 seconds.
This will delay the execution of the job by 10 seconds.

### Prefixing queue names

Expand All @@ -73,14 +75,26 @@ ServiceProvider:
$this->app['queues']->setPrefix('foobar') // foobar_foo_normal
```

__Disabling queueing__

In some cases you might want to disable queueing all together (for example during integration/functional tests)

```php
// To disable
$this->app['queues']->disabled()

// To re-enable
$this->app['queues']->disabled(false)
```

### Dependency Injection

You can also use dependency injection:

```php
use Arrounded\Queues\Queues;

class FooService
class FooService
{
public function __construct(Queues $queues)
{
Expand Down
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
"autoload-dev": {
"psr-4": {
"Arrounded\\Queues\\": "tests"
},
"files": [
"tests/helpers.php"
]
}
},
"scripts": {
"test": "phpunit",
Expand Down
323 changes: 172 additions & 151 deletions src/Queues.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,154 +7,175 @@

class Queues
{
const PRIORITY_HIGH = 'high';
const PRIORITY_NORMAL = 'normal';
const PRIORITY_LOW = 'low';

/**
* @var string
*/
protected $prefix;

/**
* @var string
*/
protected $priority = self::PRIORITY_NORMAL;

/**
* @var array
*/
protected $params = [];

/**
* @var Queue
*/
protected $queue;

/**
* @param Queue $queue
*/
public function __construct(Queue $queue)
{
$this->queue = $queue;
}

/**
* @param string $prefix
*
* @return Queues
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;

return $this;
}

/**
* @param string $queue
*
* @return $this
*/
public function on($queue)
{
$this->params['queue'] = $queue;

return $this;
}

/**
* @param string $class
*
* @return $this
*/
public function uses($class)
{
$this->params['class'] = $class;

return $this;
}

/**
* @param array $payload
*
* @return $this
*/
public function with(array $payload = [])
{
$this->params['payload'] = $payload;

return $this;
}

/**
* @param string $priority
*
* @return $this
*/
public function priority($priority = self::PRIORITY_NORMAL)
{
$this->priority = $priority;

return $this;
}

/**
* @param $seconds
*
* @return $this
*/
public function delay($seconds)
{
$this->params['delay'] = $seconds;

return $this;
}

/**
* @return JobDescription
*/
public function push()
{
$job = new JobDescription(
$this->getQueueName(array_get($this->params, 'queue')),
array_get($this->params, 'class'),
array_get($this->params, 'payload', []),
array_get($this->params, 'delay', 0)
);

$this->queue($job);

return $job;
}

/**
* @param $queue
*
* @return string
*/
protected function getQueueName($queue)
{
$parts = [$queue, $this->priority];

if ($this->prefix) {
array_unshift($parts, $this->prefix);
}

return implode('_', $parts);
}

/**
* @param JobDescription $job
*
* @return void
*/
protected function queue(JobDescription $job)
{
if ($job->isDelayed()) {
return $this->queue->later($job->getDelay(), $job->getClass(), $job->getPayload(), $job->getQueue());
}

return $this->queue->push($job->getClass(), $job->getPayload(), $job->getQueue());
}
}
const PRIORITY_HIGH = 'high';
const PRIORITY_NORMAL = 'normal';
const PRIORITY_LOW = 'low';

/**
* @var string
*/
protected $prefix;

/**
* @var string
*/
protected $priority = self::PRIORITY_NORMAL;

/**
* @var array
*/
protected $params = [];

/**
* @var Queue
*/
protected $queue;

/**
* @var
*/
protected $disabled = false;

/**
* @param Queue $queue
*/
public function __construct(Queue $queue)
{
$this->queue = $queue;
}

/**
* @param bool|true $value
*
* @return $this
*/
public function disabled($value = true)
{
$this->disabled = $value;

return $this;
}

/**
* @param string $prefix
*
* @return Queues
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;

return $this;
}

/**
* @param string $queue
*
* @return $this
*/
public function on($queue)
{
$this->params['queue'] = $queue;

return $this;
}

/**
* @param string $class
*
* @return $this
*/
public function uses($class)
{
$this->params['class'] = $class;

return $this;
}

/**
* @param array $payload
*
* @return $this
*/
public function with(array $payload = [])
{
$this->params['payload'] = $payload;

return $this;
}

/**
* @param string $priority
*
* @return $this
*/
public function priority($priority = self::PRIORITY_NORMAL)
{
$this->priority = $priority;

return $this;
}

/**
* @param $seconds
*
* @return $this
*/
public function delay($seconds)
{
$this->params['delay'] = $seconds;

return $this;
}

/**
* @return JobDescription
*/
public function push()
{
$job = new JobDescription(
$this->getQueueName(array_get($this->params, 'queue')),
array_get($this->params, 'class'),
array_get($this->params, 'payload', []),
array_get($this->params, 'delay', 0)
);

$this->queue($job);

return $job;
}

/**
* @param $queue
*
* @return string
*/
protected function getQueueName($queue)
{
$parts = [$queue, $this->priority];

if ($this->prefix) {
array_unshift($parts, $this->prefix);
}

return implode('_', $parts);
}

/**
* @param JobDescription $job
*
* @return void
*/
protected function queue(JobDescription $job)
{
if ($this->disabled) {
return;
}

if ($job->isDelayed()) {
return $this->queue->later($job->getDelay(), $job->getClass(), $job->getPayload(), $job->getQueue());
}

return $this->queue->push($job->getClass(), $job->getPayload(), $job->getQueue());
}
}
Loading

0 comments on commit 894b1cc

Please sign in to comment.