1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Solr\Reindex\Jobs; |
4
|
|
|
|
5
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
6
|
|
|
|
7
|
|
|
if (!interface_exists(QueuedJob::class)) { |
8
|
|
|
return; |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Queuedjob to re-index a small group within an index. |
13
|
|
|
* |
14
|
|
|
* This job is optimised for efficient full re-indexing of an index via Solr_Reindex. |
15
|
|
|
* |
16
|
|
|
* Operates similarly to {@see SearchUpdateQueuedJobProcessor} but can not work with an arbitrary |
17
|
|
|
* list of IDs. Instead groups are segmented by ID. Additionally, this task does incremental |
18
|
|
|
* deletions of records. |
19
|
|
|
*/ |
20
|
|
|
class SolrReindexGroupQueuedJob extends SolrReindexQueuedJobBase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Name of index to reindex |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $indexName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Variant state that this group belongs to |
31
|
|
|
* |
32
|
|
|
* @var type |
33
|
|
|
*/ |
34
|
|
|
protected $state; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Single class name to index |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $class; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Total number of groups |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $groups; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Group index |
52
|
|
|
* |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
protected $group; |
56
|
|
|
|
57
|
|
|
public function __construct($indexName = null, $state = null, $class = null, $groups = null, $group = null) |
58
|
|
|
{ |
59
|
|
|
parent::__construct(); |
60
|
|
|
$this->indexName = $indexName; |
61
|
|
|
$this->state = $state; |
62
|
|
|
$this->class = $class; |
63
|
|
|
$this->groups = $groups; |
64
|
|
|
$this->group = $group; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getJobData() |
68
|
|
|
{ |
69
|
|
|
$data = parent::getJobData(); |
70
|
|
|
|
71
|
|
|
// Custom data |
72
|
|
|
$data->jobData->indexName = $this->indexName; |
73
|
|
|
$data->jobData->state = $this->state; |
74
|
|
|
$data->jobData->class = $this->class; |
75
|
|
|
$data->jobData->groups = $this->groups; |
76
|
|
|
$data->jobData->group = $this->group; |
77
|
|
|
|
78
|
|
|
return $data; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages) |
82
|
|
|
{ |
83
|
|
|
parent::setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages); |
84
|
|
|
|
85
|
|
|
// Custom data |
86
|
|
|
$this->indexName = $jobData->indexName; |
87
|
|
|
$this->state = $jobData->state; |
88
|
|
|
$this->class = $jobData->class; |
89
|
|
|
$this->groups = $jobData->groups; |
90
|
|
|
$this->group = $jobData->group; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getTitle() |
94
|
|
|
{ |
95
|
|
|
return sprintf( |
96
|
|
|
'Solr Reindex Group (%d/%d) of %s in %s', |
97
|
|
|
($this->group+1), |
98
|
|
|
$this->groups, |
99
|
|
|
$this->class, |
100
|
|
|
json_encode($this->state) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function process() |
105
|
|
|
{ |
106
|
|
|
$logger = $this->getLogger(); |
107
|
|
|
if ($this->jobFinished()) { |
108
|
|
|
$logger->notice("reindex group already complete"); |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Get instance of index |
113
|
|
|
$indexInstance = singleton($this->indexName); |
114
|
|
|
|
115
|
|
|
// Send back to processor |
116
|
|
|
$logger->info("Beginning reindex group"); |
117
|
|
|
$this |
118
|
|
|
->getHandler() |
119
|
|
|
->runGroup($logger, $indexInstance, $this->state, $this->class, $this->groups, $this->group); |
120
|
|
|
$logger->info("Completed reindex group"); |
121
|
|
|
$this->isComplete = true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.