Skip to content

Commit 42c7776

Browse files
authored
Merge pull request #50 from scripting4u/main
exclude jobs via config or interface
2 parents 33668c7 + 4e92674 commit 42c7776

File tree

7 files changed

+77
-0
lines changed

7 files changed

+77
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,27 @@ public function boot(): void
288288

289289
Want to keep the dashboard public but still record jobs? Either return `true` from the gate even for guests, or set `VANTAGE_AUTH_ENABLED=false`.
290290

291+
### Exclude Jobs from Monitoring
292+
293+
if you want to exclude some jobs from being tracked by Vantage, you can add them to the `exclude_jobs` config array:
294+
295+
```php
296+
'exclude_jobs' => [
297+
App\Jobs\SomeNoisyJob::class,
298+
],
299+
```
300+
301+
Or implement the 'ShouldNotBeTracked' class.
302+
303+
```php
304+
use HoudaSlassi\Vantage\Contracts\ShouldNotBeTracked;
305+
306+
class MyNoisyJob implements ShouldQueue, ShouldNotBeTracked
307+
{
308+
public function handle(): void { ... }
309+
}
310+
```
311+
291312
## Testing
292313

293314
### Using the Model Factory

config/vantage.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,15 @@
170170
'logging' => [
171171
'enabled' => env('VANTAGE_LOGGING_ENABLED', true),
172172
],
173+
174+
/*
175+
|--------------------------------------------------------------------------
176+
| Excluded Jobs
177+
|--------------------------------------------------------------------------
178+
| Job classes listed here will not be recorded by Vantage.
179+
| You can also implement ShouldNotBeTracked on any job class.
180+
*/
181+
'exclude_jobs' => [
182+
// App\Jobs\SomeNoisyJob::class,
183+
],
173184
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HoudaSlassi\Vantage\Contracts;
6+
7+
interface ShouldNotBeTracked {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HoudaSlassi\Vantage\Listeners;
6+
7+
use HoudaSlassi\Vantage\Contracts\ShouldNotBeTracked;
8+
9+
trait ChecksJobExclusion
10+
{
11+
protected function isExcluded(string $jobClass): bool
12+
{
13+
// Check interface
14+
if (is_a($jobClass, ShouldNotBeTracked::class, true)) {
15+
return true;
16+
}
17+
18+
// Check config exclude list
19+
$excluded = config('vantage.exclude_jobs', []);
20+
21+
return in_array($jobClass, $excluded, true);
22+
}
23+
}

src/Listeners/RecordJobFailure.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
class RecordJobFailure
1717
{
18+
use ChecksJobExclusion;
1819
use ExtractsRetryOf;
1920

2021
public function handle(JobFailed $event): void
@@ -24,6 +25,10 @@ public function handle(JobFailed $event): void
2425
return;
2526
}
2627

28+
if ($this->isExcluded($event->job->resolveName())) {
29+
return;
30+
}
31+
2732
$uuid = $this->bestUuid($event);
2833
$jobClass = $this->jobClass($event);
2934
$queue = $event->job->getQueue();

src/Listeners/RecordJobStart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class RecordJobStart
1515
{
16+
use ChecksJobExclusion;
1617
use ExtractsRetryOf;
1718

1819
public function handle(JobProcessing $event): void
@@ -22,6 +23,10 @@ public function handle(JobProcessing $event): void
2223
return;
2324
}
2425

26+
if ($this->isExcluded($event->job->resolveName())) {
27+
return;
28+
}
29+
2530
$uuid = $this->bestUuid($event);
2631

2732
// Telemetry config & sampling

src/Listeners/RecordJobSuccess.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class RecordJobSuccess
1414
{
15+
use ChecksJobExclusion;
1516
use ExtractsRetryOf;
1617

1718
public function handle(JobProcessed $event): void
@@ -21,6 +22,10 @@ public function handle(JobProcessed $event): void
2122
return;
2223
}
2324

25+
if ($this->isExcluded($event->job->resolveName())) {
26+
return;
27+
}
28+
2429
// Some jobs (like rate-limited ones) are "processed" only to be released immediately.
2530
// Laravel exposes helpers to detect this so we don't count them as successful runs.
2631
if (method_exists($event->job, 'isReleased') && $event->job->isReleased()) {

0 commit comments

Comments
 (0)