Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ELASTICSEARCH-FIRST] Enable Elasticsearch for new wikis #147

Merged
merged 7 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions app/Http/Controllers/WikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ public function create(Request $request): \Illuminate\Http\Response
// Docs: https://www.mediawiki.org/wiki/Manual:$wgSecretKey
WikiSetting::create([
'wiki_id' => $wiki->id,
'name' => 'wgSecretKey',
'name' => WikiSetting::wgSecretKey,
'value' => Str::random(64),
]);

// Enable elasticsearch for new wikis by default
// Create the enabled elasticsearch setting
// T285541
// WikiSetting::create([
// 'wiki_id' => $wiki->id,
// 'name' => 'wwExtEnableElasticSearch',
// 'value' => true,
// ]);
WikiSetting::create([
'wiki_id' => $wiki->id,
'name' => WikiSetting::wwExtEnableElasticSearch,
'value' => true,
]);
addshore marked this conversation as resolved.
Show resolved Hide resolved

// 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->id));

$res['success'] = true;
$res['message'] = 'Success!';
Expand Down
131 changes: 96 additions & 35 deletions app/Jobs/ElasticSearchIndexInit.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,127 @@
<?php

namespace App\Jobs;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use App\WikiSetting;
use App\Http\Curl\CurlRequest;
use App\Http\Curl\HttpRequest;
use Illuminate\Support\Facades\Log;
use App\Wiki;

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

/**
* @return void
*/
public function __construct($wikiDomain)
public function __construct( int $wikiId, HttpRequest $request = null )
{
$this->wikiDomain = $wikiDomain;
$this->wikiId = $wikiId;
$this->request = $request ?? new CurlRequest();
}

/**
* The unique ID of the job.
*
* @return string
*/
public function uniqueId()
{
return strval($this->wikiId);
}

/**
* @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)
);
$wiki = Wiki::whereId( $this->wikiId )->with('settings')->with('wikiDb')->first();

// job got triggered but no wiki
if ( !$wiki ) {
$this->fail( new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiId.' was triggered but not wiki available.') );
return;
}

$setting = $wiki->settings()->where([ 'name' => WikiSetting::wwExtEnableElasticSearch, ])->first();
// job got triggered but no setting
if ( !$setting ) {
$this->fail( new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiId.' was triggered but not setting available') );
return;
}

$wikiDB = $wiki->wikiDb()->first();
// no wikiDB around
if ( !$wikiDB ) {
$this->fail( new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiId.' was triggered but not WikiDb available') );
$setting->update( [ 'value' => false ] );
return;
}

$this->request->setOptions(
[
CURLOPT_URL => getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=wbstackElasticSearchInit&format=json',
addshore marked this conversation as resolved.
Show resolved Hide resolved
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 60, // This could potentially take a bit longer
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'content-type: application/x-www-form-urlencoded',
'host: '.$wiki->domain,
]
]
);

return; //safegaurd
$rawResponse = $this->request->execute();
$err = $this->request->error();

if ($err) {
$this->fail( new \RuntimeException('curl error for '.$this->wikiId.': '.$err) );
$setting->update( [ 'value' => false ] );
return;
}

curl_close($curl);
$this->request->close();

$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
$this->fail( new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiId.'. No wbstackElasticSearchInit key in response: '.$rawResponse) );
$setting->update( [ 'value' => false ] );
return;
}

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

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

$enableElasticSearchFeature = false;

return; //safegaurd
// occurs a couple of times when newly created
if ( in_array( "\tCreating index...ok", $output ) ) {

// newly created index succeeded, turn on the wiki setting
$enableElasticSearchFeature = true;

// occurs on a successful update run
} else if ( in_array( "\t\tValidating {$wikiDB->name}_general alias...ok", $output ) ) {

// script ran and update was successful, make sure feature is enabled
$enableElasticSearchFeature = true;
} else {

Log::error(__METHOD__ . ": Job finished but didn't create or update, something is weird");
$this->fail( new \RuntimeException('wbstackElasticSearchInit call for '.$this->wikiId.' was not successful:' . $rawResponse ) );
}
toban marked this conversation as resolved.
Show resolved Hide resolved

$setting->update( [ 'value' => $enableElasticSearchFeature ] );

}
}
3 changes: 3 additions & 0 deletions app/WikiDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
* App\WikiDb.
Expand Down Expand Up @@ -33,6 +34,8 @@
*/
class WikiDb extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
Expand Down
7 changes: 7 additions & 0 deletions app/WikiSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
* App\WikiSetting.
Expand All @@ -27,6 +28,12 @@
*/
class WikiSetting extends Model
{
use HasFactory;

public const wwExtEnableElasticSearch = 'wwExtEnableElasticSearch';
public const wwExtEnableWikibaseLexeme = 'wwExtEnableWikibaseLexeme';
public const wgSecretKey = 'wgSecretKey';

/**
* The attributes that are mass assignable.
*
Expand Down
32 changes: 32 additions & 0 deletions database/factories/WikiDbFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use App\WikiDb;

class WikiDbFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = WikiDb::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->text(5),
'prefix' => $this->faker->text(5),
'user' => 'root',
'password' => 'toor',
'version' => 'seeded'
];
}
}
3 changes: 1 addition & 2 deletions database/factories/WikiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function definition()
{
return [
'sitename' => $this->faker->text(5),
'domain' => $this->faker->domainName,
'deleted_at' => $this->faker->dateTime
'domain' => $this->faker->domainName
];
}
}
26 changes: 26 additions & 0 deletions database/factories/WikiSettingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Database\Factories;

use App\WikiSetting;
use Illuminate\Database\Eloquent\Factories\Factory;

class WikiSettingFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = WikiSetting::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [];
}
}
Loading