Passed
Push — master ( 39c5e1...95276d )
by Darko
12:16
created

TmuxTaskRunner::runTvTask()   B

Complexity

Conditions 11
Paths 73

Size

Total Lines 60
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 60
rs 7.3166
cc 11
nc 73
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Services\Tmux;
4
5
use App\Models\Settings;
6
use Blacklight\ColorCLI;
7
8
/**
9
 * Service for running tasks in tmux panes
10
 */
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)
20
    {
21
        $this->sessionName = $sessionName;
22
        $this->paneManager = new TmuxPaneManager($sessionName);
23
        $this->colorCli = new ColorCLI;
24
    }
25
26
    /**
27
     * Get niceness value from settings or config with sensible default
28
     */
29
    protected function getNiceness(): int
30
    {
31
        // Try to get from settings first
32
        $niceness = Settings::settingValue('niceness');
33
34
        // If empty string or null, try config
35
        if (empty($niceness) && $niceness !== 0 && $niceness !== '0') {
36
            $niceness = config('nntmux.niceness');
37
        }
38
39
        // If still empty, use system default
40
        if (empty($niceness) && $niceness !== 0 && $niceness !== '0') {
41
            $niceness = 10; // Standard nice default
42
        }
43
44
        return (int) $niceness;
45
    }
46
47
    /**
48
     * Run a task in a specific pane
49
     */
50
    public function runTask(string $taskName, array $config): bool
51
    {
52
        $pane = $config['pane'] ?? null;
53
        $command = $config['command'] ?? null;
54
        $enabled = $config['enabled'] ?? true;
55
        $workAvailable = $config['work_available'] ?? true;
56
57
        if (! $pane || ! $command) {
58
            return false;
59
        }
60
61
        // Check if task is enabled and has work
62
        if (! $enabled) {
63
            return $this->disablePane($pane, $taskName, 'disabled in settings');
64
        }
65
66
        if (! $workAvailable) {
67
            return $this->disablePane($pane, $taskName, 'no work available');
68
        }
69
70
        // Respawn the pane with the command
71
        return $this->paneManager->respawnPane($pane, $command);
72
    }
73
74
    /**
75
     * Disable a pane with a message
76
     */
77
    protected function disablePane(string $pane, string $taskName, string $reason): bool
78
    {
79
        $color = $this->getRandomColor();
80
        $message = "echo -e \"\033[38;5;{$color}m\n{$taskName} has been disabled: {$reason}\"";
81
82
        return $this->paneManager->respawnPane($pane, $message, kill: true);
83
    }
84
85
    /**
86
     * Build a command with logging
87
     */
88
    public function buildCommand(string $baseCommand, array $options = []): string
89
    {
90
        $parts = [$baseCommand];
91
92
        // Add sleep timer at the end if specified
93
        if (isset($options['sleep'])) {
94
            $sleepCommand = $this->buildSleepCommand($options['sleep']);
95
            $parts[] = 'date +"%Y-%m-%d %T"';
96
            $parts[] = $sleepCommand;
97
        }
98
99
        // Add logging if enabled
100
        if (isset($options['log_pane'])) {
101
            $logFile = $this->getLogFile($options['log_pane']);
102
            $command = implode('; ', $parts);
103
104
            return "{$command} 2>&1 | tee -a {$logFile}";
105
        }
106
107
        return implode('; ', $parts);
108
    }
109
110
    /**
111
     * Build sleep command
112
     */
113
    protected function buildSleepCommand(int $seconds): string
114
    {
115
        $niceness = $this->getNiceness();
116
        $sleepScript = base_path('app/Services/Tmux/Scripts/showsleep.php');
117
118
        if (file_exists($sleepScript)) {
119
            return "nice -n{$niceness} php {$sleepScript} {$seconds}";
120
        }
121
122
        return "sleep {$seconds}";
123
    }
124
125
    /**
126
     * Get log file path for a pane
127
     */
128
    protected function getLogFile(string $paneName): string
