|
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
|
|
|
return Command::SUCCESS; |
|
66
|
|
|
|
|
67
|
|
|
} catch (\Exception $e) { |
|
68
|
|
|
$this->error('β Failed to start Tmux UI: ' . $e->getMessage()); |
|
69
|
|
|
return Command::FAILURE; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check system resources before starting |
|
75
|
|
|
*/ |
|
76
|
|
|
private function checkSystemResources(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$recommendations = UpdatePerformanceHelper::checkSystemResources(); |
|
79
|
|
|
|
|
80
|
|
|
if (!empty($recommendations)) { |
|
81
|
|
|
$this->warn('β οΈ System resource warnings:'); |
|
82
|
|
|
foreach ($recommendations as $recommendation) { |
|
83
|
|
|
$this->line(" β’ $recommendation"); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (!$this->confirm('Continue despite warnings?')) { |
|
87
|
|
|
throw new \Exception('Aborted due to system resource concerns'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check if tmux session is running |
|
94
|
|
|
*/ |
|
95
|
|
|
private function isSessionRunning(string $sessionName): bool |
|
96
|
|
|
{ |
|
97
|
|
|
$process = Process::timeout(10)->run("tmux list-session 2>/dev/null | grep -q '^$sessionName:'"); |
|
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); |
|
|
|
|
|
|
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
|
|
|
|