Skip to content

Commit

Permalink
Add package
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop committed Feb 18, 2015
1 parent 153718d commit 3f7d080
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 7 deletions.
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/vendor
composer.phar
vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
.DS_Store
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script: phpunit
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# laravel-queue-clear
Command for wiping your queues clear
# Laravel Queue Clear Command

[![Build Status](https://travis-ci.org/morrislaptop/laravel-queue-clear.png?branch=master)](https://travis-ci.org/morrislaptop/laravel-queue-clear)

Often, when you're issuing `php artisan db:refresh --seed`, you will have queue jobs left over that won't match with
your database records anymore.

This package simplifies the process of clearing your queues drastically.

## Installation

Begin by installing this package through Composer.

```js
{
"require": {
"morrislaptop/laravel-queue-clear": "~1.0"
}
}
```

And then include the service provider within config/app.php.

```php
'providers' => [
'Morrislaptop\LaravelQueueClear\LaravelQueueClearServiceProvider'
];
```

## Usage

```bash
php artisan queue:clear [connection] [queue]
```

Where:

* `[connection]` is the name of a connection in your config/queue.php
* `[queue]` is the name of the queue / pipe you want to clear

If you omit either argument, it will use your default driver and the default queue / pipe for that driver.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "morrislaptop/laravel-queue-clear",
"description": "Command for wiping your queues clear",
"authors": [
{
"name": "Craig Morris",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
"Morrislaptop\\LaravelQueueClear\\": "src/"
}
},
"minimum-stability": "stable"
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
38 changes: 38 additions & 0 deletions src/Clearer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php namespace Morrislaptop\LaravelQueueClear;

use Illuminate\Queue\QueueManager;
use Illuminate\Contracts\Queue\Factory as FactoryContract;
use Morrislaptop\LaravelQueueClear\Contracts\Clearer as ClearerContract;

class Clearer implements ClearerContract
{
/**
* @var QueueManager
*/
protected $manager;

/**
* {@inheritDoc}
*/
function __construct(FactoryContract $manager)
{
$this->manager = $manager;
}

/**
* {@inheritDoc}
*/
public function clear($connection, $queue)
{
$count = 0;
$connection = $this->manager->connection($connection);

while ($job = $connection->pop($queue)) {
$job->delete();
$count++;
}

return $count;
}

}
80 changes: 80 additions & 0 deletions src/Console/QueueClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php namespace Morrislaptop\LaravelQueueClear\Console;

use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Queue\QueueManager;
use Morrislaptop\LaravelQueueClear\Contracts\Clearer as ClearerContract;
use Symfony\Component\Console\Input\InputArgument;

class QueueClearCommand extends Command {


/**
* The console command name.
*
* @var string
*/
protected $name = 'queue:clear';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear all queued jobs, by deleting all pending jobs.';

/**
* @var
*/
protected $config;

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

/**
* @var Clearer
*/
protected $clearer;

/**
* @param Repository $config
* @param Clearer $clearer
*/
function __construct(Repository $config, ClearerContract $clearer)
{
$this->config = $config;
$this->clearer = $clearer;
parent::__construct();
}

/**
* Defines the arguments.
*
* @return array
*/
public function getArguments()
{
return array(
array('connection', InputArgument::OPTIONAL, 'The connection of the queue driver to clear.'),
array('queue', InputArgument::OPTIONAL, 'The name of the queue / pipe to clear.'),
);
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$connection = $this->argument('connection') ?: $this->config->get('queue.default');
$queue = $this->argument('queue') ?: $this->config->get('queue.connections.' . $connection . '.queue');

$this->info(sprintf('Clearing queue "%s" on "%s"', $queue, $connection));
$cleared = $this->clearer->clear($connection, $queue);
$this->info(sprintf('Cleared %d jobs', $cleared));
}

}
21 changes: 21 additions & 0 deletions src/Contracts/Clearer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php namespace Morrislaptop\LaravelQueueClear\Contracts;

use Illuminate\Contracts\Queue\Factory as FactoryContract;

interface Clearer
{
/**
* @param FactoryContract $manager
*/
function __construct(FactoryContract $manager);

/**
* Deletes all the jobs in the queue for connection. Returns
* how many jobs were deleted
*
* @param $connection
* @param $queue
* @return int
*/
public function clear($connection, $queue);
}
38 changes: 38 additions & 0 deletions src/LaravelQueueClearServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php namespace Morrislaptop\LaravelQueueClear;

use Illuminate\Support\ServiceProvider;

class LaravelQueueClearServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind(
'Morrislaptop\LaravelQueueClear\Contracts\Clearer',
'Morrislaptop\LaravelQueueClear\Clearer'
);
$this->commands('Morrislaptop\LaravelQueueClear\Console\QueueClearCommand');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [];
}

}
Empty file added tests/.gitkeep
Empty file.

0 comments on commit 3f7d080

Please sign in to comment.