129
    {
130
        $logsEnabled = (int) Settings::settingValue('write_logs') === 1;
131
132
        if (! $logsEnabled) {
133
            return '/dev/null';
134
        }
135
136
        $logDir = config('tmux.paths.logs', storage_path('logs/tmux'));
137
138
        if (! is_dir($logDir)) {
139
            mkdir($logDir, 0755, true);
140
        }
141
142
        $date = now()->format('Y_m_d');
143
144
        return "{$logDir}/{$paneName}-{$date}.log";
145
    }
146
147
    /**
148
     * Get a random color for terminal output
149
     */
150
    protected function getRandomColor(): int
151
    {
152
        $start = (int) Settings::settingValue('colors_start') ?? 0;
153
        $end = (int) Settings::settingValue('colors_end') ?? 255;
154
        $exclude = Settings::settingValue('colors_exc') ?? '';
155
156
        if (empty($exclude)) {
157
            return random_int($start, $end);
158
        }
159
160
        $exceptions = array_map('intval', explode(',', $exclude));
161
        sort($exceptions);
162
163
        $number = random_int($start, $end - count($exceptions));
164
165
        foreach ($exceptions as $exception) {
166
            if ($number >= $exception) {
167
                $number++;
168
            } else {
169
                break;
170
            }
171
        }
172
173
        return $number;
174
    }
175
176
    /**
177
     * Run the IRC scraper
178
     */
179
    public function runIRCScraper(array $config): bool
180
    {
181
        $runScraper = (int) ($config['constants']['run_ircscraper'] ?? 0);
182
        $pane = '3.0';
183
184
        if ($runScraper !== 1) {
185
            return $this->disablePane($pane, 'IRC Scraper', 'disabled in settings');
186
        }
187
188
        $niceness = $this->getNiceness();
189
        $artisan = base_path('artisan');
190
        $command = "nice -n{$niceness} php {$artisan} irc:scrape";
191
        $command = $this->buildCommand($command, ['log_pane' => 'scraper']);
192
193
        return $this->paneManager->respawnPane($pane, $command);
194
    }
195
196
    /**
197
     * Run binaries update
198
     */
199
    public function runBinariesUpdate(array $config): bool
200
    {
201
        $enabled = (int) ($config['settings']['binaries_run'] ?? 0);
202
        $killswitch = $config['killswitch']['pp'] ?? false;
203
        $pane = '0.1';
204
205
        if (! $enabled) {
206
            return $this->disablePane($pane, 'Update Binaries', 'disabled in settings');
207
        }
208
209
        if ($killswitch) {
210
            return $this->disablePane($pane, 'Update Binaries', 'postprocess kill limit exceeded');
211
        }
212
213
        $artisanCommand = match ((int) $enabled) {
214
            1 => 'multiprocessing:safe binaries',
215
            default => null,
216
        };
217
218
        if (! $artisanCommand) {
219
            return false;
220
        }
221
222
        $niceness = $this->getNiceness();
223
        $command = "nice -n{$niceness} ".PHP_BINARY." artisan {$artisanCommand}";
224
        $sleep = (int) ($config['settings']['bins_timer'] ?? 60);
225
        $command = $this->buildCommand($command, ['log_pane' => 'binaries', 'sleep' => $sleep]);
226
227
        return $this->paneManager->respawnPane($pane, $command);
228
    }
229
230
    /**
231
     * Run backfill
232
     */
233
    public function runBackfill(array $config): bool
