| Total Complexity | 57 |
| Total Lines | 353 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseRunner 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 BaseRunner, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | abstract class BaseRunner |
||
| 9 | { |
||
| 10 | protected ColorCLI $colorCli; |
||
| 11 | |||
| 12 | public function __construct(?ColorCLI $colorCli = null) |
||
| 15 | } |
||
| 16 | |||
| 17 | protected function buildDnrCommand(string $args): string |
||
| 18 | { |
||
| 19 | // Convert legacy command arguments to new artisan commands |
||
| 20 | return $this->convertSwitchToArtisan($args); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Convert legacy command format to new artisan commands. |
||
| 25 | */ |
||
| 26 | private function convertSwitchToArtisan(string $args): string |
||
| 27 | { |
||
| 28 | $parts = array_filter(explode(' ', trim($args))); |
||
| 29 | |||
| 30 | if (empty($parts)) { |
||
| 31 | return ''; |
||
| 32 | } |
||
| 33 | |||
| 34 | $command = $parts[0] ?? ''; |
||
| 35 | |||
| 36 | switch ($command) { |
||
| 37 | case 'backfill': |
||
| 38 | // backfill {group} {type} |
||
| 39 | $group = $parts[1] ?? ''; |
||
| 40 | $type = $parts[2] ?? '1'; |
||
| 41 | |||
| 42 | return PHP_BINARY.' artisan backfill:group "'.$group.'" '.$type; |
||
| 43 | |||
| 44 | case 'backfill_all_quantity': |
||
| 45 | // backfill_all_quantity {group} {quantity} |
||
| 46 | $group = $parts[1] ?? ''; |
||
| 47 | $quantity = $parts[2] ?? ''; |
||
| 48 | |||
| 49 | return PHP_BINARY.' artisan backfill:group "'.$group.'" 1 '.$quantity; |
||
| 50 | |||
| 51 | case 'backfill_all_quick': |
||
| 52 | // backfill_all_quick {group} |
||
| 53 | $group = $parts[1] ?? ''; |
||
| 54 | |||
| 55 | return PHP_BINARY.' artisan backfill:group "'.$group.'" 1 10000'; |
||
| 56 | |||
| 57 | case 'get_range': |
||
| 58 | // get_range {mode} {group} {first} {last} {threads} |
||
| 59 | $mode = $parts[1] ?? ''; |
||
| 60 | $group = $parts[2] ?? ''; |
||
| 61 | $first = $parts[3] ?? '0'; |
||
| 62 | $last = $parts[4] ?? '0'; |
||
| 63 | |||
| 64 | return PHP_BINARY.' artisan articles:get-range "'.$mode.'" "'.$group.'" '.$first.' '.$last; |
||
| 65 | |||
| 66 | case 'part_repair': |
||
| 67 | // part_repair {group} |
||
| 68 | $group = $parts[1] ?? ''; |
||
| 69 | |||
| 70 | return PHP_BINARY.' artisan binaries:part-repair "'.$group.'"'; |
||
| 71 | |||
| 72 | case 'releases': |
||
| 73 | // releases {groupId} |
||
| 74 | $groupId = $parts[1] ?? ''; |
||
| 75 | |||
| 76 | return PHP_BINARY.' artisan releases:process '.($groupId !== '' ? $groupId : ''); |
||
| 77 | |||
| 78 | case 'update_group_headers': |
||
| 79 | // update_group_headers {group} |
||
| 80 | $group = $parts[1] ?? ''; |
||
| 81 | |||
| 82 | return PHP_BINARY.' artisan group:update-headers "'.$group.'"'; |
||
| 83 | |||
| 84 | case 'update_per_group': |
||
| 85 | // update_per_group {groupId} |
||
| 86 | $groupId = $parts[1] ?? ''; |
||
| 87 | |||
| 88 | return PHP_BINARY.' artisan group:update-all '.$groupId; |
||
| 89 | |||
| 90 | case 'pp_additional': |
||
| 91 | // pp_additional {guid} |
||
| 92 | $guid = $parts[1] ?? ''; |
||
| 93 | |||
| 94 | return PHP_BINARY.' artisan postprocess:guid additional '.$guid; |
||
| 95 | |||
| 96 | case 'pp_nfo': |
||
| 97 | // pp_nfo {guid} |
||
| 98 | $guid = $parts[1] ?? ''; |
||
| 99 | |||
| 100 | return PHP_BINARY.' artisan postprocess:guid nfo '.$guid; |
||
| 101 | |||
| 102 | case 'pp_movie': |
||
| 103 | // pp_movie {guid} {renamed} |
||
| 104 | $guid = $parts[1] ?? ''; |
||
| 105 | $renamed = $parts[2] ?? ''; |
||
| 106 | |||
| 107 | return PHP_BINARY.' artisan postprocess:guid movie '.$guid.($renamed !== '' ? ' '.$renamed : ''); |
||
| 108 | |||
| 109 | case 'pp_tv': |
||
| 110 | // pp_tv {guid} {renamed} |
||
| 111 | $guid = $parts[1] ?? ''; |
||
| 112 | $renamed = $parts[2] ?? ''; |
||
| 113 | |||
| 114 | return PHP_BINARY.' artisan postprocess:guid tv '.$guid.($renamed !== '' ? ' '.$renamed : ''); |
||
| 115 | |||
| 116 | default: |
||
| 117 | // Log unrecognized command and return empty string |
||
| 118 | if (config('app.debug')) { |
||
| 119 | \Log::warning('Unrecognized multiprocessing command: '.$args); |
||
| 120 | } |
||
| 121 | |||
| 122 | return ''; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Public wrapper for buildDnrCommand (used by ForkingService). |
||
| 128 | */ |
||
| 129 | public function buildDnrCommandPublic(string $args): string |
||
| 130 | { |
||
| 131 | return $this->buildDnrCommand($args); |
||
| 132 | } |
||
| 133 | |||
| 134 | protected function executeCommand(string $command): string |
||
| 135 | { |
||
| 136 | $process = Process::fromShellCommandline($command); |
||
| 137 | $process->setTimeout(1800); |
||
| 138 | $process->run(function ($type, $buffer) { |
||
| 139 | if ($type === Process::ERR) { |
||
| 140 | echo $buffer; |
||
| 141 | } |
||
| 142 | }); |
||
| 143 | |||
| 144 | return $process->getOutput(); |
||
| 145 | } |
||
| 146 | |||
| 147 | protected function headerStart(string $workType, int $count, int $maxProcesses): void |
||
| 148 | { |
||
| 149 | if (config('nntmux.echocli')) { |
||
| 150 | $this->colorCli->header( |
||
| 151 | 'Multi-processing started at '.now()->toRfc2822String().' for '.$workType.' with '.$count. |
||
| 152 | ' job(s) to do using a max of '.max(1, $maxProcesses).' child process(es).' |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | protected function headerNone(): void |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Run multiple commands in parallel using Symfony Process with configurable timeout. |
||
| 166 | * This replaces Laravel Concurrency::run() which has a fixed 60-second timeout. |
||
| 167 | * |
||
| 168 | * @param array<string|int, callable> $tasks Array of callables keyed by identifier |
||
| 169 | * @param int $maxProcesses Maximum concurrent processes |
||
| 170 | * @param int|null $timeout Timeout in seconds (null = use config default) |
||
| 171 | * @return array<string|int, mixed> Results keyed by the same identifiers as $tasks |
||
| 172 | */ |
||
| 173 | protected function runParallelProcesses(array $tasks, int $maxProcesses, ?int $timeout = null): array |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Run multiple commands in parallel with real process forking and configurable timeout. |
||
| 245 | * |
||
| 246 | * @param array<string|int, string> $commands Array of shell commands keyed by identifier |
||
| 247 | * @param int $maxProcesses Maximum concurrent processes |
||
| 248 | * @param int|null $timeout Timeout in seconds (null = use config default) |
||
| 249 | * @return array<string|int, string> Command outputs keyed by the same identifiers |
||
| 250 | */ |
||
| 251 | protected function runParallelCommands(array $commands, int $maxProcesses, ?int $timeout = null): array |
||
| 252 | { |
||
| 253 | $maxProcesses = max(1, $maxProcesses); |
||
| 254 | $timeout = $timeout ?? (int) config('nntmux.multiprocessing_max_child_time', 1800); |
||
| 255 | $results = []; |
||
| 256 | $running = []; |
||
| 257 | $queue = $commands; |
||
| 258 | |||
| 259 | $startNext = function () use (&$queue, &$running, $timeout) { |
||
| 260 | if (empty($queue)) { |
||
| 261 | return; |
||
| 262 | } |
||
| 263 | $key = array_key_first($queue); |
||
| 264 | $cmd = $queue[$key]; |
||
| 265 | unset($queue[$key]); |
||
| 266 | |||
| 267 | $proc = Process::fromShellCommandline($cmd); |
||
| 268 | $proc->setTimeout($timeout); |
||
| 269 | $proc->start(); |
||
| 270 | $running[$key] = $proc; |
||
| 271 | }; |
||
| 272 | |||
| 273 | // Prime initial processes |
||
| 274 | for ($i = 0; $i < $maxProcesses && !empty($queue); $i++) { |
||
| 275 | $startNext(); |
||
| 276 | } |
||
| 277 | |||
| 278 | // Event loop |
||
| 279 | while (!empty($running)) { |
||
| 280 | foreach ($running as $key => $proc) { |
||
| 281 | if (!$proc->isRunning()) { |
||
| 282 | $results[$key] = $proc->getOutput(); |
||
| 283 | // Output errors if any |
||
| 284 | $err = $proc->getErrorOutput(); |
||
| 285 | if ($err !== '') { |
||
| 286 | echo $err; |
||
| 287 | } |
||
| 288 | unset($running[$key]); |
||
| 289 | // Start next from queue if available |
||
| 290 | if (!empty($queue)) { |
||
| 291 | $startNext(); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | usleep(50000); // 50ms |
||
| 296 | } |
||
| 297 | |||
| 298 | return $results; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Run multiple shell commands concurrently and stream their output in real-time. |
||
| 303 | * Uses Symfony Process start() with a small event loop to enforce max concurrency. |
||
| 304 | */ |
||
| 305 | protected function runStreamingCommands(array $commands, int $maxProcesses, string $desc): void |
||
| 361 | } |
||
| 362 | } |
||
| 365 |
This check looks for imports that have been defined, but are not used in the scope.