Passed
Push — sheepy/introspection ( 851cdd...69e16c )
by Marco
06:22
created

SolrIndexTask   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 58
Bugs 3 Features 0
Metric Value
wmc 25
eloc 81
c 58
b 3
f 0
dl 0
loc 251
rs 10
ccs 76
cts 80
cp 0.95

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setDebug() 0 5 1
A setService() 0 5 1
A __construct() 0 9 2
A indexClass() 0 22 4
A clearIndex() 0 8 2
A setLogger() 0 5 1
A run() 0 26 3
A indexClassForIndex() 0 8 2
A getClasses() 0 7 2
A getLogger() 0 8 2
A doReindex() 0 17 2
A taskSetup() 0 10 3
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Tasks;
5
6
use Exception;
7
use Firesphere\SolrSearch\Factories\DocumentFactory;
8
use Firesphere\SolrSearch\Indexes\BaseIndex;
9
use Firesphere\SolrSearch\Services\SolrCoreService;
10
use GuzzleHttp\Exception\RequestException;
11
use Psr\Log\LoggerInterface;
12
use SilverStripe\Control\Director;
13
use SilverStripe\Control\HTTPRequest;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\Dev\BuildTask;
16
use SilverStripe\ORM\ArrayList;
17
use SilverStripe\ORM\DataList;
18
use SilverStripe\ORM\DataObject;
19
use SilverStripe\Versioned\Versioned;
20
21
class SolrIndexTask extends BuildTask
22
{
23
    /**
24
     * @var string
25
     */
26
    private static $segment = 'SolrIndexTask';
27
28
    /**
29
     * @var string
30
     */
31
    protected $title = 'Solr Index update';
32
33
    /**
34
     * @var string
35
     */
36
    protected $description = 'Add or update documents to an existing Solr core.';
37
38
    /**
39
     * @var bool
40
     */
41
    protected $debug = false;
42
43
    /**
44
     * @var null|LoggerInterface
45
     */
46
    protected $logger;
47
48
    /**
49
     * @var SolrCoreService
50
     */
51
    protected $service;
52
53
    /**
54
     * SolrIndexTask constructor. Sets up the document factory
55
     */
56 13
    public function __construct()
57
    {
58 13
        parent::__construct();
59
        // Only index live items.
60
        // The old FTS module also indexed Draft items. This is unnecessary
61 13
        Versioned::set_reading_mode(Versioned::DRAFT . '.' . Versioned::LIVE);
62 13
        $this->setService(Injector::inst()->get(SolrCoreService::class));
63 13
        $this->setLogger(Injector::inst()->get(LoggerInterface::class));
64 13
        $this->setDebug(Director::isDev() || Director::is_cli());
65 13
    }
66
67
    /**
68
     * @param SolrCoreService $service
69
     * @return SolrIndexTask
70
     */
71 13
    public function setService(SolrCoreService $service): SolrIndexTask
72
    {
73 13
        $this->service = $service;
74
75 13
        return $this;
76
    }
77
78
    /**
79
     * @param bool $debug
80
     * @return SolrIndexTask
81
     */
82 13
    public function setDebug(bool $debug): SolrIndexTask
83
    {
84 13
        $this->debug = $debug;
85
86 13
        return $this;
87
    }
88
89
    /**
90
     * Implement this method in the task subclass to
91
     * execute via the TaskRunner
92
     *
93
     * @param HTTPRequest $request
94
     * @return int|bool
95
     * @throws Exception
96
     * @todo defer to background because it may run out of memory
97
     */
98 12
    public function run($request)
99
    {
100 12
        $startTime = time();
101 12
        [$vars, $group, $isGroup] = $this->taskSetup($request);
102 12
        $groups = 0;
103 12
        $indexes = $this->service->getValidIndexes($request->getVar('index'));
104
105 12
        foreach ($indexes as $indexName) {
106
            /** @var BaseIndex $index */
107 12
            $index = Injector::inst()->get($indexName);
108
109 12
            $indexClasses = $index->getClasses();
110 12
            $classes = $this->getClasses($vars, $indexClasses);
111 12
            if (!count($classes)) {
112
                continue;
113
            }
114
115 12
            $vars = $this->clearIndex($vars, $indexName, $index);
116 1
117 1
            $groups = $this->indexClassForIndex($classes, $isGroup, $index, $group);
118
        }
119
        $this->getLogger()->info(
120 12
            sprintf('It took me %d seconds to do all the indexing%s', (time() - $startTime), PHP_EOL)
121 12
        );
122
123
        return $groups;
124 12
    }
125 12
126
    /**
127
     * @param HTTPRequest $request
128 12
     * @return array
129
     */
130
    protected function taskSetup($request): array
131
    {
132
        $vars = $request->getVars();
133
        $this->debug = $this->debug || isset($vars['debug']);
134
        $group = $vars['group'] ?? 0;
135 12
        $start = $vars['start'] ?? 0;
136
        $group = ($start > $group) ? $start : $group;
137 12
        $isGroup = isset($vars['group']);
138 12
139 12
        return [$vars, $group, $isGroup];
140 12
    }
141 12
142 12
    /**
143
     * @param $vars
144 12
     * @param array $classes
145
     * @return bool|array
146
     */
147
    protected function getClasses($vars, array $classes): array
148
    {
149
        if (isset($vars['class'])) {
150
            return array_intersect($classes, [$vars['class']]);
151
        }
152 12
153
        return $classes;
154 12
    }
155 1
156
    /**
157
     * @return LoggerInterface
158 11
     */
159
    public function getLogger(): LoggerInterface
160
    {
161
        // Constructor should take care of this, but it's a good safeguard
162
        if (!$this->logger) {
163
            $this->setLogger(Injector::inst()->get(LoggerInterface::class));
164 13
        }
165
166
        return $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->logger could return the type null which is incompatible with the type-hinted return Psr\Log\LoggerInterface. Consider adding an additional type-check to rule them out.
Loading history...
167 13
    }
168 1
169
    /**
170
     * @param LoggerInterface|null $logger
171 13
     * @return SolrIndexTask
172
     */
173
    public function setLogger($logger = null): SolrIndexTask
174
    {
175
        $this->logger = $logger;
176
177
        return $this;
178 13
    }
179
180 13
    /**
181
     * @param $isGroup
182 13
     * @param $class
183
     * @param BaseIndex $index
184
     * @param int $group
185
     * @return int
186
     * @throws Exception
187
     */
188
    private function indexClass($isGroup, $class, BaseIndex $index, int $group): int
189
    {
190
        $this->getLogger()->info(sprintf('Indexing %s for %s', $class, $index->getIndexName()), []);
191
192
        $batchLength = DocumentFactory::config()->get('batchLength');
193 12
        $groups = (int)ceil($class::get()->count() / $batchLength);
194
        while ($group <= $groups) { // Run from oldest to newest
195 12
            try {
196
                $this->getLogger()->info(sprintf('Indexing group %s', $group));
197 12
                $this->doReindex($group, $class, $batchLength, $index);
198 12
            } catch (RequestException $error) {
199 12
                $this->getLogger()->error($error->getResponse()->getBody()->__toString());
200
                continue;
201 12
            }
202 12
            // If it's a specific group to index, break after the first run
203
            if ($isGroup) {
204
                break;
205
            }
206
            $group++;
207
        }
208 12
209 2
        return $groups;
210
    }
211 11
212
    /**
213
     * @param int $group
214 12
     * @param string $class
215
     * @param int $batchLength
216
     * @param BaseIndex $index
217
     * @throws Exception
218
     */
219
    private function doReindex($group, $class, $batchLength, BaseIndex $index): void
220
    {
221
        // Generate filtered list of local records
222
        $baseClass = DataObject::getSchema()->baseDataClass($class);
223
        $filter = [];
224 12
        $client = $index->getClient();
225
        /** @var DataList|DataObject[] $items */
226
        $items = $baseClass::get()
227 12
            ->filter($filter)
228 12
            ->sort('ID ASC')
229 12
            ->limit($batchLength, ($group * $batchLength));
230
        $update = $client->createUpdate();
231 12
        if ($items->count()) {
232 12
            $this->service->setInDebugMode($this->debug);
233 12
            $this->service->updateIndex($index, $items, $update);
234 12
            $update->addCommit();
235 12
            $client->update($update);
236 12
        }
237 11
    }
238 11
239 11
    /**
240 11
     * @param $classes
241
     * @param $isGroup
242 12
     * @param BaseIndex $index
243
     * @param $group
244
     * @return int
245
     * @throws Exception
246
     */
247
    protected function indexClassForIndex($classes, $isGroup, BaseIndex $index, $group): int
248
    {
249
        $groups = 0;
250
        foreach ($classes as $class) {
251
            $groups = $this->indexClass($isGroup, $class, $index, $group);
252
        }
253
254
        return $groups;
255
    }
256
257
    /**
258
     * @param $vars
259
     * @param $indexName
260
     * @param BaseIndex $index
261
     * @return mixed
262
     * @throws Exception
263
     */
264
    protected function clearIndex($vars, $indexName, BaseIndex $index)
265
    {
266
        if (!empty($vars['clear'])) {
267
            $this->getLogger()->info(sprintf('Clearing index %s', $indexName));
268
            $this->service->doManipulate(ArrayList::create([]), SolrCoreService::DELETE_TYPE_ALL, $index);
269
        }
270
271
        return $vars;
272
    }
273
}
274