Issues (186)

Processors/SearchUpdateQueuedJobProcessor.php (1 issue)

1
<?php
2
3
namespace SilverStripe\FullTextSearch\Search\Processors;
4
5
use SilverStripe\Core\Config\Config;
6
use stdClass;
7
use Symbiote\QueuedJobs\Services\QueuedJob;
8
use Symbiote\QueuedJobs\Services\QueuedJobService;
9
10
if (!interface_exists(QueuedJob::class)) {
11
    return;
12
}
13
14
class SearchUpdateQueuedJobProcessor extends SearchUpdateBatchedProcessor implements QueuedJob
15
{
16
    /**
17
     * The QueuedJob queue to use when processing updates
18
     * @config
19
     * @var string
20
     */
21
    private static $reindex_queue = QueuedJob::QUEUED;
22
23
    protected $messages = array();
24
25
    public function triggerProcessing()
26
    {
27
        parent::triggerProcessing();
28
        singleton(QueuedJobService::class)->queueJob($this);
29
    }
30
31
    public function getTitle()
32
    {
33
        return "FullTextSearch Update Job";
34
    }
35
36
    public function getSignature()
37
    {
38
        return md5(get_class($this) . time() . mt_rand(0, 100000));
39
    }
40
41
    public function getJobType()
42
    {
43
        return Config::inst()->get(__CLASS__, 'reindex_queue');
44
    }
45
46
    public function jobFinished()
47
    {
48
        return $this->currentBatch >= count($this->batches);
49
    }
50
51
    public function setup()
52
    {
53
        // NOP
54
    }
55
56
    public function prepareForRestart()
57
    {
58
        // NOP
59
    }
60
61
    public function afterComplete()
62
    {
63
        // Once indexing is complete, commit later in order to avoid solr limits
64
        // see http://stackoverflow.com/questions/7512945/how-to-fix-exceeded-limit-of-maxwarmingsearchers
65
        SearchUpdateCommitJobProcessor::queue();
66
    }
67
68
    public function getJobData()
69
    {
70
        $data = new stdClass();
71
        $data->totalSteps = count($this->batches);
72
        $data->currentStep = $this->currentBatch;
73
        $data->isComplete = $this->jobFinished();
74
        $data->messages = $this->messages;
75
76
        $data->jobData = new stdClass();
77
        $data->jobData->batches = $this->batches;
78
        $data->jobData->currentBatch = $this->currentBatch;
79
80
        return $data;
81
    }
82
83
    public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages)
84
    {
85
        $this->isComplete = $isComplete;
0 ignored issues
show
Bug Best Practice introduced by
The property isComplete does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
        $this->messages = $messages;
87
88
        $this->batches = $jobData->batches;
89
        $this->currentBatch = $jobData->currentBatch;
90
    }
91
92
    public function addMessage($message, $severity = 'INFO')
93
    {
94
        $severity = strtoupper($severity);
95
        $this->messages[] = '[' . date('Y-m-d H:i:s') . "][$severity] $message";
96
    }
97
98
    public function process()
99
    {
100
        $result = parent::process();
101
102
        if ($this->jobFinished()) {
103
            $this->addMessage("All batched updates complete. Queuing commit job");
104
        }
105
106
        return $result;
107
    }
108
}
109