234
    {
235
        $enabled = (int) ($config['settings']['backfill'] ?? 0);
236
        $collKillswitch = $config['killswitch']['coll'] ?? false;
237
        $ppKillswitch = $config['killswitch']['pp'] ?? false;
238
        $pane = '0.2';
239
240
        if (! $enabled) {
241
            return $this->disablePane($pane, 'Backfill', 'disabled in settings');
242
        }
243
244
        if ($collKillswitch || $ppKillswitch) {
245
            return $this->disablePane($pane, 'Backfill', 'kill limit exceeded');
246
        }
247
248
        $artisanCommand = match ((int) $enabled) {
249
            1 => 'multiprocessing:backfill',
250
            4 => 'multiprocessing:safe backfill',
251
            default => null,
252
        };
253
254
        if (! $artisanCommand) {
255
            return false;
256
        }
257
258
        // Calculate sleep time (progressive if enabled)
259
        $baseSleep = (int) ($config['settings']['back_timer'] ?? 600);
260
        $collections = (int) ($config['counts']['now']['collections_table'] ?? 0);
261
        $progressive = (int) ($config['settings']['progressive'] ?? 0);
262
263
        $sleep = ($progressive === 1 && floor($collections / 500) > $baseSleep)
264
            ? floor($collections / 500)
265
            : $baseSleep;
266
267
        $niceness = $this->getNiceness();
268
        $command = "nice -n{$niceness} ".PHP_BINARY." artisan {$artisanCommand}";
269
        $command = $this->buildCommand($command, ['log_pane' => 'backfill', 'sleep' => $sleep]);
270
271
        return $this->paneManager->respawnPane($pane, $command);
272
    }
273
274
    /**
275
     * Run releases update
276
     */
277
    public function runReleasesUpdate(array $config): bool
278
    {
279
        $enabled = (int) ($config['settings']['releases_run'] ?? 0);
280
        $pane = $config['pane'] ?? '0.3';
281
282
        if (! $enabled) {
283
            return $this->disablePane($pane, 'Update Releases', 'disabled in settings');
284
        }
285
286
        $niceness = $this->getNiceness();
287
        $command = "nice -n{$niceness} ".PHP_BINARY.' artisan multiprocessing:releases';
288
        $sleep = (int) ($config['settings']['rel_timer'] ?? 60);
289
        $command = $this->buildCommand($command, ['log_pane' => 'releases', 'sleep' => $sleep]);
290
291
        return $this->paneManager->respawnPane($pane, $command);
292
    }
293
294
    /**
295
     * Run a specific pane task based on task name
296
     *
297
     * @param  string  $taskName  The name of the task to run
298
     * @param  array  $config  Configuration for the task (target pane, etc.)
299
     * @param  array  $runVar  Runtime variables and settings
300
     * @return bool Success status
301
     */
302
    public function runPaneTask(string $taskName, array $config, array $runVar): bool
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

302
    public function runPaneTask(string $taskName, /** @scrutinizer ignore-unused */ array $config, array $runVar): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
303
    {
304
        $sequential = (int) ($runVar['constants']['sequential'] ?? 0);
305
306
        return match ($taskName) {
307
            'main' => $this->runMainTask($sequential, $runVar),
308
            'fixnames' => $this->runFixNamesTask($runVar),
309
            'removecrap' => $this->runRemoveCrapTask($runVar),
310
            'ppadditional' => $this->runPostProcessAdditional($runVar),
311
            'tv' => $this->runTvTask($runVar),
312
            'movies' => $this->runMoviesTask($runVar),
313
            'amazon' => $this->runAmazonTask($runVar),
314
            'scraper' => $this->runIRCScraper($runVar),
315
            // Legacy mapping for backward compatibility
316
            'nonamazon' => $this->runTvTask($runVar),
317
            default => false,
318
        };
319
    }
320
321
    /**
322
     * Run main task (varies by sequential mode)
323
     */
324
    protected function runMainTask(int $sequential, array $runVar): bool
325
    {
326
        return match ($sequential) {
327
            0 => $this->runMainNonSequential($runVar),
328
            1 => $this->runMainBasic($runVar),
329
            2 => $this->runMainSequential($runVar),
330
            default => false,
331
        };
332
    }
333
334
    /**
335
     * Run main non-sequential task (binaries, backfill, releases)
336
     */
337
    protected function runMainNonSequential(array $runVar): bool
338
    {
339
        // This runs in pane 0.1, 0.2, 0.3
340
        // For now, delegate to existing methods
341
        $this->runBinariesUpdate($runVar);
342
        $this->runBackfill($runVar);
343
        $this->runReleasesUpdate(array_merge($runVar, ['pane' => '0.3']));
344
345
        return true;
346
    }
