Skip to content
Open
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
26 changes: 24 additions & 2 deletions app/Http/Controllers/Backend/QsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

use App\Http\Controllers\Controller;
use App\QsBatch;
use App\Services\MediaWikiHostResolver;
use App\Services\UnknownDBVersionException;
use App\Services\UnknownWikiDomainException;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class QsController extends Controller {
public function getBatches(Request $request): Response {
return DB::transaction(function () {
public function getBatches(Request $request, MediaWikiHostResolver $mwHostResolver): Response {
return DB::transaction(function () use ($mwHostResolver) {
$oldestBatch = QsBatch::has('wiki')
->where([
['done', '=', 0],
Expand All @@ -29,11 +33,29 @@ public function getBatches(Request $request): Response {
$oldestBatch->update(['pending_since' => Carbon::now()]);
$oldestBatch->load(['wiki', 'wiki.wikiQueryserviceNamespace']);

$oldestBatch->setAttribute(
'mediawiki_api_host',
$this->getMediaWikiApiHost($mwHostResolver, $oldestBatch)
);

return response([$oldestBatch]);
}, 3);

}

private function getMediaWikiApiHost(MediaWikiHostResolver $mwHostResolver, QsBatch $batch): ?string {
try {
// Resolving by domain avoids exposing wikiDb credentials in the response
return $mwHostResolver->getHostsForDomain($batch->wiki->domain)['api'];
} catch (UnknownDBVersionException|UnknownWikiDomainException $e) {
Log::warning('Unable to resolve the MediaWiki API host for queryservice batch ' . $batch->id . ': ' . $e->getMessage());

// Keep an unknown host from blocking the queue by returning null
// allowing the batch to proceed without a resolved API host
return null;
}
}

public function markBatchesDone(Request $request): Response {
$batches = (array) $request->json()->all('batches');
QsBatch::whereIn('id', $batches)->increment(
Expand Down
26 changes: 26 additions & 0 deletions tests/Routes/QsBatch/QsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\EventPageUpdate;
use App\QsBatch;
use App\Wiki;
use App\WikiDb;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
Expand Down Expand Up @@ -58,6 +59,31 @@ public function testGetOldestBatch(): void {
$this->assertNotNull($response->json()[0]['pending_since']);
}

public function testGetOldestBatchIncludesMediaWikiApiHost(): void {
$wiki = Wiki::factory()->create(['id' => 99, 'domain' => 'test.wikibase.cloud']);
WikiDb::factory()->create(['wiki_id' => $wiki->id, 'version' => 'mw1.43-wbs2']);
QsBatch::factory()->create(['id' => 1, 'done' => 0, 'wiki_id' => 99, 'entityIds' => 'a,b']);

$this->json('GET', $this->route . '/getBatches')
->assertStatus(200)
->assertJsonPath('0.mediawiki_api_host', 'mediawiki-143-app-api.default.svc.cluster.local')
->assertJsonMissingPath('0.mediawiki_backend_host')
->assertJsonMissingPath('0.wiki.wiki_db');
}

public function testGetOldestBatchWithUnknownMediaWikiVersionStillReturnsBatch(): void {
$wiki = Wiki::factory()->create(['id' => 99, 'domain' => 'test.wikibase.cloud']);
WikiDb::factory()->create(['wiki_id' => $wiki->id, 'version' => 'mw1.43-unmapped']);
QsBatch::factory()->create(['id' => 1, 'done' => 0, 'wiki_id' => 99, 'entityIds' => 'a,b']);

$this->json('GET', $this->route . '/getBatches')
->assertStatus(200)
->assertJsonPath('0.id', 1)
->assertJsonPath('0.mediawiki_api_host', null);

$this->assertNotNull(QsBatch::where('id', 1)->first()->pending_since);
}

public function testMarkDone(): void {
QsBatch::factory()->create(['pending_since' => Carbon::now()->subSeconds(1), 'id' => 1, 'done' => 1, 'wiki_id' => 1, 'entityIds' => 'a,b']);
QsBatch::factory()->create(['pending_since' => Carbon::now()->subSeconds(2), 'id' => 2, 'done' => 0, 'wiki_id' => 1, 'entityIds' => 'c,d']);
Expand Down