| Total Complexity | 76 |
| Total Lines | 562 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TmuxTaskRunner often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TmuxTaskRunner, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class TmuxTaskRunner |
||
| 12 | { |
||
| 13 | protected TmuxPaneManager $paneManager; |
||
| 14 | |||
| 15 | protected ColorCLI $colorCli; |
||
| 16 | |||
| 17 | protected string $sessionName; |
||
| 18 | |||
| 19 | public function __construct(string $sessionName) |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Run a task in a specific pane |
||
| 28 | */ |
||
| 29 | public function runTask(string $taskName, array $config): bool |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Disable a pane with a message |
||
| 55 | */ |
||
| 56 | protected function disablePane(string $pane, string $taskName, string $reason): bool |
||
| 57 | { |
||
| 58 | $color = $this->getRandomColor(); |
||
| 59 | $message = "echo \"\\033[38;5;{$color}m\\n{$taskName} has been disabled: {$reason}\""; |
||
| 60 | |||
| 61 | return $this->paneManager->respawnPane($pane, $message, kill: true); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Build a command with logging |
||
| 66 | */ |
||
| 67 | public function buildCommand(string $baseCommand, array $options = []): string |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Build sleep command |
||
| 91 | */ |
||
| 92 | protected function buildSleepCommand(int $seconds): string |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get log file path for a pane |
||
| 106 | */ |
||
| 107 | protected function getLogFile(string $paneName): string |
||
| 108 | { |
||
| 109 | $logsEnabled = (int) Settings::settingValue('write_logs') === 1; |
||
| 110 | |||
| 111 | if (! $logsEnabled) { |
||
| 112 | return '/dev/null'; |
||
| 113 | } |
||
| 114 | |||
| 115 | $logDir = config('tmux.paths.logs', storage_path('logs/tmux')); |
||
| 116 | |||
| 117 | if (! is_dir($logDir)) { |
||
| 118 | mkdir($logDir, 0755, true); |
||
| 119 | } |
||
| 120 | |||
| 121 | $date = now()->format('Y_m_d'); |
||
| 122 | |||
| 123 | return "{$logDir}/{$paneName}-{$date}.log"; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get a random color for terminal output |
||
| 128 | */ |
||
| 129 | protected function getRandomColor(): int |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Run the IRC scraper |
||
| 157 | */ |
||
| 158 | public function runIRCScraper(array $config): bool |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Run binaries update |
||
| 177 | */ |
||
| 178 | public function runBinariesUpdate(array $config): bool |
||
| 179 | { |
||
| 180 | $enabled = (int) ($config['settings']['binaries_run'] ?? 0); |
||
| 181 | $killswitch = $config['killswitch']['pp'] ?? false; |
||
| 182 | $pane = '0.1'; |
||
| 183 | |||
| 184 | if (! $enabled) { |
||
| 185 | return $this->disablePane($pane, 'Update Binaries', 'disabled in settings'); |
||
| 186 | } |
||
| 187 | |||
| 188 | if ($killswitch) { |
||
| 189 | return $this->disablePane($pane, 'Update Binaries', 'postprocess kill limit exceeded'); |
||
| 190 | } |
||
| 191 | |||
| 192 | $artisanCommand = match ((int) $enabled) { |
||
| 193 | 1 => 'multiprocessing:safe binaries', |
||
| 194 | default => null, |
||
| 195 | }; |
||
| 196 | |||
| 197 | if (! $artisanCommand) { |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | |||
| 201 | $niceness = Settings::settingValue('niceness') ?? 2; |
||
| 202 | $command = "nice -n{$niceness} ".PHP_BINARY." artisan {$artisanCommand}"; |
||
| 203 | $sleep = (int) ($config['settings']['bins_timer'] ?? 60); |
||
| 204 | $command = $this->buildCommand($command, ['log_pane' => 'binaries', 'sleep' => $sleep]); |
||
| 205 | |||
| 206 | return $this->paneManager->respawnPane($pane, $command); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Run backfill |
||
| 211 | */ |
||
| 212 | public function runBackfill(array $config): bool |
||
| 213 | { |
||
| 214 | $enabled = (int) ($config['settings']['backfill'] ?? 0); |
||
| 215 | $collKillswitch = $config['killswitch']['coll'] ?? false; |
||
| 216 | $ppKillswitch = $config['killswitch']['pp'] ?? false; |
||
| 217 | $pane = '0.2'; |
||
| 218 | |||
| 219 | if (! $enabled) { |
||
| 220 | return $this->disablePane($pane, 'Backfill', 'disabled in settings'); |
||
| 221 | } |
||
| 222 | |||
| 223 | if ($collKillswitch || $ppKillswitch) { |
||
| 224 | return $this->disablePane($pane, 'Backfill', 'kill limit exceeded'); |
||
| 225 | } |
||
| 226 | |||
| 227 | $artisanCommand = match ((int) $enabled) { |
||
| 228 | 1 => 'multiprocessing:backfill', |
||
| 229 | 4 => 'multiprocessing:safe backfill', |
||
| 230 | default => null, |
||
| 231 | }; |
||
| 232 | |||
| 233 | if (! $artisanCommand) { |
||
| 234 | return false; |
||
| 235 | } |
||
| 236 | |||
| 237 | // Calculate sleep time (progressive if enabled) |
||
| 238 | $baseSleep = (int) ($config['settings']['back_timer'] ?? 600); |
||
| 239 | $collections = (int) ($config['counts']['now']['collections_table'] ?? 0); |
||
| 240 | $progressive = (int) ($config['settings']['progressive'] ?? 0); |
||
| 241 | |||
| 242 | $sleep = ($progressive === 1 && floor($collections / 500) > $baseSleep) |
||
| 243 | ? floor($collections / 500) |
||
| 244 | : $baseSleep; |
||
| 245 | |||
| 246 | $niceness = Settings::settingValue('niceness') ?? 2; |
||
| 247 | $command = "nice -n{$niceness} ".PHP_BINARY." artisan {$artisanCommand}"; |
||
| 248 | $command = $this->buildCommand($command, ['log_pane' => 'backfill', 'sleep' => $sleep]); |
||
| 249 | |||
| 250 | return $this->paneManager->respawnPane($pane, $command); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Run releases update |
||
| 255 | */ |
||
| 256 | public function runReleasesUpdate(array $config): bool |
||
| 257 | { |
||
| 258 | $enabled = (int) ($config['settings']['releases_run'] ?? 0); |
||
| 259 | $pane = $config['pane'] ?? '0.3'; |
||
| 260 | |||
| 261 | if (! $enabled) { |
||
| 262 | return $this->disablePane($pane, 'Update Releases', 'disabled in settings'); |
||
| 263 | } |
||
| 264 | |||
| 265 | $niceness = Settings::settingValue('niceness') ?? 2; |
||
| 266 | $command = "nice -n{$niceness} ".PHP_BINARY.' artisan multiprocessing:releases'; |
||
| 267 | $sleep = (int) ($config['settings']['rel_timer'] ?? 60); |
||
| 268 | $command = $this->buildCommand($command, ['log_pane' => 'releases', 'sleep' => $sleep]); |
||
| 269 | |||
| 270 | return $this->paneManager->respawnPane($pane, $command); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Run a specific pane task based on task name |
||
| 275 | * |
||
| 276 | * @param string $taskName The name of the task to run |
||
| 277 | * @param array $config Configuration for the task (target pane, etc.) |
||
| 278 | * @param array $runVar Runtime variables and settings |
||
| 279 | * @return bool Success status |
||
| 280 | */ |
||
| 281 | public function runPaneTask(string $taskName, array $config, array $runVar): bool |
||
|
|
|||
| 282 | { |
||
| 283 | $sequential = (int) ($runVar['constants']['sequential'] ?? 0); |
||
| 284 | |||
| 285 | return match ($taskName) { |
||
| 286 | 'main' => $this->runMainTask($sequential, $runVar), |
||
| 287 | 'fixnames' => $this->runFixNamesTask($runVar), |
||
| 288 | 'removecrap' => $this->runRemoveCrapTask($runVar), |
||
| 289 | 'ppadditional' => $this->runPostProcessAdditional($runVar), |
||
| 290 | 'nonamazon' => $this->runNonAmazonTask($runVar), |
||
| 291 | 'amazon' => $this->runAmazonTask($runVar), |
||
| 292 | 'scraper' => $this->runIRCScraper($runVar), |
||
| 293 | default => false, |
||
| 294 | }; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Run main task (varies by sequential mode) |
||
| 299 | */ |
||
| 300 | protected function runMainTask(int $sequential, array $runVar): bool |
||
| 301 | { |
||
| 302 | return match ($sequential) { |
||
| 303 | 0 => $this->runMainNonSequential($runVar), |
||
| 304 | 1 => $this->runMainBasic($runVar), |
||
| 305 | 2 => $this->runMainSequential($runVar), |
||
| 306 | default => false, |
||
| 307 | }; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Run main non-sequential task (binaries, backfill, releases) |
||
| 312 | */ |
||
| 313 | protected function runMainNonSequential(array $runVar): bool |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Run main basic sequential task (just releases) |
||
| 326 | */ |
||
| 327 | protected function runMainBasic(array $runVar): bool |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Run main full sequential task |
||
| 334 | */ |
||
| 335 | protected function runMainSequential(array $runVar): bool |
||
| 336 | { |
||
| 337 | // Full sequential mode - runs group:update-all for each group |
||
| 338 | $pane = '0.1'; |
||
| 339 | |||
| 340 | $niceness = Settings::settingValue('niceness') ?? 2; |
||
| 341 | $artisan = base_path('artisan'); |
||
| 342 | $command = "nice -n{$niceness} php {$artisan} group:update-all"; |
||
| 343 | $command = $this->buildCommand($command, ['log_pane' => 'sequential']); |
||
| 344 | |||
| 345 | return $this->paneManager->respawnPane($pane, $command); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Run fix release names task |
||
| 350 | */ |
||
| 351 | protected function runFixNamesTask(array $runVar): bool |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Run remove crap releases task |
||
| 383 | */ |
||
| 384 | protected function runRemoveCrapTask(array $runVar): bool |
||
| 385 | { |
||
| 386 | $option = $runVar['settings']['fix_crap_opt'] ?? 'Disabled'; |
||
| 387 | $pane = '1.1'; |
||
| 388 | |||
| 389 | // Handle disabled state |
||
| 390 | if ($option === 'Disabled' || $option === 0 || $option === '0') { |
||
| 391 | return $this->disablePane($pane, 'Remove Crap', 'disabled in settings'); |
||
| 392 | } |
||
| 393 | |||
| 394 | $niceness = Settings::settingValue('niceness') ?? 2; |
||
| 395 | $artisan = base_path('artisan'); |
||
| 396 | $sleep = (int) ($runVar['settings']['crap_timer'] ?? 300); |
||
| 397 | |||
| 398 | // Handle 'All' mode - run all types with 2 hour time limit |
||
| 399 | if ($option === 'All') { |
||
| 400 | $command = "nice -n{$niceness} php {$artisan} releases:remove-crap --time=2 --delete"; |
||
| 401 | $command = $this->buildCommand($command, ['log_pane' => 'removecrap', 'sleep' => $sleep]); |
||
| 402 | |||
| 403 | return $this->paneManager->respawnPane($pane, $command); |
||
| 404 | } |
||
| 405 | |||
| 406 | // Handle 'Custom' mode - run all selected types sequentially |
||
| 407 | if ($option === 'Custom') { |
||
| 408 | $selectedTypes = $runVar['settings']['fix_crap'] ?? ''; |
||
| 409 | |||
| 410 | // Convert numeric 0 or empty values to empty string |
||
| 411 | if (empty($selectedTypes) || $selectedTypes === 0 || $selectedTypes === '0') { |
||
| 412 | return $this->disablePane($pane, 'Remove Crap', 'no crap types selected'); |
||
| 413 | } |
||
| 414 | |||
| 415 | $types = is_array($selectedTypes) ? $selectedTypes : explode(',', $selectedTypes); |
||
| 416 | |||
| 417 | // Trim whitespace and filter out empty values and '0' |
||
| 418 | $types = array_map('trim', $types); |
||
| 419 | $types = array_filter($types, fn ($type) => ! empty($type) && $type !== '0'); |
||
| 420 | |||
| 421 | // Re-index array to ensure sequential keys |
||
| 422 | $types = array_values($types); |
||
| 423 | |||
| 424 | if (empty($types)) { |
||
| 425 | return $this->disablePane($pane, 'Remove Crap', 'no crap types selected'); |
||
| 426 | } |
||
| 427 | |||
| 428 | // Get state to determine if this is first run |
||
| 429 | $stateFile = storage_path('tmux/removecrap_state.json'); |
||
| 430 | $state = $this->loadCrapState($stateFile); |
||
| 431 | $isFirstRun = $state['first_run'] ?? true; |
||
| 432 | |||
| 433 | // Determine time limit: full on first run, 4 hours otherwise |
||
| 434 | $time = $isFirstRun ? 'full' : '4'; |
||
| 435 | |||
| 436 | // Build commands for all enabled types to run sequentially |
||
| 437 | $log = $this->getLogFile('removecrap'); |
||
| 438 | $commands = []; |
||
| 439 | foreach ($types as $type) { |
||
| 440 | $commands[] = "echo \"\\nRunning removeCrapReleases for {$type}\"; nice -n{$niceness} php {$artisan} releases:remove-crap --type={$type} --time={$time} --delete 2>&1 | tee -a {$log}"; |
||
| 441 | } |
||
| 442 | |||
| 443 | // Join all commands with semicolons and add final timestamp and sleep |
||
| 444 | $allCommands = implode('; ', $commands); |
||
| 445 | $fullCommand = "{$allCommands}; date +'%Y-%m-%d %T'; sleep {$sleep}"; |
||
| 446 | |||
| 447 | // Mark that we're not on the first run anymore for next cycle |
||
| 448 | $this->saveCrapState($stateFile, [ |
||
| 449 | 'first_run' => false, |
||
| 450 | 'types' => $types, |
||
| 451 | ]); |
||
| 452 | |||
| 453 | return $this->paneManager->respawnPane($pane, $fullCommand); |
||
| 454 | } |
||
| 455 | |||
| 456 | // Default fallback - disabled |
||
| 457 | return $this->disablePane($pane, 'Remove Crap', 'invalid configuration'); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Load crap removal state |
||
| 462 | */ |
||
| 463 | protected function loadCrapState(string $file): array |
||
| 464 | { |
||
| 465 | if (! file_exists($file)) { |
||
| 466 | return ['first_run' => true]; |
||
| 467 | } |
||
| 468 | |||
| 469 | $content = file_get_contents($file); |
||
| 470 | $state = json_decode($content, true); |
||
| 471 | |||
| 472 | return $state ?: ['first_run' => true]; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Save crap removal state |
||
| 477 | */ |
||
| 478 | protected function saveCrapState(string $file, array $state): void |
||
| 479 | { |
||
| 480 | $dir = dirname($file); |
||
| 481 | if (! is_dir($dir)) { |
||
| 482 | mkdir($dir, 0755, true); |
||
| 483 | } |
||
| 484 | |||
| 485 | file_put_contents($file, json_encode($state, JSON_PRETTY_PRINT)); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Run post-process additional task |
||
| 490 | */ |
||
| 491 | protected function runPostProcessAdditional(array $runVar): bool |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Run non-Amazon post-processing (TV, Movies, Anime) |
||
| 510 | */ |
||
| 511 | protected function runNonAmazonTask(array $runVar): bool |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Run Amazon post-processing (Books, Music, Games, Console, XXX) |
||
| 547 | */ |
||
| 548 | protected function runAmazonTask(array $runVar): bool |
||
| 573 | } |
||
| 574 | } |
||
| 575 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.