347
348
    /**
349
     * Run main basic sequential task (just releases)
350
     */
351
    protected function runMainBasic(array $runVar): bool
352
    {
353
        return $this->runReleasesUpdate(array_merge($runVar, ['pane' => '0.1']));
354
    }
355
356
    /**
357
     * Run main full sequential task
358
     */
359
    protected function runMainSequential(array $runVar): bool
0 ignored issues
show
Unused Code introduced by
The parameter $runVar is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

359
    protected function runMainSequential(/** @scrutinizer ignore-unused */ array $runVar): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
360
    {
361
        // Full sequential mode - runs group:update-all for each group
362
        $pane = '0.1';
363
364
        $niceness = $this->getNiceness();
365
        $artisan = base_path('artisan');
366
        $command = "nice -n{$niceness} php {$artisan} group:update-all";
367
        $command = $this->buildCommand($command, ['log_pane' => 'sequential']);
368
369
        return $this->paneManager->respawnPane($pane, $command);
370
    }
371
372
    /**
373
     * Run fix release names task
374
     */
375
    protected function runFixNamesTask(array $runVar): bool
376
    {
377
        $enabled = (int) ($runVar['settings']['fix_names'] ?? 0);
378
        $work = (int) ($runVar['counts']['now']['processrenames'] ?? 0);
379
        $pane = '1.0';
380
381
        if ($enabled !== 1) {
382
            return $this->disablePane($pane, 'Fix Release Names', 'disabled in settings');
383
        }
384
385
        if ($work === 0) {
386
            return $this->disablePane($pane, 'Fix Release Names', 'no releases to process');
387
        }
388
389
        $artisan = base_path('artisan');
390
        $log = $this->getLogFile('fixnames');
391
392
        // Run multiple fix-names passes
393
        $commands = [];
394
        foreach ([3, 5, 7, 9, 11, 13, 15, 17, 19] as $level) {
395
            $commands[] = "php {$artisan} releases:fix-names {$level} --update --category=other --set-status --show 2>&1 | tee -a {$log}";
396
        }
397
398
        $sleep = (int) ($runVar['settings']['fix_timer'] ?? 300);
399
        $allCommands = implode('; ', $commands);
400
        $sleepCommand = $this->buildSleepCommand($sleep);
401
        $fullCommand = "{$allCommands}; date +'%Y-%m-%d %T'; {$sleepCommand}";
402
403
        return $this->paneManager->respawnPane($pane, $fullCommand);
404
    }
405
406
    /**
407
     * Run remove crap releases task
408
     */
409
    protected function runRemoveCrapTask(array $runVar): bool
