diff --git a/app/Http/Controllers/Backend/QsController.php b/app/Http/Controllers/Backend/QsController.php index 01c38ebbd..12ec97341 100644 --- a/app/Http/Controllers/Backend/QsController.php +++ b/app/Http/Controllers/Backend/QsController.php @@ -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], @@ -29,11 +33,31 @@ public function getBatches(Request $request): Response { $oldestBatch->update(['pending_since' => Carbon::now()]); $oldestBatch->load(['wiki', 'wiki.wikiQueryserviceNamespace']); + // queryservice-updater reads from MediaWiki directly + // platform-nginx is bypassed, which does not serve suspended wikis + $oldestBatch->setAttribute( + 'mediawiki_backend_host', + $this->getMediaWikiBackendHost($mwHostResolver, $oldestBatch) + ); + return response([$oldestBatch]); }, 3); } + private function getMediaWikiBackendHost(MediaWikiHostResolver $mwHostResolver, QsBatch $batch): ?string { + try { + // Resolving by domain avoids exposing wikiDb credentials in the response + return $mwHostResolver->getBackendHostForDomain($batch->wiki->domain); + } catch (UnknownDBVersionException|UnknownWikiDomainException $e) { + Log::warning('Unable to resolve the MediaWiki backend 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 backend host + return null; + } + } + public function markBatchesDone(Request $request): Response { $batches = (array) $request->json()->all('batches'); QsBatch::whereIn('id', $batches)->increment( diff --git a/tests/Routes/QsBatch/QsControllerTest.php b/tests/Routes/QsBatch/QsControllerTest.php index 9b41e1598..bb70a564f 100644 --- a/tests/Routes/QsBatch/QsControllerTest.php +++ b/tests/Routes/QsBatch/QsControllerTest.php @@ -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; @@ -58,6 +59,30 @@ public function testGetOldestBatch(): void { $this->assertNotNull($response->json()[0]['pending_since']); } + public function testGetOldestBatchIncludesMediaWikiBackendHost(): 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_backend_host', 'mediawiki-143-app-backend.default.svc.cluster.local') + ->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_backend_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']);