-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathElasticSearchAliasInit.php
More file actions
106 lines (91 loc) · 3.59 KB
/
Copy pathElasticSearchAliasInit.php
File metadata and controls
106 lines (91 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace App\Jobs;
use Illuminate\Support\Facades\Log;
use App\Http\Curl\HttpRequest;
use App\WikiDb;
class ElasticSearchAliasInit extends Job
{
private $wikiId;
private $dbName;
private $sharedPrefix;
/**
* @param int $wikiId
* @param string $dbName
*/
public function __construct( int $wikiId, string $sharedPrefix = null )
{
$this->wikiId = $wikiId;
$this->sharedPrefix = $sharedPrefix ?? getenv( 'ELASTICSEARCH_SHARED_INDEX_PREFIX' );
}
/**
* @param HttpRequest $request
*/
public function handle( HttpRequest $request )
{
Log::info( __METHOD__ . ": Updating Elasticsearch aliases for $this->wikiId" );
if ( !$this->sharedPrefix ) {
Log::error( __METHOD__ . ": Missing shared index prefix for $this->wikiId" );
$this->fail(
new \RuntimeException( "Missing shared index prefix for $this->wikiId" )
);
return;
}
Log::info( __METHOD__ . ": Using '$this->sharedPrefix' as the shared prefix for $this->wikiId" );
$this->dbName = WikiDb::where( 'wiki_id', $this->wikiId )->pluck( 'name' )->first();
if ( !$this->dbName ) {
Log::error( __METHOD__ . ": Failed to get database name for $this->wikiId" );
$this->fail(
new \RuntimeException( "Failed to get database name for $this->wikiId" )
);
return;
}
$actions = [];
foreach ( [ 'content', 'general' ] as $index ) {
$notAliasedIndex = $this->sharedPrefix . '_' . $index . '_first';
$filter = $this->dbName . '-';
$aliases = [
$this->dbName,
$this->dbName . '_' . $index,
$this->dbName . '_' . $index . '_first'
];
foreach ( $aliases as $alias ) {
array_push( $actions, [
'add' => [
'index' => $notAliasedIndex,
'alias' => $alias,
'routing' => $alias,
'filter' => [ 'prefix' => [ 'wiki' => $filter ] ]
]
] );
}
}
$request->setOptions( [
CURLOPT_URL => getenv( 'ELASTICSEARCH_SHARED_INDEX_HOST' ) . '/_aliases',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60 * 15,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [ 'Content-Type: application/json' ],
CURLOPT_POSTFIELDS => json_encode( [ 'actions' => $actions ] )
] );
$rawResponse = $request->execute();
$error = $request->error();
$request->close();
if ( $error ) {
Log::error( __METHOD__ . ": Updating Elasticsearch aliases failed for $this->wikiId with $rawResponse" );
$this->fail(
new \RuntimeException( "cURL errored for $this->wikiId with $error" )
);
return;
}
$json = json_decode( $rawResponse, true );
if ( !isset( $json[ 'acknowledged' ] ) || $json[ 'acknowledged' ] !== true ) {
Log::error( __METHOD__ . ": Updating Elasticsearch aliases failed for $this->wikiId with $rawResponse" );
$this->fail(
new \RuntimeException( "Updating Elasticsearch aliases failed for $this->wikiId with $rawResponse" )
);
return;
}
Log::info( __METHOD__ . ": Updating Elasticsearch aliases finished for $this->wikiId" );
}
}