410
    {
411
        $option = $runVar['settings']['fix_crap_opt'] ?? 'Disabled';
412
        $pane = '1.1';
413
414
        // Handle disabled state
415
        if ($option === 'Disabled' || $option === 0 || $option === '0') {
416
            return $this->disablePane($pane, 'Remove Crap', 'disabled in settings');
417
        }
418
419
        $niceness = $this->getNiceness();
420
        $artisan = base_path('artisan');
421
        $sleep = (int) ($runVar['settings']['crap_timer'] ?? 300);
422
423
        // Handle 'All' mode - run all types with 2 hour time limit
424
        if ($option === 'All') {
425
            $command = "nice -n{$niceness} php {$artisan} releases:remove-crap --time=2 --delete";
426
            $command = $this->buildCommand($command, ['log_pane' => 'removecrap', 'sleep' => $sleep]);
427
428
            return $this->paneManager->respawnPane($pane, $command);
429
        }
430
431
        // Handle 'Custom' mode - run all selected types sequentially
432
        if ($option === 'Custom') {
433
            $selectedTypes = $runVar['settings']['fix_crap'] ?? '';
434
435
            // Convert numeric 0 or empty values to empty string
436
            if (empty($selectedTypes) || $selectedTypes === 0 || $selectedTypes === '0') {
437
                return $this->disablePane($pane, 'Remove Crap', 'no crap types selected');
438
            }
439
440
            $types = is_array($selectedTypes) ? $selectedTypes : explode(',', $selectedTypes);
441
442
            // Trim whitespace and filter out empty values and '0'
443
            $types = array_map('trim', $types);
444
            $types = array_filter($types, fn ($type) => ! empty($type) && $type !== '0');
445
446
            // Re-index array to ensure sequential keys
447
            $types = array_values($types);
448
449
            if (empty($types)) {
450
                return $this->disablePane($pane, 'Remove Crap', 'no crap types selected');
451
            }
452
453
            // Get state to determine if this is first run
454
            $stateFile = storage_path('tmux/removecrap_state.json');
455
            $state = $this->loadCrapState($stateFile);
456
            $isFirstRun = $state['first_run'] ?? true;
457
458
            // Determine time limit: full on first run, 4 hours otherwise
459
            $time = $isFirstRun ? 'full' : '4';
0 ignored issues
show
introduced by
The condition $isFirstRun is always true.
Loading history...
460
461
            // Build commands for all enabled types to run sequentially
462
            $log = $this->getLogFile('removecrap');
463
            $commands = [];
464
            foreach ($types as $type) {
465
                $commands[] = "echo \"\nRunning removeCrapReleases for {$type}\"; nice -n{$niceness} php {$artisan} releases:remove-crap --type={$type} --time={$time} --delete 2>&1 | tee -a {$log}";
466
            }
467
468
            // Join all commands with semicolons and add final timestamp and sleep
469
            $allCommands = implode('; ', $commands);
470
            $sleepCommand = $this->buildSleepCommand($sleep);
471
            $fullCommand = "{$allCommands}; date +'%Y-%m-%d %T'; {$sleepCommand}";
472
473
            // Mark that we're not on the first run anymore for next cycle
474
            $this->saveCrapState($stateFile, [
475
                'first_run' => false,
476
                'types' => $types,
477
            ]);
478
479
            return $this->paneManager->respawnPane($pane, $fullCommand);
480
        }
481
482
        // Default fallback - disabled
483
        return $this->disablePane($pane, 'Remove Crap', 'invalid configuration');
484
    }
485
486
    /**
487
     * Load crap removal state
488
     */
489
    protected function loadCrapState(string $file): array
490
    {
491
        if (! file_exists($file)) {
492
            return ['first_run' => true];
493
        }
494
495
        $content = file_get_contents($file);
496
        $state = json_decode($content, true);
497
498
        return $state ?: ['first_run' => true];
499
    }
500
501
    /**
502
     * Save crap removal state
503
     */
504
    protected function saveCrapState(string $file, array $state): void
505
    {
506
        $dir = dirname($file);
507
        if (! is_dir($dir)) {
508
            mkdir($dir, 0755, true);
509
        }
510
511
        file_put_contents($file, json_encode($state, JSON_PRETTY_PRINT));
512
    }
513
514
    /**
515
     * Run post-process additional task
516
     */
517
    protected function runPostProcessAdditional(array $runVar): bool
518
    {
519
        $postSetting = (int) ($runVar['settings']['post'] ?? 0);
520
        $pane = '2.0';
521
522
        // Check if post processing is enabled (1 = additional, 2 = nfo, 3 = both)
523
        if ($postSetting === 0) {
524
            return $this->disablePane($pane, 'Post-process Additional', 'disabled in settings');
525
        }
526
527
        $hasWork = (int) ($runVar['counts']['now']['work'] ?? 0) > 0;
528
        $hasNfo = (int) ($runVar['counts']['now']['processnfo'] ?? 0) > 0;
529
530
        $niceness = Settings::settingValue('niceness') ?? 2;
531
        $log = $this->getLogFile('post_additional');
532
        $sleep = (int) ($runVar['settings']['post_timer'] ?? 300);
533
534
        $commands = [];
535
536
        // Build commands based on post setting value
537
        if ($postSetting === 1) {
538
            // Post = 1: Additional processing only
539
            if ($hasWork) {
540
                $commands[] = "nice -n{$niceness} ".PHP_BINARY." artisan update:postprocess additional true 2>&1 | tee -a {$log}";
541
            }
542
        } elseif ($postSetting === 2) {
543
            // Post = 2: NFO processing only
544
            if ($hasNfo) {
545
                $commands[] = "nice -n{$niceness} ".PHP_BINARY." artisan update:postprocess nfo true 2>&1 | tee -a {$log}";
546
            }
547
        } elseif ($postSetting === 3) {
548
            // Post = 3: Both additional and NFO
549
            if ($hasWork) {
550
                $commands[] = "nice -n{$niceness} ".PHP_BINARY." artisan update:postprocess additional true 2>&1 | tee -a {$log}";
551
            }
552
            if ($hasNfo) {
553
                $commands[] = "nice -n{$niceness} ".PHP_BINARY." artisan update:postprocess nfo true 2>&1 | tee -a {$log}";
554
            }
555
        }
556
557
        // If no work available, disable the pane
558
        if (empty($commands)) {
559
            $reason = match ($postSetting) {
560
                1 => 'no additional work to process',
561
                2 => 'no NFOs to process',
562
                3 => 'no additional work or NFOs to process',
563
                default => 'invalid post setting value',
564
            };
565
566
            return $this->disablePane($pane, 'Post-process Additional', $reason);
567
        }
568
569
        // Build the full command with all parts
570
        $allCommands = implode('; ', $commands);
571
        $sleepCommand = $this->buildSleepCommand($sleep);
572
        $fullCommand = "{$allCommands}; date +'%Y-%m-%d %T'; {$sleepCommand}";
573
574
        return $this->paneManager->respawnPane($pane, $fullCommand);
575
    }
576
577
    /**
578
     * Run TV/Anime post-processing
579
     */
580
    protected function runTvTask(array $runVar): bool
581
    {
582
        $enabled = (int) ($runVar['settings']['post_non'] ?? 0);
583
        $pane = '2.1';
584
585
        if ($enabled !== 1) {
586
            return $this->disablePane($pane, 'Post-process TV/Anime', 'disabled in settings');
587
        }
588
589
        $niceness = $this->getNiceness();
590
        $log = $this->getLogFile('post_tv');
591
        $artisan = PHP_BINARY.' artisan';
592
        $commands = [];
593
594
        // TV processing - Check work count before adding to queue
595
        $processTv = (int) ($runVar['settings']['processtvrage'] ?? 0);
596
        $hasTvWork = (int) ($runVar['counts']['now']['processtv'] ?? 0) > 0;
597
        if ($processTv > 0) {
598
            if ($hasTvWork) {
599
                $commands[] = "nice -n{$niceness} {$artisan} update:postprocess tv 2>&1 | tee -a {$log}";
600
            } else {
601
                // Log that TV processing was skipped due to no work
602
                if ($this->echooutput ?? true) {
0 ignored issues
show
Bug Best Practice introduced by
The property echooutput does not exist on App\Services\Tmux\TmuxTaskRunner. Did you maybe forget to declare it?
Loading history...
603
                    $this->colorCli->notice('Skipping TV processing - no work available');
604
                }
605
            }
606
        }
607
608
        // Anime processing
609
        $processAnime = (int) ($runVar['settings']['processanime'] ?? 0);
610
        $hasAnimeWork = (int) ($runVar['counts']['now']['processanime'] ?? 0) > 0;
611
        if ($processAnime > 0 && $hasAnimeWork) {
612
            $commands[] = "nice -n{$niceness} {$artisan} update:postprocess anime true 2>&1 | tee -a {$log}";
613
        }
614
615
        // If no work available for any enabled type, disable the pane
616
        if (empty($commands)) {
617
            $enabledTypes = [];
618
            if ($processTv > 0) {
619
                $enabledTypes[] = 'TV';
620
            }
621
            if ($processAnime > 0) {
622
                $enabledTypes[] = 'Anime';
623
            }
624
625
            if (empty($enabledTypes)) {
626
                return $this->disablePane($pane, 'Post-process TV/Anime', 'no types enabled (TV/Anime)');
627
            }
628
629
            $typesList = implode(', ', $enabledTypes);
630
631
            return $this->disablePane($pane, 'Post-process TV/Anime', "no work for enabled types ({$typesList})");
632
        }
633
634
        $sleep = (int) ($runVar['settings']['post_timer_non'] ?? 300);
635
        $allCommands = implode('; ', $commands);
636
        $sleepCommand = $this->buildSleepCommand($sleep);
637
        $fullCommand = "{$allCommands}; date +'%Y-%m-%d %T'; {$sleepCommand}";
638
639
        return $this->paneManager->respawnPane($pane, $fullCommand);
640
    }
