Passed
Push — sheepy/introspection ( 69e16c...c6c7ca )
by Marco
05:28
created

SolrConfigureTask::getLoggerFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Tasks;
5
6
use Exception;
7
use Firesphere\SolrSearch\Indexes\BaseIndex;
8
use Firesphere\SolrSearch\Interfaces\ConfigStore;
9
use Firesphere\SolrSearch\Services\SolrCoreService;
10
use Firesphere\SolrSearch\Stores\FileConfigStore;
11
use Firesphere\SolrSearch\Stores\PostConfigStore;
12
use Firesphere\SolrSearch\Traits\LoggerTrait;
13
use GuzzleHttp\Exception\RequestException;
14
use ReflectionException;
15
use RuntimeException;
16
use SilverStripe\Control\HTTPRequest;
17
use SilverStripe\Core\Injector\Injector;
18
use SilverStripe\Dev\BuildTask;
19
20
class SolrConfigureTask extends BuildTask
21
{
22
    use LoggerTrait;
23
24
    protected static $storeModes = [
25
        'file' => FileConfigStore::class,
26
        'post' => PostConfigStore::class,
27
//        'webdav' => WebdavConfigStore::class, // @todo
28
    ];
29
    private static $segment = 'SolrConfigureTask';
30
    protected $title = 'Configure Solr cores';
31
    protected $description = 'Create or reload a Solr Core by adding or reloading a configuration.';
32
33 13
    public function __construct()
34
    {
35 13
        parent::__construct();
36 13
    }
37 13
38
    /**
39
     * Implement this method in the task subclass to
40
     * execute via the TaskRunner
41
     *
42 13
     * @param HTTPRequest $request
43
     * @return bool|Exception
44 13
     * @throws ReflectionException
45
     */
46
    public function run($request)
47
    {
48
        $this->extend('onBeforeSolrConfigureTask', $request);
49
50
        $indexes = (new SolrCoreService())->getValidIndexes();
51
52
        foreach ($indexes as $index) {
53
            try {
54
                $this->configureIndex($index);
55 12
            } catch (RequestException $error) {
56
                $exception = $error->getResponse()->getBody()->getContents();
57 12
                $this->getLogger()->error($exception);
58
                $this->getLogger()->error(sprintf('Core loading failed for %s', $index));
59 12
                // Continue to the next index
60
                continue;
61 12
            }
62
            $this->extend('onAfterConfigureIndex', $index);
63 12
        }
64
65
        $this->extend('onAfterSolrConfigureTask');
66
67
        return true;
68
    }
69
70 12
    /**
71
     * Update the index on the given store
72
     *
73 12
     * @param string $index
74
     */
75 12
    protected function configureIndex($index): void
76
    {
77
        /** @var BaseIndex $instance */
78
        $instance = Injector::inst()->get($index);
79
80
        $index = $instance->getIndexName();
81
82
        // Then tell Solr to use those config files
83 12
        /** @var SolrCoreService $service */
84
        $service = Injector::inst()->get(SolrCoreService::class);
85
86 12
        // Assuming a core that doesn't exist doesn't have uptime, as per Solr docs
87
        // And it has a start time.
88 12
        // You'd have to be pretty darn fast to hit 0 uptime and 0 starttime for an existing core!
89
        $status = $service->coreStatus($index);
90
        $configStore = $this->createConfigForIndex($instance);
91
        // Default to create
92 12
        $method = 'coreCreate';
93
        // Switch to reload if the core is loaded
94
        if ($status && ($status->getUptime() && $status->getStartTime() !== null)) {
95
            $method = 'coreReload';
96
        }
97 12
        try {
98 12
            $service->$method($index, $configStore);
99
            $this->getLogger()->info(sprintf('Core %s successfully loaded', $index));
100 12
        } catch (RequestException $error) {
101 12
            throw new RuntimeException($error);
102
        }
103 12
    }
104 12
105 12
    /**
106
     * @param BaseIndex $instance
107
     * @return ConfigStore
108 12
     */
109 12
    protected function createConfigForIndex(BaseIndex $instance): ConfigStore
110
    {
111
        $storeConfig = SolrCoreService::config()->get('store');
112
        $configStore = $this->getStore($storeConfig);
113 12
        $instance->uploadConfig($configStore);
114
115
        return $configStore;
116
    }
117
118
    /**
119 12
     * @param $storeConfig
120
     * @return ConfigStore
121 12
     */
122 12
    protected function getStore($storeConfig): ConfigStore
123 12
    {
124
        $store = static::$storeModes[$storeConfig['mode']];
125 12
        $configStore = Injector::inst()->create($store, $storeConfig);
126
127
        // Allow changing the configStore if it needs to change to a different store
128
        $this->extend('onBeforeConfig', $configStore, $storeConfig);
129
130
        return $configStore;
131
    }
132
}
133