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

feat: add job for marking stalled entity imports as stalled #833

Merged
merged 1 commit into from
Jul 8, 2024
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
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Jobs\UpdateWikiSiteStatsJob;
use App\Jobs\SendEmptyWikiNotificationsJob;
use App\Jobs\CreateQueryserviceBatchesJob;
use App\Jobs\FailStalledEntityImportsJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -38,6 +39,7 @@ protected function schedule(Schedule $schedule): void
$schedule->job(new PruneQueryserviceBatchesTable)->everyFifteenMinutes();
$schedule->job(new CreateQueryserviceBatchesJob)->everyMinute();
$schedule->job(new RequeuePendingQsBatchesJob)->everyFifteenMinutes();
$schedule->job(new FailStalledEntityImportsJob)->hourly();

// Sandbox
// TODO this should maybe only be run when sandbox as a whole is loaded?
Expand Down
38 changes: 38 additions & 0 deletions app/Jobs/FailStalledEntityImportsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Jobs;

use App\WikiEntityImportStatus;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\WikiEntityImport;

class FailStalledEntityImportsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle(): void
{
$deadline = Carbon::now()->subHours(24);
$now = Carbon::now();

$stalledImports = WikiEntityImport::where([
['status', '=', WikiEntityImportStatus::Pending],
['started_at', '<=', $deadline],
]);
$stalledImports->update([
'status' => WikiEntityImportStatus::Failed,
'finished_at' => $now,
]);

if ($stalledImports->count() > 0) {
Log::info(
"Marked ".$stalledImports->count()." WikiEntityImports as failed as they seem to be stalled."
);
}
}
}
71 changes: 71 additions & 0 deletions tests/Jobs/FailStalledEntityImportsJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Tests\Jobs;

use App\WikiEntityImportStatus;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Contracts\Queue\Job;
use Carbon\Carbon;
use App\Wiki;
use App\WikiEntityImport;
use App\Jobs\FailStalledEntityImportsJob;

class FailStalledEntityImportsJobTest extends TestCase
{

use RefreshDatabase;

public function setUp(): void
{
parent::setUp();
WikiEntityImport::query()->delete();
Wiki::query()->delete();
}

public function tearDown(): void
{
WikiEntityImport::query()->delete();
Wiki::query()->delete();
parent::tearDown();
}
public function testFailsEligible()
{
$wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']);
WikiEntityImport::factory()->create([
'wiki_id' => $wiki->id,
'status' => WikiEntityImportStatus::Pending,
'started_at' => Carbon::now()->subMinutes(60),
]);
WikiEntityImport::factory()->create([
'wiki_id' => $wiki->id,
'status' => WikiEntityImportStatus::Pending,
'started_at' => Carbon::now()->subDays(4),
]);
WikiEntityImport::factory()->create([
'wiki_id' => $wiki->id,
'status' => WikiEntityImportStatus::Success,
'started_at' => Carbon::now()->subDays(4),
]);

$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->never())->method('fail');

$job = new FailStalledEntityImportsJob();
$job->setJob($mockJob);
$job->handle();

$this->assertEquals(
1,
WikiEntityImport::where(['status' => WikiEntityImportStatus::Failed])->count(),
);
$this->assertEquals(
1,
WikiEntityImport::where(['status' => WikiEntityImportStatus::Pending])->count(),
);
$this->assertEquals(
1,
WikiEntityImport::where(['status' => WikiEntityImportStatus::Success])->count(),
);
}
}
Loading