Skip to content

Commit 7856536

Browse files
authored
Fix horizon capturing jobs (#125)
1 parent 5faaa7d commit 7856536

File tree

7 files changed

+52
-24
lines changed

7 files changed

+52
-24
lines changed

.github/workflows/pull_request.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ concurrency:
99

1010
env:
1111
minimum_supported_php_version: "8.2"
12-
package_extensions: none, mbstring, curl, ctype, tokenizer, mongodb, xml, pdo, fileinfo, dom, xmlwriter, simplexml, sqlite3, pdo_sqlite
12+
package_extensions: none, mbstring, curl, ctype, tokenizer, mongodb, xml, pdo, fileinfo, dom, xmlwriter, simplexml, sqlite3, pdo_sqlite, posix, redis
1313
agent_extensions: none, mbstring, curl, ctype, dom, simplexml, tokenizer, xml, xmlwriter
1414
client_extensions: none, mbstring, curl, ctype, dom, simplexml, tokenizer, xml, xmlwriter
1515

@@ -337,6 +337,13 @@ jobs:
337337
- build_client
338338
if: ${{ (needs.build_agent.outputs.agent-committed == 'false') && (needs.build_client.outputs.client-committed == 'false') }}
339339

340+
services:
341+
redis:
342+
image: redis
343+
ports:
344+
- 6379:6379
345+
options: --entrypoint redis-server
346+
340347
strategy:
341348
matrix:
342349
php: [8.2, 8.3]

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"guzzlehttp/guzzle": "^7.0",
3131
"guzzlehttp/promises": "^2.0",
3232
"guzzlehttp/psr7": "^2.0",
33+
"laravel/horizon": "^5.4",
3334
"laravel/pint": "1.21.0",
3435
"mockery/mockery": "^1.0",
3536
"mongodb/laravel-mongodb": "^5.0",

src/Hooks/CommandStartingListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __invoke(CommandStarting $event): void
4949

5050
try {
5151
match ($event->command) {
52-
'queue:work', 'queue:listen' => $this->registerJobHooks(),
52+
'queue:work', 'queue:listen', 'horizon:work' => $this->registerJobHooks(),
5353
'schedule:run', 'schedule:work' => $this->registerScheduledTaskHooks(),
5454
default => $this->registerCommandHooks(),
5555
};

testbench.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
providers:
22
- Laravel\Nightwatch\NightwatchServiceProvider
3-
- App\Providers\AppServiceProvider
43

54
workbench:
65
start: '/'

tests/Feature/Sensors/CommandSensorTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
it('filters out the list command')->todo();
7676
it('filters out the queue:work command')->todo();
7777
it('filters out the queue:listen command')->todo();
78+
it('filters out the horizon:work command')->todo();
7879

7980
it('modifies status code to value in range of 0-255', function () {
8081
$ingest = fakeIngest();

tests/Feature/Sensors/JobAttemptSensorTest.php

+35-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Facades\Config;
1313
use Illuminate\Support\Facades\Event;
1414
use Illuminate\Support\Facades\Mail;
15+
use Illuminate\Support\Facades\Redis;
1516
use Illuminate\Support\Str;
1617

1718
use function Pest\Laravel\travelTo;
@@ -30,17 +31,30 @@
3031

3132
setTraceId('0d3ca349-e222-4982-ac23-2343692de258');
3233
Config::set('queue.default', 'database');
34+
Redis::command('FLUSHALL');
3335
})->skip(version_compare(Application::VERSION, '11.0.0', '<'), 'Laravel 10 support is pending');
3436

35-
it('ingests processed job attempts', function () {
37+
$workCommands = [
38+
'queue:work',
39+
'horizon:work',
40+
];
41+
42+
$workOptions = [
43+
'--max-jobs' => 1,
44+
'--sleep' => 0,
45+
'--stop-when-empty' => true,
46+
'--tries' => 1,
47+
];
48+
49+
it('ingests processed job attempts', function ($workCommand) use ($workOptions) {
3650
$ingest = fakeIngest();
3751
Str::createUuidsUsingSequence([
3852
$jobId = 'e2cb5fa7-6c2e-4bc5-82c9-45e79c3e8fdd',
3953
$attemptId = '02cb9091-8973-427f-8d3f-042f2ec4e862',
4054
]);
4155
ProcessedJob::dispatch();
4256

43-
Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true, '--sleep' => 0]);
57+
Artisan::call($workCommand, $workOptions);
4458

4559
$ingest->assertWrittenTimes(1);
4660
$ingest->assertLatestWrite('job-attempt:*', [
@@ -76,17 +90,17 @@
7690
'peak_memory_usage' => 1234,
7791
],
7892
]);
79-
});
93+
})->with($workCommands);
8094

