├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Console │ └── RestartCommand.php ├── Jobs │ └── HorizonRestartJob.php └── RestartServiceProvider.php └── tests ├── Feature └── CommandTest.php ├── Pest.php └── TestCase.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 D.J.Hwang 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-horizon-restart 2 | 3 | [![Latest Test](https://github.com/huangdijia/laravel-horizon-restart/workflows/tests/badge.svg)](https://github.com/huangdijia/laravel-horizon-restart/actions) 4 | [![Latest Stable Version](https://poser.pugx.org/huangdijia/laravel-horizon-restart/v)](https://packagist.org/packages/huangdijia/laravel-horizon-restart) 5 | [![Total Downloads](https://poser.pugx.org/huangdijia/laravel-horizon-restart/downloads)](https://packagist.org/packages/huangdijia/laravel-horizon-restart) 6 | [![Monthly Downloads](https://poser.pugx.org/huangdijia/laravel-horizon-restart/d/monthly)](https://packagist.org/packages/huangdijia/laravel-horizon-restart) 7 | [![Daily Downloads](https://poser.pugx.org/huangdijia/laravel-horizon-restart/d/daily)](https://packagist.org/packages/huangdijia/laravel-horizon-restart) 8 | [![License](https://poser.pugx.org/huangdijia/laravel-horizon-restart/license)](https://packagist.org/packages/huangdijia/laravel-horizon-restart) 9 | [![PHP Version Require](https://poser.pugx.org/huangdijia/laravel-horizon-restart/require/php)](https://packagist.org/packages/huangdijia/laravel-horizon-restart) 10 | 11 | Restart the Horizon supervisors of multiple servers like `php artisan queue:restart` 12 | 13 | ## Installation 14 | 15 | ```bash 16 | composer require huangdijia/laravel-horizon-restart:^2.0 17 | ``` 18 | 19 | ## Usage 20 | 21 | ```bash 22 | php artisan horizon:restart 23 | ``` 24 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "huangdijia/laravel-horizon-restart", 3 | "description": "Horizon Restart for Laravel.", 4 | "type": "library", 5 | "keywords": [ 6 | "laravel", 7 | "horizon" 8 | ], 9 | "homepage": "https://github.com/huangdijia/laravel-horizon-restart", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "huangdijia", 14 | "email": "huangdijia@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=8.2", 19 | "illuminate/support": "^11.0|^12.0", 20 | "illuminate/console": "^11.0|^12.0", 21 | "illuminate/bus": "^11.0|^12.0", 22 | "laravel/horizon": "^5.0|^6.0" 23 | }, 24 | "require-dev": { 25 | "phpstan/phpstan": "^1.0|^2.1", 26 | "laravel/framework": "^11.0|^12.0", 27 | "pestphp/pest": "^3.7", 28 | "pestphp/pest-plugin-laravel": "^2.0|^3.1", 29 | "huangdijia/php-coding-standard": "^2.0" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Huangdijia\\Horizon\\": "src/" 34 | } 35 | }, 36 | "extra": { 37 | "branch-alias": { 38 | "dev-main": "4.0-dev" 39 | }, 40 | "laravel": { 41 | "providers": [ 42 | "Huangdijia\\Horizon\\RestartServiceProvider" 43 | ] 44 | } 45 | }, 46 | "minimum-stability": "dev", 47 | "scripts": { 48 | "analyse": "@php vendor/bin/phpstan analyse --memory-limit 300M -l 0 -c phpstan.neon ./src", 49 | "cs-fix": "@php vendor/bin/php-cs-fixer fix $1", 50 | "test": "@php vendor/bin/pest" 51 | }, 52 | "config": { 53 | "allow-plugins": { 54 | "pestphp/pest-plugin": true, 55 | "ergebnis/composer-normalize": true 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests 6 | 7 | 8 | 9 | 10 | ./app 11 | ./src 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Console/RestartCommand.php: -------------------------------------------------------------------------------- 1 | laravel->make(MasterSupervisorRepository::class); 42 | $masters = $repository->all(); 43 | 44 | $env = app('config')['app.env'] ?? 'local'; 45 | $connection = app('config')['horizon.environments.' . $env . '.supervisor-horizon-restart.connection'] ?? 'redis'; 46 | 47 | collect($masters)->each(function ($master) use ($connection) { 48 | $queue = Str::substr($master->name, 0, -5); 49 | HorizonRestartJob::dispatch()->onQueue($queue)->onConnection($connection); 50 | 51 | $this->info("Server [{$queue}] terminated"); 52 | }); 53 | 54 | $this->info('Horizon restarted successfully.'); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Jobs/HorizonRestartJob.php: -------------------------------------------------------------------------------- 1 | registerCommands(); 24 | } 25 | 26 | public function register() 27 | { 28 | $this->configure(); 29 | } 30 | 31 | protected function configure() 32 | { 33 | $env = $this->app['config']['app.env'] ?? 'local'; 34 | $environments = $this->app['config']['horizon.environments.' . $env] ?? []; 35 | $connection = current($environments)['connection'] ?? 'redis'; 36 | $queue = MasterSupervisor::basename(); 37 | 38 | $this->app['config']->set('horizon.environments.' . $env . '.supervisor-horizon-restart', [ 39 | 'connection' => $connection, 40 | 'queue' => [$queue], 41 | 'balance' => 'simple', 42 | 'processes' => 1, 43 | 'tries' => 3, 44 | ]); 45 | } 46 | 47 | protected function registerCommands() 48 | { 49 | if ($this->app->runningInConsole()) { 50 | $this->commands([ 51 | Console\RestartCommand::class, 52 | ]); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/Feature/CommandTest.php: -------------------------------------------------------------------------------- 1 | toBeTrue(); 13 | }); 14 | -------------------------------------------------------------------------------- /tests/Pest.php: -------------------------------------------------------------------------------- 1 | in('Feature'); 14 | // uses(TestCase::class)->in('Feature'); 15 | 16 | /* 17 | |-------------------------------------------------------------------------- 18 | | Expectations 19 | |-------------------------------------------------------------------------- 20 | | 21 | | When you're writing tests, you often need to check that values meet certain conditions. The 22 | | "expect()" function gives you access to a set of "expectations" methods that you can use 23 | | to assert different things. Of course, you may extend the Expectation API at any time. 24 | | 25 | */ 26 | 27 | expect()->extend('toBeOne', function () { 28 | return $this->toBe(1); 29 | }); 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Functions 34 | |-------------------------------------------------------------------------- 35 | | 36 | | While Pest is very powerful out-of-the-box, you may have some testing code specific to your 37 | | project that you don't want to repeat in every file. Here you can also expose helpers as 38 | | global functions to help you to reduce the number of lines of code in your test files. 39 | | 40 | */ 41 | 42 | function something() 43 | { 44 | // .. 45 | } 46 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 |