├── tests └── .gitkeep ├── .gitignore ├── .travis.yml ├── src ├── Contracts │ └── Clearer.php ├── LaravelQueueClearServiceProvider.php ├── Clearer.php └── Console │ └── QueueClearCommand.php ├── phpunit.xml ├── composer.json ├── LICENSE └── README.md /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - 7.0 8 | - 7.1 9 | 10 | before_script: 11 | - travis_retry composer self-update 12 | - travis_retry composer install --prefer-source --no-interaction --dev 13 | 14 | script: phpunit 15 | -------------------------------------------------------------------------------- /src/Contracts/Clearer.php: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "morrislaptop/laravel-queue-clear", 3 | "description": "Command for wiping your queues clear", 4 | "authors": [ 5 | { 6 | "name": "Craig Morris", 7 | "email": "craig.michael.morris@gmail.com" 8 | } 9 | ], 10 | "require": { 11 | "php": ">=5.4.0", 12 | "illuminate/support": ">=5.0.0" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Morrislaptop\\LaravelQueueClear\\": "src/" 17 | } 18 | }, 19 | "extra": { 20 | "laravel": { 21 | "providers": [ 22 | "Morrislaptop\\LaravelQueueClear\\LaravelQueueClearServiceProvider" 23 | ] 24 | } 25 | }, 26 | "minimum-stability": "stable" 27 | } 28 | -------------------------------------------------------------------------------- /src/LaravelQueueClearServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind( 22 | 'Morrislaptop\LaravelQueueClear\Contracts\Clearer', 23 | 'Morrislaptop\LaravelQueueClear\Clearer' 24 | ); 25 | $this->commands('Morrislaptop\LaravelQueueClear\Console\QueueClearCommand'); 26 | } 27 | 28 | /** 29 | * Get the services provided by the provider. 30 | * 31 | * @return array 32 | */ 33 | public function provides() 34 | { 35 | return []; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Craig Morris 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Queue Clear Command 2 | 3 | [![Build Status](https://travis-ci.org/morrislaptop/laravel-queue-clear.png?branch=master)](https://travis-ci.org/morrislaptop/laravel-queue-clear) 4 | 5 | Often, when you're issuing `php artisan db:refresh --seed`, you will have queue jobs left over that won't match with 6 | your database records anymore. 7 | 8 | This package simplifies the process of clearing your queues drastically. 9 | 10 | ## Installation 11 | 12 | Begin by installing this package through Composer. 13 | 14 | ```js 15 | { 16 | "require": { 17 | "morrislaptop/laravel-queue-clear": "~1.0" 18 | } 19 | } 20 | ``` 21 | 22 | Laravel 5.5+ will use the auto-discovery function. 23 | 24 | If using Laravel 5.4 (or if you don't use auto-discovery) you will need to include the service provider in `config/app.php`. 25 | 26 | ```php 27 | 'providers' => [ 28 | Morrislaptop\LaravelQueueClear\LaravelQueueClearServiceProvider::class, 29 | ]; 30 | ``` 31 | 32 | ## Usage 33 | 34 | ```bash 35 | php artisan queue:clear [connection] [queue] 36 | ``` 37 | 38 | Where: 39 | 40 | * `[connection]` is the name of a connection in your `config/queue.php` 41 | * `[queue]` is the name of the queue / pipe you want to clear 42 | 43 | If you omit either argument, it will use your default driver and the default queue / pipe for that driver. 44 | -------------------------------------------------------------------------------- /src/Clearer.php: -------------------------------------------------------------------------------- 1 | manager = $manager; 20 | } 21 | 22 | /** 23 | * {@inheritDoc} 24 | */ 25 | public function clear($connection, $queue) 26 | { 27 | $count = 0; 28 | $connection = $this->manager->connection($connection); 29 | 30 | $count += $this->clearJobs($connection, $queue); 31 | $count += $this->clearJobs($connection, $queue . ':reserved'); 32 | $count += $this->clearDelayedJobs($connection, $queue); 33 | 34 | return $count; 35 | } 36 | 37 | protected function clearJobs($connection, $queue) 38 | { 39 | $count = 0; 40 | 41 | while ($job = $connection->pop($queue)) { 42 | $job->delete(); 43 | $count++; 44 | } 45 | 46 | return $count; 47 | } 48 | 49 | protected function clearDelayedJobs($connection, $queue) 50 | { 51 | if (method_exists($connection, 'getRedis')) { 52 | return $this->clearDelayedJobsOnRedis($connection, $queue); 53 | } 54 | 55 | throw new \InvalidArgumentException('Queue Connection not supported'); 56 | } 57 | 58 | protected function clearDelayedJobsOnRedis($connection, $queue) { 59 | $key = "queues:{$queue}:delayed"; 60 | $redis = $connection->getRedis()->connection(config('queue.connections.redis.connection')); 61 | $count = $redis->zcount($key, '-inf', '+inf'); 62 | $redis->del($key); 63 | 64 | return $count; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/Console/QueueClearCommand.php: -------------------------------------------------------------------------------- 1 | config = $config; 48 | $this->clearer = $clearer; 49 | parent::__construct(); 50 | } 51 | 52 | /** 53 | * Defines the arguments. 54 | * 55 | * @return array 56 | */ 57 | public function getArguments() 58 | { 59 | return array( 60 | array('connection', InputArgument::OPTIONAL, 'The connection of the queue driver to clear.'), 61 | array('queue', InputArgument::OPTIONAL, 'The name of the queue / pipe to clear.'), 62 | ); 63 | } 64 | 65 | /** 66 | * Execute the console command. 67 | * 68 | * @return void 69 | */ 70 | public function handle() 71 | { 72 | $connection = $this->argument('connection') ?: $this->config->get('queue.default'); 73 | $queue = $this->argument('queue') ?: $this->config->get('queue.connections.' . $connection . '.queue'); 74 | 75 | $this->info(sprintf('Clearing queue "%s" on "%s"', $queue, $connection)); 76 | $cleared = $this->clearer->clear($connection, $queue); 77 | $this->info(sprintf('Cleared %d jobs', $cleared)); 78 | } 79 | 80 | } 81 | --------------------------------------------------------------------------------