1 | <?php |
||
2 | |||
3 | namespace App\Console\Commands; |
||
4 | |||
5 | use App\Models\Settings; |
||
6 | use App\Support\UpdatePerformanceHelper; |
||
7 | use Blacklight\Tmux; |
||
8 | use Illuminate\Console\Command; |
||
9 | use Illuminate\Support\Facades\Process; |
||
10 | |||
11 | class TmuxUIStart extends Command |
||
12 | { |
||
13 | /** |
||
14 | * The name and signature of the console command. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $signature = 'tmux-ui:start |
||
19 | {--timeout=300 : Timeout for tmux operations} |
||
20 | {--force : Force start even if session exists} |
||
21 | {--monitor : Enable monitoring mode}'; |
||
22 | |||
23 | /** |
||
24 | * The console command description. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description = 'Start the processing of tmux scripts with improved performance'; |
||
29 | |||
30 | /** |
||
31 | * Execute the console command. |
||
32 | */ |
||
33 | public function handle(): int |
||
34 | { |
||
35 | try { |
||
36 | $this->info('🚀 Starting Tmux UI...'); |
||
37 | |||
38 | $tmux = new Tmux; |
||
39 | $tmuxSession = Settings::settingValue('tmux_session') ?? 0; |
||
40 | $timeout = (int) $this->option('timeout'); |
||
41 | |||
42 | // Check system resources before starting |
||
43 | $this->checkSystemResources(); |
||
44 | |||
45 | // Check if session already exists |
||
46 | if (! $this->option('force') && $this->isSessionRunning($tmuxSession)) { |
||
47 | $this->error("❌ Tmux session '$tmuxSession' is already running"); |
||
48 | |||
49 | if (! $this->confirm('Would you like to restart the session?')) { |
||
50 | return Command::FAILURE; |
||
51 | } |
||
52 | |||
53 | $this->call('tmux-ui:stop', ['--kill' => true]); |
||
54 | sleep(2); // Brief pause to ensure clean shutdown |
||
55 | } |
||
56 | |||
57 | // Set running state |
||
58 | $tmux->startRunning(); |
||
59 | $this->line(' ✓ Tmux running state activated'); |
||
60 | |||
61 | // Start tmux session |
||
62 | $this->startTmuxSession($tmuxSession, $timeout); |
||
63 | |||
64 | $this->info('✅ Tmux UI started successfully'); |
||
65 | |||
66 | return Command::SUCCESS; |
||
67 | |||
68 | } catch (\Exception $e) { |
||
69 | $this->error('❌ Failed to start Tmux UI: '.$e->getMessage()); |
||
70 | |||
71 | return Command::FAILURE; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Check system resources before starting |
||
77 | */ |
||
78 | private function checkSystemResources(): void |
||
79 | { |
||
80 | $recommendations = UpdatePerformanceHelper::checkSystemResources(); |
||
81 | |||
82 | if (! empty($recommendations)) { |
||
83 | $this->warn('⚠️ System resource information:'); |
||
84 | foreach ($recommendations as $recommendation) { |
||
85 | $this->line(" • $recommendation"); |
||
86 | } |
||
87 | $this->line(''); // Add empty line for better readability |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Check if tmux session is running |
||
93 | */ |
||
94 | private function isSessionRunning(string $sessionName): bool |
||
95 | { |
||
96 | $process = Process::timeout(10)->run("tmux list-session 2>/dev/null | grep -q '^$sessionName:'"); |
||
97 | |||
98 | return $process->successful(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Start the tmux session |
||
103 | */ |
||
104 | private function startTmuxSession(string $sessionName, int $timeout): void |
||
105 | { |
||
106 | $this->info('🔧 Initializing tmux session...'); |
||
107 | |||
108 | $runScript = base_path('misc/update/tmux/run.php'); |
||
109 | |||
110 | if (! file_exists($runScript)) { |
||
111 | throw new \Exception("Tmux run script not found: $runScript"); |
||
112 | } |
||
113 | |||
114 | // Ensure the run script is executable |
||
115 | chmod($runScript, 0755); |
||
116 | |||
117 | // Start the tmux session with better error handling |
||
118 | $command = "php $runScript"; |
||
119 | |||
120 | if ($this->option('monitor')) { |
||
121 | // Start in monitoring mode |
||
122 | $this->info('📊 Starting in monitoring mode...'); |
||
123 | $process = Process::timeout($timeout)->tty()->run($command); |
||
124 | |||
125 | if (! $process->successful()) { |
||
126 | throw new \Exception('Tmux session failed to start: '.$process->errorOutput()); |
||
127 | } |
||
128 | } else { |
||
129 | // Start in background |
||
130 | $this->info('🔄 Starting tmux session in background...'); |
||
131 | $process = Process::timeout(30)->start($command); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
132 | |||
133 | // Give it a moment to start |
||
134 | sleep(3); |
||
135 | |||
136 | if (! $this->isSessionRunning($sessionName)) { |
||
137 | throw new \Exception('Failed to start tmux session'); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $this->line(' ✓ Tmux session started successfully'); |
||
142 | } |
||
143 | } |
||
144 |