TmuxTaskRunner::runNonAmazonTask()   D
last analyzed

Complexity

Conditions 14
Paths 273

Size

Total Lines 69
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

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