Passed
Push — hans/bugfix ( 1801a6...e64a42 )
by Simon
14:25 queued 04:18
created

SolrIndexTask::triggerIndexing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Class SolrIndexTask|Firesphere\SolrSearch\Tasks\SolrIndexTask Index Solr cores
4
 *
5
 * @package Firesphere\SolrSearch\Tasks
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrSearch\Tasks;
11
12
use Exception;
13
use Firesphere\SolrSearch\Factories\DocumentFactory;
14
use Firesphere\SolrSearch\Helpers\SolrLogger;
15
use Firesphere\SolrSearch\Indexes\BaseIndex;
16
use Firesphere\SolrSearch\Services\SolrCoreService;
17
use Firesphere\SolrSearch\States\SiteState;
18
use Firesphere\SolrSearch\Traits\LoggerTrait;
19
use Firesphere\SolrSearch\Traits\SolrIndexTrait;
20
use GuzzleHttp\Exception\GuzzleException;
21
use Psr\Log\LoggerInterface;
22
use ReflectionException;
23
use SilverStripe\Control\Controller;
24
use SilverStripe\Control\Director;
25
use SilverStripe\Control\HTTPRequest;
26
use SilverStripe\Core\Injector\Injector;
27
use SilverStripe\Dev\BuildTask;
28
use SilverStripe\ORM\ArrayList;
29
use SilverStripe\ORM\DataList;
30
use SilverStripe\ORM\DataObject;
31
use SilverStripe\ORM\DB;
32
use SilverStripe\ORM\SS_List;
33
use SilverStripe\ORM\ValidationException;
34
use SilverStripe\Versioned\Versioned;
35
36
/**
37
 * Class SolrIndexTask
38
 *
39
 * @description Index items to Solr through a tasks
40
 * @package Firesphere\SolrSearch\Tasks
41
 */
