1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Tasks; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Firesphere\SolrSearch\Factories\DocumentFactory; |
8
|
|
|
use Firesphere\SolrSearch\Helpers\SolrLogger; |
9
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
10
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
11
|
|
|
use Firesphere\SolrSearch\States\SiteState; |
12
|
|
|
use Firesphere\SolrSearch\Traits\LoggerTrait; |
13
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use ReflectionException; |
16
|
|
|
use SilverStripe\Control\Director; |
17
|
|
|
use SilverStripe\Control\HTTPRequest; |
18
|
|
|
use SilverStripe\Core\Injector\Injector; |
19
|
|
|
use SilverStripe\Dev\BuildTask; |
20
|
|
|
use SilverStripe\Dev\Debug; |
21
|
|
|
use SilverStripe\ORM\ArrayList; |
22
|
|
|
use SilverStripe\ORM\DataList; |
23
|
|
|
use SilverStripe\ORM\DataObject; |
24
|
|
|
use SilverStripe\ORM\DB; |
25
|
|
|
use SilverStripe\ORM\ValidationException; |
26
|
|
|
use SilverStripe\Versioned\Versioned; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class SolrIndexTask |
30
|
|
|
* |
31
|
|
|
* @description Index items to Solr through a tasks |
32
|
|
|
* @package Firesphere\SolrSearch\Tasks |
33
|
|
|
*/ |
34
|
|
|
class SolrIndexTask extends BuildTask |
35
|
|
|
{ |
36
|
|
|
use LoggerTrait; |
37
|
|
|
/** |
38
|
|
|
* URLSegment of this task |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private static $segment = 'SolrIndexTask'; |
43
|
|
|
/** |
44
|
|
|
* Store the current states for all instances of SiteState |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
public $currentStates; |
49
|
|
|
/** |
50
|
|
|
* My name |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $title = 'Solr Index update'; |
55
|
|
|
/** |
56
|
|
|
* What do I do? |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $description = 'Add or update documents to an existing Solr core.'; |
61
|
|
|
/** |
62
|
|
|
* Debug mode enabled, default false |
63
|
|
|
* |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
protected $debug = false; |
67
|
|
|
/** |
68
|
|
|
* Singleton of {@link SolrCoreService} |
69
|
|
|
* |
70
|
|
|
* @var SolrCoreService |
71
|
|
|
*/ |
72
|
|
|
protected $service; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* SolrIndexTask constructor. Sets up the document factory |
76
|
|
|
* |
77
|
14 |
|
* @throws ReflectionException |
78
|
|
|
*/ |
79
|
14 |
|
public function __construct() |
80
|
|
|
{ |
81
|
|
|
parent::__construct(); |
82
|
14 |
|
// Only index live items. |
83
|
14 |
|
// The old FTS module also indexed Draft items. This is unnecessary |
84
|
14 |
|
Versioned::set_reading_mode(Versioned::DEFAULT_MODE); |
85
|
14 |
|
// If versioned is needed, a separate Versioned Search module is required |
86
|
14 |
|
$this->setService(Injector::inst()->get(SolrCoreService::class)); |
87
|
14 |
|
$this->setLogger(Injector::inst()->get(LoggerInterface::class)); |
88
|
14 |
|
$this->setDebug(Director::isDev() || Director::is_cli()); |
89
|
|
|
$currentStates = SiteState::currentStates(); |
90
|
|
|
SiteState::setDefaultStates($currentStates); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the {@link SolrCoreService} |
95
|
|
|
* |
96
|
14 |
|
* @param SolrCoreService $service |
97
|
|
|
* @return SolrIndexTask |
98
|
14 |
|
*/ |
99
|
|
|
public function setService(SolrCoreService $service): SolrIndexTask |
100
|
14 |
|
{ |
101
|
|
|
$this->service = $service; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the debug mode |
108
|
|
|
* |
109
|
14 |
|
* @param bool $debug |
110
|
|
|
* @return SolrIndexTask |
111
|
14 |
|
*/ |
112
|
|
|
public function setDebug(bool $debug): SolrIndexTask |
113
|
14 |
|
{ |
114
|
|
|
$this->debug = $debug; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Implement this method in the task subclass to |
121
|
|
|
* execute via the TaskRunner |
122
|
|
|
* |
123
|
|
|
* @param HTTPRequest $request |
124
|
|
|
* @return int|bool |
125
|
|
|
* @throws Exception |
126
|
13 |
|
* @throws GuzzleException |
127
|
|
|
* @todo defer to background because it may run out of memory |
128
|
13 |
|
*/ |
129
|
13 |
|
public function run($request) |
130
|
13 |
|
{ |
131
|
13 |
|
$start = time(); |
132
|
|
|
$this->getLogger()->info(date('Y-m-d H:i:s')); |
133
|
13 |
|
list($vars, $group, $isGroup) = $this->taskSetup($request); |
134
|
|
|
$groups = 0; |
135
|
13 |
|
$indexes = $this->service->getValidIndexes($request->getVar('index')); |
136
|
|
|
|
137
|
13 |
|
foreach ($indexes as $indexName) { |
138
|
13 |
|
/** @var BaseIndex $index */ |
139
|
13 |
|
$index = Injector::inst()->get($indexName, false); |
140
|
10 |
|
|
141
|
|
|
$indexClasses = $index->getClasses(); |
142
|
|
|
$classes = $this->getClasses($vars, $indexClasses); |
143
|
13 |
|
if (!count($classes)) { |
144
|
|
|
continue; |
145
|
13 |
|
} |
146
|
|
|
|
147
|
13 |
|
$this->clearIndex($vars, $index); |
148
|
13 |
|
|
149
|
|
|
$groups = $this->indexClassForIndex($classes, $isGroup, $index, $group); |
150
|
|
|
} |
151
|
13 |
|
|
152
|
13 |
|
$this->getLogger()->info(date('Y-m-d H:i:s')); |
153
|
|
|
$this->getLogger()->info(sprintf('Time taken: %s minutes', (time() - $start) / 60)); |
154
|
13 |
|
|
155
|
|
|
return $groups; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set up the requirements for this task |
160
|
|
|
* |
161
|
|
|
* @param HTTPRequest $request |
162
|
|
|
* @return array |
163
|
13 |
|
*/ |
164
|
|
|
protected function taskSetup($request): array |
165
|
13 |
|
{ |
166
|
13 |
|
$vars = $request->getVars(); |
167
|
13 |
|
$this->debug = $this->debug || isset($vars['debug']); |
168
|
13 |
|
$group = $vars['group'] ?? 0; |
169
|
13 |
|
$start = $vars['start'] ?? 0; |
170
|
|
|
$group = ($start > $group) ? $start : $group; |
171
|
13 |
|
$isGroup = isset($vars['group']); |
172
|
|
|
|
173
|
|
|
return [$vars, $group, $isGroup]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* get the classes to run for this task execution |
178
|
|
|
* |
179
|
|
|
* @param $vars |
180
|
|
|
* @param array $classes |
181
|
13 |
|
* @return bool|array |
182
|
|
|
*/ |
183
|
13 |
|
protected function getClasses($vars, array $classes): array |
184
|
1 |
|
{ |
185
|
|
|
if (isset($vars['class'])) { |
186
|
|
|
return array_intersect($classes, [$vars['class']]); |
187
|
12 |
|
} |
188
|
|
|
|
189
|
|
|
return $classes; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Clear the given index if a full re-index is needed |
194
|
|
|
* |
195
|
|
|
* @param $vars |
196
|
|
|
* @param BaseIndex $index |
197
|
13 |
|
* @throws Exception |
198
|
|
|
*/ |
199
|
13 |
|
public function clearIndex($vars, BaseIndex $index) |
200
|
1 |
|
{ |
201
|
1 |
|
if (!empty($vars['clear'])) { |
202
|
|
|
$this->getLogger()->info(sprintf('Clearing index %s', $index->getIndexName())); |
203
|
13 |
|
$this->service->doManipulate(ArrayList::create([]), SolrCoreService::DELETE_TYPE_ALL, $index); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Index the classes for a specific index |
209
|
|
|
* |
210
|
|
|
* @param $classes |
211
|
|
|
* @param $isGroup |
212
|
|
|
* @param BaseIndex $index |
213
|
|
|
* @param $group |
214
|
|
|
* @return int |
215
|
13 |
|
* @throws Exception |
216
|
|
|
* @throws GuzzleException |
217
|
13 |
|
*/ |
218
|
13 |
|
protected function indexClassForIndex($classes, $isGroup, BaseIndex $index, $group): int |
219
|
13 |
|
{ |
220
|
|
|
$groups = 0; |
221
|
|
|
foreach ($classes as $class) { |
222
|
13 |
|
$groups = $this->indexClass($isGroup, $class, $index, $group); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $groups; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Index a single class for a given index. {@link static::indexClassForIndex()} |
230
|
|
|
* |
231
|
|
|
* @param bool $isGroup |
232
|
|
|
* @param string $class |
233
|
|
|
* @param BaseIndex $index |
234
|
|
|
* @param int $group |
235
|
13 |
|
* @return int |
236
|
|
|
* @throws GuzzleException |
237
|
13 |
|
* @throws ValidationException |
238
|
|
|
*/ |
239
|
|
|
private function indexClass($isGroup, $class, BaseIndex $index, int $group): int |
240
|
13 |
|
{ |
241
|
|
|
$this->getLogger()->info(sprintf('Indexing %s for %s', $class, $index->getIndexName())); |
242
|
|
|
$batchLength = DocumentFactory::config()->get('batchLength'); |
243
|
|
|
$groups = (int)ceil($class::get()->count() / $batchLength); |
244
|
|
|
$groups = $isGroup ? $group : $groups; |
245
|
13 |
|
$cores = SolrCoreService::config()->get('cores') ?: 1; |
246
|
|
|
$this->getLogger()->info(sprintf('Total groups %s', $groups)); |
247
|
|
|
do { // Run from oldest to newest |
248
|
|
|
try { |
249
|
|
|
// Temporary workaround for CircleCI |
250
|
|
|
if (function_exists('pcntl_fork')) { |
251
|
|
|
// for each core, start a grouped indexing |
252
|
|
|
$group = $this->spawnChildren($class, $index, $group, $cores, $groups); |
253
|
|
|
} else { |
254
|
|
|
$this->doReindex($group, $class, $batchLength, $index); |
255
|
|
|
} |
256
|
|
|
} catch (Exception $error) { |
257
|
13 |
|
$this->logException($index->getIndexName(), $group, $error); |
258
|
|
|
$group++; |
259
|
13 |
|
continue; |
260
|
13 |
|
} |
261
|
|
|
$group++; |
262
|
|
|
} while ($group <= $groups); |
263
|
13 |
|
|
264
|
|
|
return $groups; |
265
|
|
|
} |
266
|
13 |
|
|
267
|
13 |
|
/** |
268
|
|
|
* Reindex the given group, for each state |
269
|
|
|
* |
270
|
|
|
* @param int $group |
271
|
|
|
* @param string $class |
272
|
|
|
* @param int $batchLength |
273
|
|
|
* @param BaseIndex $index |
274
|
|
|
* @throws Exception |
275
|
|
|
*/ |
276
|
|
|
private function doReindex($group, $class, BaseIndex $index, $pcntl = false) |
277
|
13 |
|
{ |
278
|
|
|
foreach (SiteState::getStates() as $state) { |
279
|
|
|
if ($state !== 'default' && !empty($state)) { |
280
|
13 |
|
SiteState::withState($state); |
281
|
|
|
} |
282
|
13 |
|
$this->stateReindex($group, $class, $index); |
283
|
13 |
|
} |
284
|
13 |
|
|
285
|
13 |
|
SiteState::withState(SiteState::DEFAULT_STATE); |
286
|
13 |
|
$this->getLogger()->info(sprintf('Indexed group %s', $group)); |
287
|
|
|
|
288
|
13 |
|
if ($pcntl) { |
289
|
1 |
|
exit(0); |
|
|
|
|
290
|
|
|
} |
291
|
13 |
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Index a group of a class for a specific state and index |
295
|
|
|
* |
296
|
|
|
* @param $group |
297
|
|
|
* @param $class |
298
|
|
|
* @param $batchLength |
299
|
|
|
* @param BaseIndex $index |
300
|
1 |
|
* @throws Exception |
301
|
|
|
*/ |
302
|
1 |
|
private function stateReindex($group, $class, BaseIndex $index): void |
303
|
1 |
|
{ |
304
|
1 |
|
$batchLength = DocumentFactory::config()->get('batchLength'); |
305
|
|
|
// Generate filtered list of local records |
306
|
|
|
$baseClass = DataObject::getSchema()->baseDataClass($class); |
307
|
|
|
/** @var DataList|DataObject[] $items */ |
308
|
|
|
$items = DataObject::get($baseClass) |
309
|
|
|
->sort('ID ASC') |
310
|
|
|
->limit($batchLength, ($group * $batchLength)); |
311
|
|
|
if ($items->count()) { |
312
|
|
|
$this->updateIndex($index, $items); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Execute the update on the client |
318
|
|
|
* |
319
|
|
|
* @param BaseIndex $index |
320
|
|
|
* @param $items |
321
|
|
|
* @throws Exception |
322
|
|
|
*/ |
323
|
|
|
private function updateIndex(BaseIndex $index, $items): void |
324
|
|
|
{ |
325
|
|
|
$client = $index->getClient(); |
326
|
|
|
$update = $client->createUpdate(); |
327
|
|
|
$this->service->setInDebugMode($this->debug); |
328
|
|
|
$this->service->updateIndex($index, $items, $update); |
329
|
|
|
$update->addCommit(); |
330
|
|
|
$client->update($update); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Log an exception if it happens. Most are catched, these logs are for the developers |
335
|
|
|
* to identify problems and fix them. |
336
|
|
|
* |
337
|
|
|
* @param string $index |
338
|
|
|
* @param int $group |
339
|
|
|
* @param Exception $exception |
340
|
|
|
* @throws GuzzleException |
341
|
|
|
* @throws ValidationException |
342
|
|
|
*/ |
343
|
|
|
private function logException($index, int $group, Exception $exception): void |
344
|
|
|
{ |
345
|
|
|
$this->getLogger()->error($exception->getMessage()); |
346
|
|
|
$msg = sprintf( |
347
|
|
|
'Error indexing core %s on group %s,' . PHP_EOL . |
348
|
|
|
'Please log in to the CMS to find out more about Indexing errors' . PHP_EOL, |
349
|
|
|
$index, |
350
|
|
|
$group |
351
|
|
|
); |
352
|
|
|
SolrLogger::logMessage('ERROR', $msg, $index); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param string $class |
357
|
|
|
* @param BaseIndex $index |
358
|
|
|
* @param int $group |
359
|
|
|
* @param int $cores |
360
|
|
|
* @param int $groups |
361
|
|
|
* @return array |
362
|
|
|
* @throws Exception |
363
|
|
|
*/ |
364
|
|
|
private function spawnChildren($class, BaseIndex $index, int $group, int $cores, int $groups): int |
365
|
|
|
{ |
366
|
|
|
$pids = []; |
367
|
|
|
$start = $group; |
368
|
|
|
for ($i = 0; $i < $cores; $i++) { |
369
|
|
|
$start = $group + $i; |
370
|
|
|
$pid = pcntl_fork(); |
371
|
|
|
// PID needs to be pushed before anything else, for some reason |
372
|
|
|
$pids[$i] = $pid; |
373
|
|
|
if (!$pid && $start <= $groups) { |
374
|
|
|
$config = DB::getConfig(); |
375
|
|
|
DB::connect($config); |
376
|
|
|
$this->doReindex($start, $class, $index, true); |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// Wait for each child to finish |
381
|
|
|
foreach ($pids as $key => $pid) { |
382
|
|
|
if ($pid) { |
383
|
|
|
pcntl_waitpid($pid, $status); |
384
|
|
|
if ($status === 0) { |
385
|
|
|
unset($pids[$key]); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return $start; |
|
|
|
|
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.