|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Collection; |
|
6
|
|
|
use App\Models\Settings; |
|
7
|
|
|
use App\Services\Tmux\TmuxMonitorService; |
|
8
|
|
|
use App\Services\Tmux\TmuxSessionManager; |
|
9
|
|
|
use App\Services\Tmux\TmuxTaskRunner; |
|
10
|
|
|
use Blacklight\ColorCLI; |
|
11
|
|
|
use Blacklight\TmuxOutput; |
|
12
|
|
|
use Illuminate\Console\Command; |
|
13
|
|
|
use Illuminate\Support\Facades\DB; |
|
14
|
|
|
|
|
15
|
|
|
class TmuxMonitor extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The name and signature of the console command. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $signature = 'tmux:monitor |
|
23
|
|
|
{--session= : Tmux session name} |
|
24
|
|
|
{--reset-collections : Reset old collections before starting}'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The console command description. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $description = 'Monitor and manage tmux processing panes (modernized)'; |
|
32
|
|
|
|
|
33
|
|
|
private TmuxSessionManager $sessionManager; |
|
34
|
|
|
|
|
35
|
|
|
private TmuxMonitorService $monitor; |
|
36
|
|
|
|
|
37
|
|
|
private TmuxTaskRunner $taskRunner; |
|
38
|
|
|
|
|
39
|
|
|
private TmuxOutput $tmuxOutput; |
|
40
|
|
|
|
|
41
|
|
|
private ColorCLI $colorCli; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Execute the console command. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function handle(): int |
|
47
|
|
|
{ |
|
48
|
|
|
$this->colorCli = new ColorCLI; |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
// Reset old collections if requested |
|
52
|
|
|
if ($this->option('reset-collections')) { |
|
53
|
|
|
$this->resetOldCollections(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Initialize services |
|
57
|
|
|
$sessionName = $this->option('session') |
|
58
|
|
|
?? Settings::settingValue('tmux_session') |
|
59
|
|
|
?? config('tmux.session.default_name', 'nntmux'); |
|
60
|
|
|
|
|
61
|
|
|
$this->sessionManager = new TmuxSessionManager($sessionName); |
|
62
|
|
|
$this->monitor = new TmuxMonitorService; |
|
63
|
|
|
$this->taskRunner = new TmuxTaskRunner($sessionName); |
|
64
|
|
|
$this->tmuxOutput = new TmuxOutput; |
|
65
|
|
|
|
|
66
|
|
|
// Verify session exists |
|
67
|
|
|
if (! $this->sessionManager->sessionExists()) { |
|
68
|
|
|
$this->error("❌ Tmux session '{$sessionName}' does not exist."); |
|
69
|
|
|
$this->info("💡 Run 'php artisan tmux-ui:start' to create the session first."); |
|
70
|
|
|
|
|
71
|
|
|
return Command::FAILURE; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->colorCli->header('Starting Tmux Monitor'); |
|
75
|
|
|
$this->info("📊 Monitoring session: {$sessionName}"); |
|
76
|
|
|
|
|
77
|
|
|
// Initialize monitor |
|
78
|
|
|
$runVar = $this->monitor->initializeMonitor(); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
// Main monitoring loop |
|
81
|
|
|
$iteration = 0; |
|
82
|
|
|
while ($this->monitor->shouldContinue()) { |
|
83
|
|
|
$iteration++; |
|
84
|
|
|
|
|
85
|
|
|
// Collect statistics |
|
86
|
|
|
$runVar = $this->monitor->collectStatistics(); |
|
87
|
|
|
|
|
88
|
|
|
// Update display |
|
89
|
|
|
$this->tmuxOutput->updateMonitorPane($runVar); |
|
90
|
|
|
|
|
91
|
|
|
// Run pane tasks if tmux is running |
|
92
|
|
|
if ((int) ($runVar['settings']['is_running'] ?? 0) === 1) { |
|
93
|
|
|
$this->runPaneTasks($runVar); |
|
94
|
|
|
} else { |
|
95
|
|
|
if ($iteration % 60 === 0) { // Log every 10 minutes |
|
96
|
|
|
$this->info('⏸️ Tmux is not running. Waiting...'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Increment iteration and sleep |
|
101
|
|
|
$this->monitor->incrementIteration(); |
|
102
|
|
|
sleep(10); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->info('🛑 Monitor stopped by exit flag'); |
|
106
|
|
|
|
|
107
|
|
|
return Command::SUCCESS; |
|
108
|
|
|
|
|
109
|
|
|
} catch (\Exception $e) { |
|
110
|
|
|
$this->error('❌ Monitor failed: '.$e->getMessage()); |
|
111
|
|
|
logger()->error('Tmux monitor error', [ |
|
112
|
|
|
'message' => $e->getMessage(), |
|
113
|
|
|
'trace' => $e->getTraceAsString(), |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
return Command::FAILURE; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Reset old collections based on delay time |
|
122
|
|
|
*/ |
|
123
|
|
|
private function resetOldCollections(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$delayTime = (int) (Settings::settingValue('delaytime') ?? 2); |
|
126
|
|
|
|
|
127
|
|
|
$this->colorCli->header('Resetting expired collections...'); |
|
128
|
|
|
|
|
129
|
|
|
try { |
|
130
|
|
|
DB::transaction(function () use ($delayTime) { |
|
131
|
|
|
$count = Collection::query() |
|
132
|
|
|
->where('dateadded', '<', now()->subHours($delayTime)) |
|
133
|
|
|
->update(['dateadded' => now()]); |
|
134
|
|
|
|
|
135
|
|
|
if ($count > 0) { |
|
136
|
|
|
$this->info("✅ Reset {$count} collections"); |
|
137
|
|
|
} else { |
|
138
|
|
|
$this->info('✅ No collections needed resetting'); |
|
139
|
|
|
} |
|
140
|
|
|
}, 10); |
|
141
|
|
|
|
|
142
|
|
|
} catch (\Exception $e) { |
|
143
|
|
|
$this->error('Failed to reset collections: '.$e->getMessage()); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Run tasks in appropriate panes |
|
149
|
|
|
*/ |
|
150
|
|
|
private function runPaneTasks(array $runVar): void |
|
151
|
|
|
{ |
|
152
|
|
|
$sequential = (int) ($runVar['constants']['sequential'] ?? 0); |
|
153
|
|
|
|
|
154
|
|
|
// Always run IRC scraper |
|
155
|
|
|
$this->runIRCScraper($runVar); |
|
156
|
|
|
|
|
157
|
|
|
// Run main tasks based on sequential mode |
|
158
|
|
|
if ($sequential === 2) { |
|
159
|
|
|
// Stripped mode - only essential tasks |
|
160
|
|
|
$this->runSequentialTasks($runVar); |
|
161
|
|
|
} elseif ($sequential === 1) { |
|
162
|
|
|
// Basic sequential mode |
|
163
|
|
|
$this->runBasicTasks($runVar); |
|
164
|
|
|
} else { |
|
165
|
|
|
// Full non-sequential mode |
|
166
|
|
|
$this->runFullTasks($runVar); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Run IRC scraper |
|
172
|
|
|
*/ |
|
173
|
|
|
private function runIRCScraper(array $runVar): void |
|
174
|
|
|
{ |
|
175
|
|
|
$this->taskRunner->runIRCScraper([ |
|
176
|
|
|
'constants' => $runVar['constants'], |
|
177
|
|
|
]); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Run full non-sequential tasks |
|
182
|
|
|
*/ |
|
183
|
|
|
private function runFullTasks(array $runVar): void |
|
184
|
|
|
{ |
|
185
|
|
|
// Update binaries |
|
186
|
|
|
$this->taskRunner->runBinariesUpdate($runVar); |
|
187
|
|
|
|
|
188
|
|
|
// Backfill |
|
189
|
|
|
$this->taskRunner->runBackfill($runVar); |
|
190
|
|
|
|
|
191
|
|
|
// Update releases |
|
192
|
|
|
$this->taskRunner->runReleasesUpdate(array_merge($runVar, ['pane' => '0.3'])); |
|
193
|
|
|
|
|
194
|
|
|
// Post-processing and cleanup tasks |
|
195
|
|
|
$this->runPostProcessingTasks($runVar); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Run basic sequential tasks |
|
200
|
|
|
*/ |
|
201
|
|
|
private function runBasicTasks(array $runVar): void |
|
202
|
|
|
{ |
|
203
|
|
|
// Update releases |
|
204
|
|
|
$this->taskRunner->runReleasesUpdate(array_merge($runVar, ['pane' => '0.1'])); |
|
205
|
|
|
|
|
206
|
|
|
// Post-processing and cleanup tasks |
|
207
|
|
|
$this->runPostProcessingTasks($runVar); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Run stripped sequential tasks |
|
212
|
|
|
*/ |
|
213
|
|
|
private function runSequentialTasks(array $runVar): void |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
// Minimal tasks for complete sequential mode |
|
216
|
|
|
// Tasks are handled by the sequential script itself |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Run post-processing tasks (common to most modes) |
|
221
|
|
|
*/ |
|
222
|
|
|
private function runPostProcessingTasks(array $runVar): void |
|
223
|
|
|
{ |
|
224
|
|
|
$sequential = (int) ($runVar['constants']['sequential'] ?? 0); |
|
225
|
|
|
|
|
226
|
|
|
if ($sequential === 2) { |
|
227
|
|
|
// Skip post-processing in complete sequential mode |
|
228
|
|
|
return; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
// These tasks would be implemented in TmuxTaskRunner |
|
232
|
|
|
// For now, we're keeping the structure similar to the original |
|
233
|
|
|
// but using the modern service architecture |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|