|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ariadata\LarastackSupervisor\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Ariadata\LarastackSupervisor\LarastackSupervisor; |
| 7 | + |
| 8 | +class SupervisorCommand extends Command |
| 9 | +{ |
| 10 | + // Signature to handle various subcommands and arguments |
| 11 | + protected $signature = 'supervisor {action?} {service?}'; |
| 12 | + protected $description = 'Manage Larastack Supervisor processes'; |
| 13 | + |
| 14 | + public function handle() |
| 15 | + { |
| 16 | + // Retrieve the action and service arguments |
| 17 | + $action = $this->argument('action'); |
| 18 | + $service = $this->argument('service') ?? 'all'; |
| 19 | + |
| 20 | + // If no action is provided or help is requested, show help |
| 21 | + if (!$action || $action === 'help') { |
| 22 | + return $this->showHelp(); |
| 23 | + } |
| 24 | + |
| 25 | + // Handle the various actions |
| 26 | + switch ($action) { |
| 27 | + case 'list': |
| 28 | + return $this->listProcesses(); |
| 29 | + |
| 30 | + case 'status': |
| 31 | + return $this->status($service); |
| 32 | + |
| 33 | + case 'start': |
| 34 | + return $this->start($service); |
| 35 | + |
| 36 | + case 'stop': |
| 37 | + return $this->stop($service); |
| 38 | + |
| 39 | + case 'restart': |
| 40 | + return $this->restart($service); |
| 41 | + |
| 42 | + default: |
| 43 | + // If the action is unknown, show the help message |
| 44 | + $this->error("Unknown action: $action"); |
| 45 | + return $this->showHelp(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Show help information if no arguments are provided or 'help' is requested. |
| 51 | + */ |
| 52 | + protected function showHelp() |
| 53 | + { |
| 54 | + $this->info('Usage:'); |
| 55 | + $this->line(' php artisan supervisor list'); |
| 56 | + $this->line(' php artisan supervisor status [all|service-name]'); |
| 57 | + $this->line(' php artisan supervisor start [all|service-name]'); |
| 58 | + $this->line(' php artisan supervisor stop [all|service-name]'); |
| 59 | + $this->line(' php artisan supervisor restart [all|service-name]'); |
| 60 | + $this->line(' php artisan supervisor help'); |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * List all processes managed by Supervisor. |
| 66 | + */ |
| 67 | + protected function listProcesses() |
| 68 | + { |
| 69 | + $processes = LarastackSupervisor::list(); |
| 70 | + if (empty($processes)) { |
| 71 | + $this->info('No processes found.'); |
| 72 | + } else { |
| 73 | + foreach ($processes as $process) { |
| 74 | + $this->line($process); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Show the status of a specific service or all services. |
| 81 | + */ |
| 82 | + protected function status(string $service): void |
| 83 | + { |
| 84 | + $statuses = LarastackSupervisor::status($service); |
| 85 | + if (isset($statuses['name'])) { |
| 86 | + // Single service status |
| 87 | + $this->info("Service: {$statuses['name']} - Status: {$statuses['status']}"); |
| 88 | + } else { |
| 89 | + // Multiple services status |
| 90 | + foreach ($statuses as $process) { |
| 91 | + $this->info("Service: {$process['name']} - Status: {$process['status']}"); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Start a specific service or all services. |
| 98 | + */ |
| 99 | + protected function start(string $service): void |
| 100 | + { |
| 101 | + if (LarastackSupervisor::start($service)) { |
| 102 | + $this->info("Supervisor process {$service} has been started."); |
| 103 | + } else { |
| 104 | + $this->error("Failed to start Supervisor process {$service}."); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Stop a specific service or all services. |
| 110 | + */ |
| 111 | + protected function stop(string $service): void |
| 112 | + { |
| 113 | + if (LarastackSupervisor::stop($service)) { |
| 114 | + $this->info("Supervisor process {$service} has been stopped."); |
| 115 | + } else { |
| 116 | + $this->error("Failed to stop Supervisor process {$service}."); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Restart a specific service or all services. |
| 122 | + */ |
| 123 | + protected function restart(string $service): void |
| 124 | + { |
| 125 | + if (LarastackSupervisor::restart($service)) { |
| 126 | + $this->info("Supervisor process {$service} has been restarted."); |
| 127 | + } else { |
| 128 | + $this->error("Failed to restart Supervisor process {$service}."); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments