From 1a53be9bf1bae7ef089b43b288674d85a8b7453e Mon Sep 17 00:00:00 2001 From: Bereket teshome Date: Mon, 5 Jul 2021 14:24:11 +0200 Subject: [PATCH 1/2] Added job to create elastic indexes when a new wiki is created Bug: T285541 --- app/Http/Controllers/WikiController.php | 11 +++++ app/Jobs/ElasticSearchIndexInit.php | 66 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 app/Jobs/ElasticSearchIndexInit.php diff --git a/app/Http/Controllers/WikiController.php b/app/Http/Controllers/WikiController.php index a6fed7844..66e6cbf45 100644 --- a/app/Http/Controllers/WikiController.php +++ b/app/Http/Controllers/WikiController.php @@ -6,6 +6,7 @@ use App\Jobs\MediawikiInit; use App\Jobs\ProvisionQueryserviceNamespaceJob; use App\Jobs\ProvisionWikiDbJob; +use App\Jobs\ElasticSearchIndexInit; use App\QueryserviceNamespace; use App\Wiki; use App\WikiDb; @@ -79,6 +80,14 @@ public function create(Request $request): \Illuminate\Http\Response 'value' => Str::random(64), ]); + // Enable elasticsearch for new wikis by default + // T285541 + WikiSetting::create([ + 'wiki_id' => $wiki->id, + 'name' => 'wwExtEnableElasticSearch', + 'value' => true, + ]); + // Also track the domain forever in your domains table $wikiDomain = WikiDomain::create([ 'domain' => $wiki->domain, @@ -108,6 +117,8 @@ public function create(Request $request): \Illuminate\Http\Response $this->dispatch(new ProvisionWikiDbJob(null, null, 10)); $this->dispatch(new ProvisionQueryserviceNamespaceJob(null, 10)); + $this->dispatch(new ElasticSearchIndexInit($wiki->domain)); + $res['success'] = true; $res['message'] = 'Success!'; $res['data'] = [ diff --git a/app/Jobs/ElasticSearchIndexInit.php b/app/Jobs/ElasticSearchIndexInit.php new file mode 100644 index 000000000..9d8b6c890 --- /dev/null +++ b/app/Jobs/ElasticSearchIndexInit.php @@ -0,0 +1,66 @@ +wikiDomain = $wikiDomain; + } + + /** + * @return void + */ + public function handle() + { + $curl = curl_init(); + curl_setopt_array($curl, [ + CURLOPT_URL => getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=wbstackElasticSearchInit&format=json', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_TIMEOUT => 10, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_HTTPHEADER => [ + 'content-type: application/x-www-form-urlencoded', + 'host: '.$this->wikiDomain, + ], + ]); + + $rawResponse = curl_exec($curl); + $err = curl_error($curl); + if ($err) { + $this->fail( + new \RuntimeException('curl error for '.$this->wikiDomain.': '.$err) + ); + + return; //safegaurd + } + + curl_close($curl); + + $response = json_decode($rawResponse, true); + + if (!array_key_exists('wbstackElasticSearchInit', $response)) { + $this->fail( + new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiDomain.'. No wbstackElasticSearchInit key in response: '.$rawResponse) + ); + + return; //safegaurd + } + + if ($response['wbstackElasticSearchInit']['success'] == 0) { + $this->fail( + new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiDomain.' was not successful:'.$rawResponse) + ); + + return; //safegaurd + } + } +} From e25e60c904912ee93c831c1c774a983c0fe72189 Mon Sep 17 00:00:00 2001 From: addshore Date: Thu, 15 Jul 2021 20:36:56 +0100 Subject: [PATCH 2/2] Do not enable elastic or run the elastic job just yet --- app/Http/Controllers/WikiController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/WikiController.php b/app/Http/Controllers/WikiController.php index 66e6cbf45..ae017da6c 100644 --- a/app/Http/Controllers/WikiController.php +++ b/app/Http/Controllers/WikiController.php @@ -82,11 +82,11 @@ public function create(Request $request): \Illuminate\Http\Response // Enable elasticsearch for new wikis by default // T285541 - WikiSetting::create([ - 'wiki_id' => $wiki->id, - 'name' => 'wwExtEnableElasticSearch', - 'value' => true, - ]); +// WikiSetting::create([ +// 'wiki_id' => $wiki->id, +// 'name' => 'wwExtEnableElasticSearch', +// 'value' => true, +// ]); // Also track the domain forever in your domains table $wikiDomain = WikiDomain::create([ @@ -117,7 +117,7 @@ public function create(Request $request): \Illuminate\Http\Response $this->dispatch(new ProvisionWikiDbJob(null, null, 10)); $this->dispatch(new ProvisionQueryserviceNamespaceJob(null, 10)); - $this->dispatch(new ElasticSearchIndexInit($wiki->domain)); +// $this->dispatch(new ElasticSearchIndexInit($wiki->domain)); $res['success'] = true; $res['message'] = 'Success!';