641
642
    /**
643
     * Run Movies post-processing
644
     */
645
    protected function runMoviesTask(array $runVar): bool
646
    {
647
        $enabled = (int) ($runVar['settings']['post_non'] ?? 0);
648
        $pane = '2.2';
649
650
        if ($enabled !== 1) {
651
            return $this->disablePane($pane, 'Post-process Movies', 'disabled in settings');
652
        }
653
654
        $niceness = $this->getNiceness();
655
        $log = $this->getLogFile('post_movies');
656
        $artisan = PHP_BINARY.' artisan';
657
658
        // Movies processing - Uses single-process command
659
        $processMovies = (int) ($runVar['settings']['processmovies'] ?? 0);
660
        $hasMoviesWork = (int) ($runVar['counts']['now']['processmovies'] ?? 0) > 0;
661
662
        if ($processMovies === 0) {
663
            return $this->disablePane($pane, 'Post-process Movies', 'disabled in settings');
664
        }
665
666
        if (! $hasMoviesWork) {
667
            return $this->disablePane($pane, 'Post-process Movies', 'no work available');
668
        }
669
670
        $sleep = (int) ($runVar['settings']['post_timer_non'] ?? 300);
671
        $command = "nice -n{$niceness} {$artisan} update:postprocess movies true 2>&1 | tee -a {$log}";
672
        $sleepCommand = $this->buildSleepCommand($sleep);
673
        $fullCommand = "{$command}; date +'%Y-%m-%d %T'; {$sleepCommand}";
674
675
        return $this->paneManager->respawnPane($pane, $fullCommand);
676
    }
677
678
    /**
679
     * Legacy method for backward compatibility - now just calls runTvTask
680
     * @deprecated Use runTvTask() instead
681
     */
682
    protected function runNonAmazonTask(array $runVar): bool
683
    {
684
        return $this->runTvTask($runVar);
685
    }
686
687
    /**
688
     * Run Amazon post-processing (Books, Music, Games, Console, XXX)
689
     */
690
    protected function runAmazonTask(array $runVar): bool
691
    {
692
        $enabled = (int) ($runVar['settings']['post_amazon'] ?? 0);
693
        $pane = '2.3';
694
695
        if ($enabled !== 1) {
696
            return $this->disablePane($pane, 'Post-process Amazon', 'disabled in settings');
697
        }
698
699
        $hasWork = (int) ($runVar['counts']['now']['processmusic'] ?? 0) > 0
700
            || (int) ($runVar['counts']['now']['processbooks'] ?? 0) > 0
701
            || (int) ($runVar['counts']['now']['processconsole'] ?? 0) > 0
702
            || (int) ($runVar['counts']['now']['processgames'] ?? 0) > 0
703
            || (int) ($runVar['counts']['now']['processxxx'] ?? 0) > 0;
704
705
        if (! $hasWork) {
706
            return $this->disablePane($pane, 'Post-process Amazon', 'no music/books/games to process');
707
        }
708
709
        $niceness = Settings::settingValue('niceness') ?? 2;
710
        $command = "nice -n{$niceness} ".PHP_BINARY.' artisan update:postprocess amazon true';
711
        $sleep = (int) ($runVar['settings']['post_timer_amazon'] ?? 300);
712
        $command = $this->buildCommand($command, ['log_pane' => 'post_amazon', 'sleep' => $sleep]);
713
714
        return $this->paneManager->respawnPane($pane, $command);
715
    }
716
}
717