SolrReindexQueuedHandler::runGroup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 6
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr\Reindex\Handlers;
4
5
use Psr\Log\LoggerInterface;
6
use SilverStripe\FullTextSearch\Solr\SolrIndex;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\DB;
9
use SilverStripe\Core\Convert;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\FullTextSearch\Solr\Reindex\Jobs\SolrReindexQueuedJob;
12
use SilverStripe\FullTextSearch\Solr\Reindex\Jobs\SolrReindexGroupQueuedJob;
13
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateCommitJobProcessor;
14
use Symbiote\QueuedJobs\Services\QueuedJob;
0 ignored issues
show
Bug introduced by
The type Symbiote\QueuedJobs\Services\QueuedJob was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symbiote\QueuedJobs\Services\QueuedJobService;
0 ignored issues
show
Bug introduced by
The type Symbiote\QueuedJobs\Services\QueuedJobService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
0 ignored issues
show
Bug introduced by
The type Symbiote\QueuedJobs\Data...cts\QueuedJobDescriptor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
if (!interface_exists(QueuedJob::class)) {
19
    return;
20
}
21
22
/**
23
 * Represents a queued task to start the reindex job
24
 */
25
class SolrReindexQueuedHandler extends SolrReindexBase
26
{
27
    /**
28
     * @return QueuedJobService
29
     */
30
    protected function getQueuedJobService()
31
    {
32
        return singleton(QueuedJobService::class);
33
    }
34
35
    /**
36
     * Cancel any cancellable jobs
37
     *
38
     * @param string $type Type of job to cancel
39
     * @return int Number of jobs cleared
40
     */
41
    protected function cancelExistingJobs($type)
42
    {
43
        $clearable = array(
44
            // Paused jobs need to be discarded
45
            QueuedJob::STATUS_PAUSED,
46
47
            // These types would be automatically started
48
            QueuedJob::STATUS_NEW,
49
            QueuedJob::STATUS_WAIT,
50
51
            // Cancel any in-progress job
52
            QueuedJob::STATUS_INIT,
53
            QueuedJob::STATUS_RUN
54
        );
55
        DB::query(sprintf(
56
            'UPDATE "%s" '
57
                . ' SET "JobStatus" = \'%s\''
58
                . ' WHERE "JobStatus" IN (\'%s\')'
59
                . ' AND "Implementation" = \'%s\'',
60
            Convert::raw2sql(DataObject::getSchema()->tableName(QueuedJobDescriptor::class)),
61
            Convert::raw2sql(QueuedJob::STATUS_CANCELLED),
0 ignored issues
show
Bug introduced by
It seems like SilverStripe\Core\Conver...dJob::STATUS_CANCELLED) can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            /** @scrutinizer ignore-type */ Convert::raw2sql(QueuedJob::STATUS_CANCELLED),
Loading history...
62
            implode("','", Convert::raw2sql($clearable)),
63
            Convert::raw2sql($type)
64
        ));
65
        return DB::affected_rows();
66
    }
67
68
    public function triggerReindex(LoggerInterface $logger, $batchSize, $taskName, $classes = null)
69
    {
70
        // Cancel existing jobs
71
        $queues = $this->cancelExistingJobs(SolrReindexQueuedJob::class);
72
        $groups = $this->cancelExistingJobs(SolrReindexGroupQueuedJob::class);
73
        $logger->info("Cancelled {$queues} re-index tasks and {$groups} re-index groups");
74
75
        // Although this class is used as a service (singleton) it may also be instantiated
76
        // as a queuedjob
77
        $job = Injector::inst()->create(SolrReindexQueuedJob::class, $batchSize, $taskName, $classes);
78
        $this
79
            ->getQueuedJobService()
80
            ->queueJob($job);
81
82
        $title = $job->getTitle();
83
        $logger->info("Queued {$title}");
84
    }
85
86
    protected function processGroup(
87
        LoggerInterface $logger,
88
        SolrIndex $indexInstance,
89
        $state,
90
        $class,
91
        $groups,
92
        $group,
93
        $taskName
94
    ) {
95
        // Trigger another job for this group
96
        $job = Injector::inst()->create(
97
            SolrReindexGroupQueuedJob::class,
98
            get_class($indexInstance),
99
            $state,
100
            $class,
101
            $groups,
102
            $group
103
        );
104
        $this
105
            ->getQueuedJobService()
106
            ->queueJob($job);
107
108
        $title = $job->getTitle();
109
        $logger->info("Queued {$title}");
110
    }
111
112
    public function runGroup(
113
        LoggerInterface $logger,
114
        SolrIndex $indexInstance,
115
        $state,
116
        $class,
117
        $groups,
118
        $group
119
    ) {
120
        parent::runGroup($logger, $indexInstance, $state, $class, $groups, $group);
121
122
        // After any changes have been made, mark all indexes as dirty for commit
123
        // see http://stackoverflow.com/questions/7512945/how-to-fix-exceeded-limit-of-maxwarmingsearchers
124
        $logger->info("Queuing commit on all changes");
125
        SearchUpdateCommitJobProcessor::queue();
126
    }
127
}
128