├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── queue-cancel-batch.php ├── database ├── factories │ └── ModelFactory.php └── migrations │ └── create_queue_cancel_batch_table.php.stub ├── resources ├── package-image.png └── views │ └── .gitkeep └── src ├── Commands └── LaravelQueueCancelBatchCommand.php ├── Facades └── LaravelQueueCancelBatch.php ├── LaravelQueueCancelBatch.php └── LaravelQueueCancelBatchServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-queue-cancel-batch` will be documented in this file. 4 | 5 | ## 1.0.2 - 2025-03-20 6 | 7 | - Feature: Laravel 12 compatibility 8 | 9 | ## 1.0.1 - 2024-09-25 10 | 11 | - Bugfix: exit earlier if there are no active batches 12 | 13 | ## 1.0.0 - 2024-09-25 14 | 15 | - Initial release 16 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) macbookandrew andrewrminion.com 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Queue Cancel Batch 2 | 3 | Provides a command to cancel a specific batch of queued jobs. 4 | 5 | ![package image](resources/package-image.png) 6 | 7 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/macbookandrew/laravel-queue-cancel-batch.svg?style=flat-square)](https://packagist.org/packages/macbookandrew/laravel-queue-cancel-batch) 8 | 10 | [![Total Downloads](https://img.shields.io/packagist/dt/macbookandrew/laravel-queue-cancel-batch.svg?style=flat-square)](https://packagist.org/packages/macbookandrew/laravel-queue-cancel-batch) 11 | 12 | ## Installation 13 | 14 | You can install the package via composer: 15 | 16 | ```bash 17 | composer require macbookandrew/laravel-queue-cancel-batch 18 | ``` 19 | 20 | ## Usage 21 | 22 | ```shell 23 | # search for active batches and chancel one or more 24 | php artisan queue:cancel-batch 25 | 26 | ┌ Select one or more batches to cancel ─────────────────────────┐ 27 | │ My Test Batch (44/56 completed jobs; started 3 seconds ago) │ 28 | │ My Test Batch 2 (12/56 completed jobs; started 1 second ago) │ 29 | └───────────────────────────────────────────────────────────────┘ 30 | 31 | # cancel a batch if you already have the batch ID 32 | php artisan queue:cancel-batch 70045ad7-3279-4481-9b1e-012710f22221 33 | ``` 34 | 35 | ## Testing 36 | 37 | ```bash 38 | composer test 39 | ``` 40 | 41 | ## Changelog 42 | 43 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 44 | 45 | ## Contributing 46 | 47 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 48 | 49 | ## Security Vulnerabilities 50 | 51 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 52 | 53 | ## Credits 54 | 55 | - [Andrew Minion](https://github.com/macbookandrew) 56 | - [All Contributors](../../contributors) 57 | 58 | ## License 59 | 60 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 61 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "macbookandrew/laravel-queue-cancel-batch", 3 | "description": "Provides an Artisan command to cancel a specific batch of queued jobs", 4 | "keywords": [ 5 | "macbookandrew", 6 | "laravel", 7 | "laravel-queue-cancel-batch" 8 | ], 9 | "homepage": "https://github.com/macbookandrew/laravel-queue-cancel-batch", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Andrew Minion", 14 | "homepage": "https://andrewrminion.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "spatie/laravel-package-tools": "^1.16", 21 | "illuminate/contracts": "^10.0||^11.0||^12.0" 22 | }, 23 | "require-dev": { 24 | "laravel/pint": "^1.14", 25 | "nunomaduro/collision": "^8.1.1||^7.10.0", 26 | "larastan/larastan": "^3.0||^2.9", 27 | "orchestra/testbench": "^10.0||^9.0.0||^8.22.0", 28 | "pestphp/pest": "^3.0||^2.34", 29 | "pestphp/pest-plugin-arch": "^3.0||^2.7", 30 | "pestphp/pest-plugin-laravel": "^3.0||^2.3", 31 | "phpstan/extension-installer": "^1.3", 32 | "phpstan/phpstan-deprecation-rules": "^1.1", 33 | "phpstan/phpstan-phpunit": "^1.3" 34 | }, 35 | "autoload": { 36 | "psr-4": { 37 | "MacbookAndrew\\LaravelQueueCancelBatch\\": "src/", 38 | "MacbookAndrew\\LaravelQueueCancelBatch\\Database\\Factories\\": "database/factories/" 39 | } 40 | }, 41 | "autoload-dev": { 42 | "psr-4": { 43 | "MacbookAndrew\\LaravelQueueCancelBatch\\Tests\\": "tests/", 44 | "Workbench\\App\\": "workbench/app/" 45 | } 46 | }, 47 | "scripts": { 48 | "post-autoload-dump": "@composer run prepare", 49 | "clear": "@php vendor/bin/testbench package:purge-laravel-queue-cancel-batch --ansi", 50 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 51 | "build": [ 52 | "@composer run prepare", 53 | "@php vendor/bin/testbench workbench:build --ansi" 54 | ], 55 | "start": [ 56 | "Composer\\Config::disableProcessTimeout", 57 | "@composer run build", 58 | "@php vendor/bin/testbench serve" 59 | ], 60 | "analyse": "vendor/bin/phpstan analyse", 61 | "test": "vendor/bin/pest", 62 | "test-coverage": "vendor/bin/pest --coverage", 63 | "format": "vendor/bin/pint" 64 | }, 65 | "config": { 66 | "sort-packages": true, 67 | "allow-plugins": { 68 | "pestphp/pest-plugin": true, 69 | "phpstan/extension-installer": true 70 | } 71 | }, 72 | "extra": { 73 | "laravel": { 74 | "providers": [ 75 | "MacbookAndrew\\LaravelQueueCancelBatch\\LaravelQueueCancelBatchServiceProvider" 76 | ], 77 | "aliases": { 78 | "LaravelQueueCancelBatch": "MacbookAndrew\\LaravelQueueCancelBatch\\Facades\\LaravelQueueCancelBatch" 79 | } 80 | } 81 | }, 82 | "minimum-stability": "dev", 83 | "prefer-stable": true 84 | } 85 | -------------------------------------------------------------------------------- /config/queue-cancel-batch.php: -------------------------------------------------------------------------------- 1 | id(); 13 | 14 | // add fields 15 | 16 | $table->timestamps(); 17 | }); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /resources/package-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macbookandrew/laravel-queue-cancel-batch/b95f5b253c1cf439369d2924c53519a6de2def04/resources/package-image.png -------------------------------------------------------------------------------- /resources/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macbookandrew/laravel-queue-cancel-batch/b95f5b253c1cf439369d2924c53519a6de2def04/resources/views/.gitkeep -------------------------------------------------------------------------------- /src/Commands/LaravelQueueCancelBatchCommand.php: -------------------------------------------------------------------------------- 1 | */ 20 | private Collection $batches; 21 | 22 | public function handle(): int 23 | { 24 | /** @var BatchRepository */ 25 | $batchRepository = app()->make(BatchRepository::class); 26 | 27 | if ($this->argument('batchUuids')) { 28 | $this->batches = collect($this->argument('batchUuids')); 29 | } else { 30 | $allBatches = collect($batchRepository->get(100, null))->reject(fn (Batch $batch) => $batch->pendingJobs < 1 || $batch->finished() || $batch->canceled()); 31 | 32 | if ($allBatches->isEmpty()) { 33 | $this->info('There are no active batches.'); 34 | 35 | return self::INVALID; 36 | } 37 | 38 | /** @var array */ 39 | $batchIds = multiselect( 40 | label: 'Select one or more batches to cancel:', 41 | options: $allBatches->mapWithKeys(fn (Batch $batch) => [$batch->id => sprintf( 42 | '%s (%d/%d completed jobs; started %s)', 43 | $batch->name, 44 | $batch->pendingJobs, 45 | $batch->totalJobs, 46 | $batch->createdAt->diffForHumans(), 47 | )])->all() 48 | ); 49 | 50 | $this->batches = collect($batchIds); 51 | } 52 | 53 | if ($this->batches->isEmpty()) { 54 | $this->info('There are no active batches.'); 55 | 56 | return self::INVALID; 57 | } 58 | 59 | $this->batches->each(function (string $uuid) { 60 | $batch = Bus::findBatch($uuid); 61 | 62 | if (! $batch) { 63 | $this->error('Could not find batch '.$uuid); 64 | 65 | return; 66 | } 67 | 68 | $batch->cancel(); 69 | 70 | $this->info('Cancelled batch '.$batch->name); 71 | }); 72 | 73 | return self::SUCCESS; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Facades/LaravelQueueCancelBatch.php: -------------------------------------------------------------------------------- 1 | name('laravel-queue-cancel-batch') 20 | ->hasCommand(LaravelQueueCancelBatchCommand::class); 21 | } 22 | } 23 | --------------------------------------------------------------------------------