1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Solr\Reindex\Jobs; |
4
|
|
|
|
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\FullTextSearch\Solr\Reindex\Handlers\SolrReindexHandler; |
9
|
|
|
use SilverStripe\FullTextSearch\Utils\Logging\SearchLogFactory; |
10
|
|
|
use stdClass; |
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
12
|
|
|
|
13
|
|
|
if (!interface_exists(QueuedJob::class)) { |
14
|
|
|
return; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base class for jobs which perform re-index |
19
|
|
|
*/ |
20
|
|
|
abstract class SolrReindexQueuedJobBase implements QueuedJob |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Flag whether this job is done |
24
|
|
|
* |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $isComplete; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* List of messages |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $messages; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Logger to use for this job |
38
|
|
|
* |
39
|
|
|
* @var LoggerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $logger; |
42
|
|
|
|
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->isComplete = false; |
46
|
|
|
$this->messages = array(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return SearchLogFactory |
51
|
|
|
*/ |
52
|
|
|
protected function getLoggerFactory() |
53
|
|
|
{ |
54
|
|
|
return Injector::inst()->get(SearchLogFactory::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets a logger for this job |
59
|
|
|
* |
60
|
|
|
* @return LoggerInterface |
61
|
|
|
*/ |
62
|
|
|
protected function getLogger() |
63
|
|
|
{ |
64
|
|
|
if ($this->logger) { |
65
|
|
|
return $this->logger; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Set logger for this job |
69
|
|
|
$this->logger = $this |
|
|
|
|
70
|
|
|
->getLoggerFactory() |
71
|
|
|
->getQueuedJobLogger($this); |
72
|
|
|
return $this->logger; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Assign custom logger for this job |
77
|
|
|
* |
78
|
|
|
* @param LoggerInterface $logger |
79
|
|
|
*/ |
80
|
|
|
public function setLogger($logger) |
81
|
|
|
{ |
82
|
|
|
$this->logger = $logger; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getJobData() |
86
|
|
|
{ |
87
|
|
|
$data = new stdClass(); |
88
|
|
|
|
89
|
|
|
// Standard fields |
90
|
|
|
$data->totalSteps = 1; |
91
|
|
|
$data->currentStep = $this->isComplete ? 0 : 1; |
92
|
|
|
$data->isComplete = $this->isComplete; |
93
|
|
|
$data->messages = $this->messages; |
94
|
|
|
|
95
|
|
|
// Custom data |
96
|
|
|
$data->jobData = new stdClass(); |
97
|
|
|
return $data; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages) |
101
|
|
|
{ |
102
|
|
|
$this->isComplete = $isComplete; |
103
|
|
|
$this->messages = $messages; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the reindex handler |
108
|
|
|
* |
109
|
|
|
* @return SolrReindexHandler |
110
|
|
|
*/ |
111
|
|
|
protected function getHandler() |
112
|
|
|
{ |
113
|
|
|
return Injector::inst()->get(SolrReindexHandler::class); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function jobFinished() |
117
|
|
|
{ |
118
|
|
|
return $this->isComplete; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function prepareForRestart() |
122
|
|
|
{ |
123
|
|
|
// NOOP |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setup() |
127
|
|
|
{ |
128
|
|
|
// NOOP |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function afterComplete() |
132
|
|
|
{ |
133
|
|
|
// NOOP |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getJobType() |
137
|
|
|
{ |
138
|
|
|
return QueuedJob::QUEUED; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getSignature() |
142
|
|
|
{ |
143
|
|
|
return sha1(get_class($this) . time() . mt_rand(0, 100000)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function addMessage($message) |
147
|
|
|
{ |
148
|
|
|
$this->messages[] = $message; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.