Skip to content

Commit 1e71258

Browse files
Fixed the ended() query scope in the JobExecution model
1 parent 24285bc commit 1e71258

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Models/JobExecution.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ protected function scopeActives(Builder $query): Builder
219219

220220
protected function scopeEnded(Builder $query): Builder
221221
{
222-
return $query->whereNotNull('completed_at')->whereNotNull('failed_at');
222+
return $query->whereNotNull('completed_at')->orWhereNotNull('failed_at');
223223
}
224224

225225
protected function log(string $message, string $level, array $context): JobExecutionLogContract

tests/JobExecutionTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,48 @@ public function it_returns_the_latest_log_date_when_any_of_the_log_entries_are_l
171171

172172
$this->assertEquals('2024-11-27 14:00:20', $job->lastTimeSomethingHasHappenedWithIt()->format('Y-m-d H:i:s'));
173173
}
174+
175+
/** @test */
176+
public function the_actives_scope_returns_the_records_where_neither_completed_at_nor_failed_at_are_set()
177+
{
178+
JobExecution::ofJobClass(SampleTrackableJob::class)->delete();
179+
180+
$this->createSampleJobExecution();
181+
$this->createSampleJobExecution();
182+
$this->createSampleJobExecution();
183+
$this->createSampleJobExecution();
184+
$this->createSampleJobExecution(['failed_at' => Carbon::now()]);
185+
$this->createSampleJobExecution(['completed_at' => Carbon::now()]);
186+
$this->createSampleJobExecution(['completed_at' => Carbon::now(), 'failed_at' => Carbon::now()]); // stupid, yeah
187+
188+
$this->assertCount(4, JobExecution::actives()->get());
189+
}
190+
191+
/** @test */
192+
public function the_ended_scope_returns_the_records_where_either_completed_at_or_failed_at_are_set()
193+
{
194+
JobExecution::ofJobClass(SampleTrackableJob::class)->delete();
195+
196+
$this->createSampleJobExecution();
197+
$this->createSampleJobExecution();
198+
$this->createSampleJobExecution(['failed_at' => Carbon::now()]);
199+
$this->createSampleJobExecution(['completed_at' => Carbon::now()]);
200+
$this->createSampleJobExecution(['completed_at' => Carbon::now(), 'failed_at' => Carbon::now()]);
201+
202+
$this->assertCount(3, JobExecution::ended()->get());
203+
}
204+
205+
private function createSampleJobExecution(array $attributes = []): JobExecution
206+
{
207+
$data = array_merge(
208+
[
209+
'job_class' => SampleTrackableJob::class,
210+
'tracking_id' => Str::ulid()->toBase58(),
211+
'queued_at' => Carbon::now(),
212+
],
213+
$attributes,
214+
);
215+
216+
return JobExecution::create($data);
217+
}
174218
}

0 commit comments

Comments
 (0)