Passed
Push — master ( e21d9f...a9d080 )
by Darko
11:59
created

TmuxTaskRunner   F

Complexity

Total Complexity 99

Size/Duplication

Total Lines 655
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 99
eloc 293
c 8
b 0
f 0
dl 0
loc 655
rs 2

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B runPostProcessAdditional() 0 57 10
A runIRCScraper() 0 15 2
A runTask() 0 22 5
A buildSleepCommand() 0 10 2
A buildCommand() 0 20 3
A runMainNonSequential() 0 9 1
A getRandomColor() 0 24 4
A runPaneTask() 0 13 1
C runRemoveCrapTask() 0 74 14
A disablePane() 0 6 1
A runBinariesUpdate() 0 29 4
A getLogFile() 0 17 3
A runFixNamesTask() 0 28 4
B getNiceness() 0 16 7
A runReleasesUpdate() 0 15 2
A runMainBasic() 0 3 1
A runMainSequential() 0 11 1
A loadCrapState() 0 10 3
A runMainTask() 0 7 1
A saveCrapState() 0 8 2
B runBackfill() 0 39 7
C runNonAmazonTask() 0 62 13
B runAmazonTask() 0 25 7

How to fix   Complexity   

Complex Class

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
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
            'nonamazon' => $this->runNonAmazonTask($runVar),
312
            'amazon' => $this->runAmazonTask($runVar),
313
            'scraper' => $this->runIRCScraper($runVar),
314
            default => false,
315
        };
316
    }
317
318
    /**
319
     * Run main task (varies by sequential mode)
320
     */
321
    protected function runMainTask(int $sequential, array $runVar): bool
322
    {
323
        return match ($sequential) {
324
            0 => $this->runMainNonSequential($runVar),
325
            1 => $this->runMainBasic($runVar),
326
            2 => $this->runMainSequential($runVar),
327
            default => false,
328
        };
329
    }
330
331
    /**
332
     * Run main non-sequential task (binaries, backfill, releases)
333
     */
334
    protected function runMainNonSequential(array $runVar): bool
335
    {
336
        // This runs in pane 0.1, 0.2, 0.3
337
        // For now, delegate to existing methods
338
        $this->runBinariesUpdate($runVar);
339
        $this->runBackfill($runVar);
340
        $this->runReleasesUpdate(array_merge($runVar, ['pane' => '0.3']));
341
342
        return true;
343
    }
344
345
    /**
346
     * Run main basic sequential task (just releases)
347
     */
348
    protected function runMainBasic(array $runVar): bool
349
    {
350
        return $this->runReleasesUpdate(array_merge($runVar, ['pane' => '0.1']));
351
    }
352
353
    /**
354
     * Run main full sequential task
355
     */
356
    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

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