Issues (569)

app/Console/Commands/UpdatePerGroup.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Console\Commands;
6
7
use App\Models\Settings;
8
use App\Models\UsenetGroup;
9
use Blacklight\Backfill;
10
use Blacklight\Binaries;
11
use Blacklight\Nfo;
12
use Blacklight\NNTP;
13
use Blacklight\processing\post\ProcessAdditional;
0 ignored issues
show
The type Blacklight\processing\post\ProcessAdditional was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Blacklight\processing\ProcessReleases;
0 ignored issues
show
The type Blacklight\processing\ProcessReleases was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Illuminate\Console\Command;
16
use Illuminate\Support\Facades\Log;
17
18
class UpdatePerGroup extends Command
19
{
20
    /**
21
     * The name and signature of the console command.
22
     *
23
     * @var string
24
     */
25
    protected $signature = 'group:update-all {groupId : Group ID to process}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Do a single group (update_binaries/backFill/update_releases/postprocess)';
33
34
    /**
35
     * Execute the console command.
36
     */
37
    public function handle(): int
38
    {
39
        $groupId = $this->argument('groupId');
40
41
        if (!is_numeric($groupId)) {
42
            $this->error('Group ID must be numeric.');
43
44
            return self::FAILURE;
45
        }
46
47
        try {
48
            $group = UsenetGroup::find($groupId);
49
50
            if ($group === null) {
51
                $this->error("Group not found with id {$groupId}");
52
53
                return self::FAILURE;
54
            }
55
56
            $groupMySQL = $group->toArray();
57
            $nntp = $this->getNntp();
58
            $backFill = new Backfill();
59
60
            // Update the group for new binaries
61
            $this->info("Updating binaries for group: {$groupMySQL['name']}");
62
            (new Binaries())->updateGroup($groupMySQL);
63
64
            // BackFill the group with 20k articles
65
            $this->info("Backfilling group: {$groupMySQL['name']}");
66
            $backFill->backfillAllGroups($groupMySQL['name'], 20000, 'normal');
67
68
            // Create releases
69
            $this->info("Processing releases for group: {$groupMySQL['name']}");
70
            $this->processReleases(new ProcessReleases(), (string) $groupId);
71
72
            // Post process the releases
73
            $this->info("Post-processing additional for group: {$groupMySQL['name']}");
74
            (new ProcessAdditional(['Echo' => true, 'NNTP' => $nntp]))->start($groupId);
75
76
            $this->info("Processing NFO files for group: {$groupMySQL['name']}");
77
            (new Nfo())->processNfoFiles(
78
                $nntp,
79
                $groupId,
80
                '',
81
                (bool)Settings::settingValue('lookupimdb'),
82
                (bool)Settings::settingValue('lookuptv')
83
            );
84
85
            $this->info("Completed all processing for group: {$groupMySQL['name']}");
86
87
            return self::SUCCESS;
88
        } catch (\Throwable $e) {
89
            Log::error($e->getTraceAsString());
90
            $this->error($e->getMessage());
91
92
            return self::FAILURE;
93
        }
94
    }
95
96
    /**
97
     * Create / process releases for a groupID.
98
     *
99
     * Uses the ProcessReleases DTO-based workflow for cleaner code.
100
     */
101
    private function processReleases(ProcessReleases $releases, string $groupID): void
102
    {
103
        $limit = $releases->getReleaseCreationLimit();
104
105
        $releases->processIncompleteCollections($groupID);
106
        $releases->processCollectionSizes($groupID);
107
        $releases->deleteUnwantedCollections($groupID);
108
109
        do {
110
            $result = $releases->createReleases($groupID);
111
            $nzbFilesAdded = $releases->createNZBs($groupID);
112
113
            // Continue if we processed up to the limit (more work may be available)
114
            $shouldContinue = $result->total() >= $limit || $nzbFilesAdded >= $limit;
115
        } while ($shouldContinue);
116
117
        $releases->deleteCollections($groupID);
118
    }
119
120
    /**
121
     * Get NNTP connection.
122
     *
123
     * @throws \Exception If unable to connect to usenet
124
     */
125
    private function getNntp(): NNTP
126
    {
127
        $nntp = new NNTP();
128
129
        $useAlternate = config('nntmux_nntp.use_alternate_nntp_server') === true;
130
        $connected = $useAlternate
131
            ? $nntp->doConnect(false, true)
132
            : $nntp->doConnect();
133
134
        if ($connected !== true) {
135
            throw new \RuntimeException('Unable to connect to usenet.');
136
        }
137
138
        return $nntp;
139
    }
140
}
141