Skip to content

Commit ba3865a

Browse files
authored
Ensure that report helper is called when a batch is failed (#696)
* test: assert that report helper is called when a batch is failed * fix: make sure report method is called * chore: fix indent * refactor: move logic into isolated class methods * doc: add CHANGELOG
1 parent 9c65c79 commit ba3865a

3 files changed

Lines changed: 41 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# api
22

33

4+
## 8x.28.9 - 28 November 2023
5+
- Fix error reporting on detecting failed Qs Batches
6+
47
## 8x.28.8 - 28 November 2023
58
- Chunk query for all page update events in QsBatches job
69
- Reenable QsBatches job

app/Jobs/RequeuePendingQsBatchesJob.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,40 @@ public function __construct() {
1818

1919
public function handle(): void
2020
{
21-
DB::transaction(function () {
22-
tap(QsBatch::where([
21+
$failedBatches = $this->markBatchesFailed();
22+
foreach ($failedBatches as $batchId) {
23+
report("QsBatch with ID ".$batchId."was marked as failed.");
24+
}
25+
26+
$this->requeueStalledBatches();
27+
}
28+
29+
private function markBatchesFailed(): array
30+
{
31+
return DB::transaction(function () {
32+
$failedBatches = QsBatch::where([
2333
['processing_attempts', '>=', $this->markFailedAfter],
2434
['failed', '=', false],
25-
]))
26-
->update(['failed' => true])
35+
])
36+
->select('id')
2737
->get()
28-
->each(function ($batch, $index) {
29-
report("QsBatch with ID ".$batch->id."was marked as failed.");
30-
});
31-
32-
$threshold = Carbon::now()->subtract(new \DateInterval($this->pendingTimeout));
33-
QsBatch::where([['pending_since', '<>', null], ['pending_since', '<', $threshold]])
34-
->increment(
35-
'processing_attempts', 1,
36-
['pending_since' => null, 'done' => 0]
37-
);
38+
->pluck('id')
39+
->toArray();
40+
QsBatch::whereIn('id', $failedBatches)->update(['failed' => true]);
41+
return $failedBatches;
3842
});
3943
}
44+
45+
private function requeueStalledBatches(): void
46+
{
47+
$threshold = Carbon::now()->subtract(new \DateInterval($this->pendingTimeout));
48+
QsBatch::where([
49+
['pending_since', '<>', null],
50+
['pending_since', '<', $threshold],
51+
])
52+
->increment(
53+
'processing_attempts', 1,
54+
['pending_since' => null, 'done' => 0]
55+
);
56+
}
4057
}

tests/Jobs/RequeuePendingQsBatchesJobTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Foundation\Testing\RefreshDatabase;
99
use Illuminate\Contracts\Queue\Job;
1010
use Carbon\Carbon;
11+
use Illuminate\Contracts\Debug\ExceptionHandler;
1112

1213
class RequeuePendingQsBatchesJobTest extends TestCase
1314
{
@@ -30,6 +31,12 @@ public function testRequeue (): void {
3031
QsBatch::factory()->create(['pending_since' => Carbon::now()->subSeconds(400), 'id' => 2, 'done' => 0, 'wiki_id' => 1, 'entityIds' => 'a,b']);
3132
QsBatch::factory()->create(['processing_attempts' => 3, 'id' => 3, 'done' => 0, 'wiki_id' => 1, 'entityIds' => 'a,b']);
3233

34+
$mockExceptionHandler = $this->createMock(ExceptionHandler::class);
35+
$mockExceptionHandler
36+
->expects($this->once())
37+
->method('report');
38+
$this->app->instance(ExceptionHandler::class, $mockExceptionHandler);
39+
3340
$mockJob = $this->createMock(Job::class);
3441
$job = new RequeuePendingQsBatchesJob();
3542
$job->setJob($mockJob);

0 commit comments

Comments
 (0)