42
class SolrIndexTask extends BuildTask
43
{
44
    use LoggerTrait;
45
    use SolrIndexTrait;
46
    /**
47
     * URLSegment of this task
48
     *
49
     * @var string
50
     */
51
    private static $segment = 'SolrIndexTask';
52
    /**
53
     * Store the current states for all instances of SiteState
54
     *
55
     * @var array
56
     */
57
    public $currentStates;
58
    /**
59
     * My name
60
     *
61
     * @var string
62
     */
63
    protected $title = 'Solr Index update';
64
    /**
65
     * What do I do?
66
     *
67
     * @var string
68
     */
69
    protected $description = 'Add or update documents to an existing Solr core.';
70
71
    /**
72
     * SolrIndexTask constructor. Sets up the document factory
73
     *
74
     * @throws ReflectionException
75
     */
76 16
    public function __construct()
77
    {
78 16
        parent::__construct();
79
        // Only index live items.
80
        // The old FTS module also indexed Draft items. This is unnecessary
81
        // If versioned is needed, a separate Versioned Search module is required
82 16
        Versioned::set_reading_mode(Versioned::DEFAULT_MODE);
83 16
        $this->setService(Injector::inst()->get(SolrCoreService::class));
84 16
        $this->setLogger(Injector::inst()->get(LoggerInterface::class));
85 16
        $this->setDebug(Director::isDev() || Director::is_cli());
86 16
        $this->setBatchLength(DocumentFactory::config()->get('batchLength'));
87 16
        $cores = SolrCoreService::config()->get('cpucores') ?: 1;
88 16
        $this->setCores($cores);
89 16
        $currentStates = SiteState::currentStates();
90 16
        SiteState::setDefaultStates($currentStates);
91 16
    }
92
93
    /**
94
     * Implement this method in the task subclass to
95
     * execute via the TaskRunner
96
     *
97
     * @param HTTPRequest $request Current request
98
     * @return int|bool
99
     * @throws Exception
100
     * @throws GuzzleException
101
     */
102 13
    public function run($request)
103
    {
104 13
        $start = time();
105 13
        $this->getLogger()->info(date('Y-m-d H:i:s'));
106 13
        list($vars, $group, $isGroup) = $this->taskSetup($request);
107 13
        $groups = 0;
108 13
        $indexes = $this->service->getValidIndexes($request->getVar('index'));
109
110 13
        foreach ($indexes as $indexName) {
111
            /** @var BaseIndex $index */
112 13
            $index = Injector::inst()->get($indexName, false);
113 13
            $this->setIndex($index);
114
115 13
            $indexClasses = $this->index->getClasses();
116 13
            $classes = $this->getClasses($vars, $indexClasses);
117 13
            if (!count($classes)) {
118 10
                continue;
119
            }
120
121 13
            $this->clearIndex($vars);
122
123 13
            $groups = $this->indexClassForIndex($classes, $isGroup, $group);
124
        }
125
126 13
        $this->getLogger()->info(gmdate('Y-m-d H:i:s'));
127 13
        $time = gmdate('H:i:s', (time() - $start));
128 13
        $this->getLogger()->info(sprintf('Time taken: %s', $time));
129
130 13
        return $groups;
131
    }
132
133
    /**
134
     * Set up the requirements for this task
135
     *
136
     * @param HTTPRequest $request Current request
137
     * @return array
138
     */
139 13
    protected function taskSetup($request): array
140
    {
141 13
        $vars = $request->getVars();
142 13
        $debug = $this->isDebug() || isset($vars['debug']);
143
        // Forcefully set the debugging to whatever the outcome of the above is
144 13
        $this->setDebug($debug, true);
145 13
        $group = $vars['group'] ?? 0;
146 13
        $start = $vars['start'] ?? 0;
147 13
        $group = ($start > $group) ? $start : $group;
148 13
        $isGroup = isset($vars['group']);
149
150 13
        return [$vars, $group, $isGroup];
151
    }
152
153
    /**
154
     * get the classes to run for this task execution
155
     *
156
     * @param array $vars URL GET Parameters
157
     * @param array $classes Classes to index
158
     * @return bool|array
159
     */
160 13
    protected function getClasses($vars, array $classes): array
161
    {
162 13
        if (isset($vars['class'])) {
163 1
            return array_intersect($classes, [$vars['class']]);
164
        }
165
166 12
        return $classes;
167
    }
168
169
    /**
170
     * Clear the given index if a full re-index is needed
171
     *
172
     * @param array $vars URL GET Parameters
173
     * @throws Exception
174
     */
175 13
    public function clearIndex($vars)
176
    {
177 13
        if (!empty($vars['clear'])) {
178 1
            $this->getLogger()->info(sprintf('Clearing index %s', $this->index->getIndexName()));
179 1
            $this->service->doManipulate(ArrayList::create([]), SolrCoreService::DELETE_TYPE_ALL, $this->index);
180
        }
181 13
    }
182
183
    /**
184
     * Index the classes for a specific index
185
     *
186
     * @param array $classes Classes that need indexing
187
     * @param bool $isGroup Indexing a specific group?
188
     * @param int $group Group to index
189
     * @return int
190
     * @throws Exception
191
     * @throws GuzzleException
192
     */
193 13
    protected function indexClassForIndex($classes, $isGroup, $group): int
194
    {
195 13
        $groups = 0;
196 13
        foreach ($classes as $class) {
197 13
            $groups = $this->indexClass($isGroup, $class, $group);
198
        }
199
200 13
        return $groups;
201
    }
202
203
    /**
204
     * Index a single class for a given index. {@link static::indexClassForIndex()}
205
     *
206
     * @param bool $isGroup Is a specific group indexed
207
     * @param string $class Class to index
208
     * @param int $group Group to index
209
     * @return int
210
     * @throws GuzzleException
211
     * @throws ValidationException
212
     */
213 13
    private function indexClass($isGroup, $class, int $group): int
214
    {
215 13
        $this->getLogger()->info(sprintf('Indexing %s for %s', $class, $this->getIndex()->getIndexName()));
216 13
        list($totalGroups, $groups) = $this->getGroupSettings($isGroup, $class, $group);
217 13
        $this->getLogger()->info(sprintf('Total groups %s', $totalGroups));
218
        do { // Run from oldest to newest
219
            try {
220 13
                $group = $this->triggerIndexing($class, $group, $groups);
221
            } catch (Exception $error) {
222
                // @codeCoverageIgnoreStart
223
                $this->logException($this->index->getIndexName(), $group, $error);
224
                $group++;
225 13
                continue;
226
                // @codeCoverageIgnoreEnd
227 13
            }
228
        } while ($group <= $groups);
229
230
        return $totalGroups;
231
    }
232
233
    /**
234
     * Check the amount of groups and the total against the isGroup check.
235 13
     *
236
     * @param bool $isGroup Is it a specific group
237 13
     * @param string $class Class to check
238
     * @param int $group Current group to index
239
     * @return array
240
     */
241
    private function getGroupSettings($isGroup, $class, int $group): array
242
    {
243
        $totalGroups = (int)ceil($class::get()->count() / $this->getBatchLength());
244
        $groups = $isGroup ? ($group + $this->getCores() - 1) : $totalGroups;
245
246
        return [$totalGroups, $groups];
247
    }
248 13
249
    /**
250 13
     * Check if PCNTL is available and/or useable.
251 13
     * The unittest param is from phpunit.xml.dist, meant to bypass the exit(0) call
252
     * The pcntl parameter check is for unit tests, but PHPUnit does not support PCNTL (yet)
253 13
     *
254
     * @return bool
255
     */
256
    private function hasPCNTL()
257
    {
258
        return Director::is_cli() &&
259
            function_exists('pcntl_fork') &&
260
            (Controller::curr()->getRequest()->getVar('unittest') === 'pcntl' ||
261
                !Controller::curr()->getRequest()->getVar('unittest'));
262
    }
263 13
264
    /**
265 13
     * For each core, spawn a child process that will handle a separate group.
266 13
     * This speeds up indexing through CLI massively.
267
     *
268 13
     * @codeCoverageIgnore Can't be tested because PCNTL is not available
269
     * @param string $class Class to index
270
     * @param int $group Group to index
271
     * @param int $groups Total amount of groups
272
     * @return int Last group indexed
273
     * @throws Exception
274
     * @throws GuzzleException
275
     */
276
    private function spawnChildren($class, int $group, int $groups): int
277
    {
278
        $start = $group;
279
        $pids = [];
280
        $cores = $this->getCores();
281
        // for each core, start a grouped indexing
282
        for ($i = 0; $i < $cores; $i++) {
283
            $start = $group + $i;
284
            if ($start < $groups) {
285
                $this->runForkedChild($class, $pids, $start);
286
            }
287
        }
288
        // Wait for each child to finish
289
        // It needs to wait for them independently,
290
        // or it runs out of memory for some reason
291
        foreach ($pids as $pid) {
292
            pcntl_waitpid($pid, $status);
293
        }
294
        $commit = $this->index->getClient()->createUpdate();
295
        $commit->addCommit();
296
297
        $this->index->getClient()->update($commit);
298
299
        return $start;
300
    }
301
302
    /**
303
     * Create a fork and run the child
304
     *
305
     * @codeCoverageIgnore Can't be tested because PCNTL is not available
306
     * @param string $class Class to index
307
     * @param array $pids Array of all the child Process IDs
308
     * @param int $start Start point for the objects
309
     * @return void
310
     * @throws GuzzleException
311
     */
312
    private function runForkedChild($class, array &$pids, int $start): void
313
    {
314
        $pid = pcntl_fork();
315
        // PID needs to be pushed before anything else, for some reason
316
        $pids[] = $pid;
317
        $config = DB::getConfig();
318
        DB::connect($config);
319
        if ($pid === 0) {
320
            try {
321
                $this->runChild($class, $pid, $start);
322
            } catch (Exception $e) {
323
                exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
324
            }
325
        }
326
    }
327
328
    /**
329
     * Ren a single child index operation
330
     *
331
     * @codeCoverageIgnore Can't be tested because PCNTL is not available
332
     * @param string $class Class to index
333
     * @param int $pid PID of the child
334
     * @param int $start Position to start
335
     * @throws GuzzleException
336
     * @throws ValidationException
337
     * @throws Exception
338
     */
339
    private function runChild($class, int $pid, int $start): void
340
    {
341
        try {
342
            $this->doReindex($start, $class, $pid);
343
        } catch (Exception $error) {
344
            SolrLogger::logMessage('ERROR', $error->getMessage(), $this->index->getIndexName());
345
            $msg = sprintf(
346
                'Something went wrong while indexing %s on %s, see the logs for details',
347
                $start,
348
                $this->index->getIndexName()
349
            );
350
            throw new Exception($msg);
351
        }
352
    }
353
354
    /**
355
     * Reindex the given group, for each state
356
     *
357
     * @param int $group Group to index
358
     * @param string $class Class to index
359
     * @param bool|int $pid Are we a child process or not
360
     * @throws Exception
361
     */
362
    private function doReindex($group, $class, $pid = false)
363
    {
364
        $start = time();
365
        foreach (SiteState::getStates() as $state) {
366
            if ($state !== 'default' && !empty($state)) {
367
                SiteState::withState($state);
368
            }
369
            $this->indexStateClass($group, $class);
370 13
        }
371
372 13
        SiteState::withState(SiteState::DEFAULT_STATE);
373 13
        $end = gmdate('i:s', time() - $start);
374 13
        $this->getLogger()->info(sprintf('Indexed group %s in %s', $group, $end));
375
376
        // @codeCoverageIgnoreStart
377 13
        if ($pid !== false) {
378
            exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
379
        }
380 13
        // @codeCoverageIgnoreEnd
381 13
    }
382 13
383
    /**
384
     * Index a group of a class for a specific state and index
385
     *
386
     * @param string $group Group to index
387
     * @param string $class Class to index
388
     * @throws Exception
389 13
     */
390
    private function indexStateClass($group, $class): void
391
    {
392
        // Generate filtered list of local records
393
        $baseClass = DataObject::getSchema()->baseDataClass($class);
394
        /** @var DataList|DataObject[] $items */
395
        $items = DataObject::get($baseClass)
396
            ->sort('ID ASC')
397
            ->limit($this->getBatchLength(), ($group * $this->getBatchLength()));
398 13
        $this->updateIndex($items);
399
    }
400
401 13
    /**
402
     * Execute the update on the client
403 13
     *
404 13
     * @param SS_List $items Items to index
405 13
     * @throws Exception
406 13
     */
407 1
    private function updateIndex($items): void
408
    {
409 13
        $index = $this->getIndex();
410
        $client = $index->getClient();
411
        $update = $client->createUpdate();
412
        $this->service->setDebug($this->debug);
413
        $this->service->updateIndex($index, $items, $update);
414
        $client->update($update);
415
    }
416
417 1
    /**
418
     * Log an exception if it happens. Most are catched, these logs are for the developers
419 1
     * to identify problems and fix them.
420 1
     *
421 1
     * @codeCoverageIgnore This is actually tested through reflection
422 1
     * @param string $index Index that is currently running
423 1
     * @param int $group Group currently attempted to index
424 1
     * @param Exception $exception Exception that's been thrown
425 1
     * @throws GuzzleException
426
     * @throws ValidationException
427
     */
428
    private function logException($index, int $group, Exception $exception): void
429
    {
430
        $this->getLogger()->error($exception->getMessage());
431
        $msg = sprintf(
432
            'Error indexing core %s on group %s,' . PHP_EOL .
433
            'Please log in to the CMS to find out more about Indexing errors' . PHP_EOL,
434
            $index,
435
            $group
436
        );
437
        SolrLogger::logMessage('ERROR', $msg, $index);
438
    }
439
440
    /**
441
     * Do a reindex, as a child or not
442
     *
443
     * @param string $class Class to index
444
     * @param int $group Group to index
445
     * @param int $groups Total amount of groups
446
     * @return int
447
     * @throws GuzzleException
448
     * @throws Exception
449
     */
450
    private function triggerIndexing($class, int $group, $groups): int
451
    {
452
        if ($this->hasPCNTL()) {
453
            // @codeCoverageIgnoreStart
454
            $group = $this->spawnChildren($class, $group, $groups);
455
            // @codeCoverageIgnoreEnd
456
        } else {
457
            $this->doReindex($group, $class);
458
        }
459
        $group++;
460
461
        return $group;
462
    }
463
}
464