81-
it('ingests job released job attempts', function () {
95+
it('ingests job released job attempts', function ($workCommand) use ($workOptions) {
8296
$ingest = fakeIngest();
8397
Str::createUuidsUsingSequence([
8498
$jobId = 'e2cb5fa7-6c2e-4bc5-82c9-45e79c3e8fdd',
8599
$attemptId = '02cb9091-8973-427f-8d3f-042f2ec4e862',
86100
]);
87101
ReleasedJob::dispatch();
88102

89-
Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true, '--sleep' => 0]);
103+
Artisan::call($workCommand, $workOptions);
90104

91105
$ingest->assertWrittenTimes(1);
92106
$ingest->assertLatestWrite('job-attempt:*', [
@@ -122,17 +136,17 @@
122136
'peak_memory_usage' => 1234,
123137
],
124138
]);
125-
});
139+
})->with($workCommands);
126140

127-
it('ingests job failed job attempts', function () {
141+
it('ingests job failed job attempts', function ($workCommand) use ($workOptions) {
128142
$ingest = fakeIngest();
129143
Str::createUuidsUsingSequence([
130144
$jobId = 'e2cb5fa7-6c2e-4bc5-82c9-45e79c3e8fdd',
131145
$attemptId = '02cb9091-8973-427f-8d3f-042f2ec4e862',
132146
]);
133147
FailedJob::dispatch();
134148

135-
Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true, '--sleep' => 0]);
149+
Artisan::call($workCommand, $workOptions);
136150

137151
$ingest->assertWrittenTimes(1);
138152
$ingest->assertLatestWrite('job-attempt:*', [
@@ -170,7 +184,7 @@
170184
]);
171185
$ingest->assertLatestWrite('exception:0.execution_source', 'job');
172186
$ingest->assertLatestWrite('exception:0.execution_id', $attemptId);
173-
});
187+
})->with($workCommands);
174188

175189
it('does not ingest jobs dispatched on the sync queue', function () {
176190
$ingest = fakeIngest();
@@ -179,7 +193,7 @@
179193
$ingest->assertWrittenTimes(0);
180194
});
181195

182-
it('captures closure job', function () {
196+
it('captures closure job', function ($workCommand) use ($workOptions) {
183197
$ingest = fakeIngest();
184198
Str::createUuidsUsingSequence([
185199
$jobId = 'e2cb5fa7-6c2e-4bc5-82c9-45e79c3e8fdd',
@@ -190,7 +204,7 @@
190204
travelTo(now()->addMicroseconds(2500));
191205
});
192206

193-
Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true, '--sleep' => 0]);
207+
Artisan::call($workCommand, $workOptions);
194208

195209
$ingest->assertWrittenTimes(1);
196210
$ingest->assertLatestWrite('job-attempt:*', [
@@ -226,9 +240,9 @@
226240
'peak_memory_usage' => 1234,
227241
],
228242
]);
229-
});
243+
})->with($workCommands);
230244

231-
it('captures queued event listener', function () {
245+
it('captures queued event listener', function ($workCommand) use ($workOptions) {
232246
$ingest = fakeIngest();
233247
Str::createUuidsUsingSequence([
234248
$jobId = 'e2cb5fa7-6c2e-4bc5-82c9-45e79c3e8fdd',
@@ -237,7 +251,7 @@
237251
Event::listen(MyEvent::class, MyEventListener::class);
238252
Event::dispatch(new MyEvent);
239253

240-
Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true, '--sleep' => 0]);
254+
Artisan::call($workCommand, $workOptions);
241255

242256
$ingest->assertWrittenTimes(1);
243257
$ingest->assertLatestWrite('job-attempt:*', [
@@ -273,9 +287,9 @@
273287
'peak_memory_usage' => 1234,
274288
],
275289
]);
276-
});
290+
})->with($workCommands);
277291

278-
it('captures queued mail', function () {
292+
it('captures queued mail', function ($workCommand) use ($workOptions) {
279293
$ingest = fakeIngest();
280294
Str::createUuidsUsingSequence([
281295
$jobId = 'e2cb5fa7-6c2e-4bc5-82c9-45e79c3e8fdd',
@@ -285,7 +299,7 @@
285299

286300
Mail::to('[email protected]')->queue(new MyQueuedMail);
287301

288-
Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true, '--sleep' => 0]);
302+
Artisan::call($workCommand, $workOptions);
289303

290304
$ingest->assertWrittenTimes(1);
291305
$ingest->assertLatestWrite('job-attempt:*', [
@@ -345,17 +359,17 @@
345359
'failed' => false,
346360
],
347361
]);
348-
});
362+
})->with($workCommands);
349363

350-
it('captures multiple job attempts', function () {
364+
it('captures multiple job attempts', function ($workCommand) use ($workOptions) {
351365
$ingest = fakeIngest();
352366
FailedJob::dispatch();
353367

354-
Artisan::call('queue:work', ['--max-jobs' => 2, '--tries' => 2, '--stop-when-empty' => true, '--sleep' => 0]);
368+
Artisan::call($workCommand, [...$workOptions, '--max-jobs' => 2, '--tries' => 2]);
355369

356370
$ingest->assertWrittenTimes(2);
357371
$ingest->assertLatestWrite('job-attempt:0.attempt', 2);
358-
});
372+
})->with($workCommands);
359373

360374
final class ProcessedJob implements ShouldQueue
361375
{

workbench/bootstrap/providers.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
\App\Providers\AppServiceProvider::class,
5+
\Laravel\Horizon\HorizonServiceProvider::class,
6+
];

0 commit comments

Comments
 (0)