Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
toban committed Aug 12, 2021
1 parent 0383f1c commit 34eada0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 346 deletions.
15 changes: 8 additions & 7 deletions app/Http/Controllers/WikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public function create(Request $request): \Illuminate\Http\Response
'value' => Str::random(64),
]);

// Enable elasticsearch for new wikis by default
// Create elasticsearch setting to be updated once ElasticSearch has been initialized
// T285541
// WikiSetting::create([
// 'wiki_id' => $wiki->id,
// 'name' => 'wwExtEnableElasticSearch',
// 'value' => true,
// ]);
WikiSetting::create([
'wiki_id' => $wiki->id,
'name' => 'wwExtEnableElasticSearch',
'value' => false,
]);

// Also track the domain forever in your domains table
$wikiDomain = WikiDomain::create([
Expand Down Expand Up @@ -117,7 +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));
// dispatch elasticsearch init job to enable the feature
$this->dispatch(new ElasticSearchIndexInit($wiki->domain, $wiki->id));

$res['success'] = true;
$res['message'] = 'Success!';
Expand Down
42 changes: 39 additions & 3 deletions app/Jobs/ElasticSearchIndexInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace App\Jobs;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use App\WikiSetting;

class ElasticSearchIndexInit extends Job implements ShouldBeUnique
{
private $wikiDomain;
private $wikiId;

/**
* @return void
*/
public function __construct($wikiDomain)
public function __construct( $wikiDomain, $wikiId )
{
$this->wikiDomain = $wikiDomain;
$this->wikiId = $wikiId;
}

/**
Expand All @@ -30,6 +33,17 @@ public function uniqueId()
*/
public function handle()
{
$setting = WikiSetting::where([ 'wiki_id' => $this->wikiId, 'name' => WikiSetting::wwExtEnableElasticSearch, ])->first();

// job got triggered but no setting in database
if ( $setting === null ) {
$this->fail(
new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiDomain.' was triggered but not setting available.')
);

return;
}

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=wbstackElasticSearchInit&format=json',
Expand Down Expand Up @@ -63,15 +77,37 @@ public function handle()
new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiDomain.'. No wbstackElasticSearchInit key in response: '.$rawResponse)
);

return; //safegaurd
return;
}

if ($response['wbstackElasticSearchInit']['success'] == 0) {
$this->fail(
new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiDomain.' was not successful:'.$rawResponse)
);

return; //safegaurd
return;
}

$output = $response['wbstackElasticSearchInit']['output'];

$newlyCreatedOutput = "\tCreating index...ok"; // occurs a couple of times when newly created
$updated = "\t\tValidating {$this->wikiDomain}_general alias...ok"; // occurs on a successful update run

if ( in_array( $newlyCreatedOutput, $output ) ) {
// newly created index succeeded, turn on the wiki setting
$setting->update(
[
'value' => true
]
);
} else if ( !in_array( $updated, $output ) ) {
// it did not even succeed in updating, disable the setting on the wiki
$setting->update(
[
'value' => false
]
);
}

}
}
104 changes: 0 additions & 104 deletions app/Observers/ElasticSearchWikiSettingsObserver.php

This file was deleted.

1 change: 0 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ class EventServiceProvider extends ServiceProvider
public function boot()
{
parent::boot();
WikiSetting::observe(ElasticSearchWikiSettingsObserver::class);
}
}
Loading

0 comments on commit 34eada0

Please sign in to comment.