Skip to content

Commit 80916ff

Browse files
[5.x] Allow any queued class to be silenced (#1241)
* move sileneced logic to job payload * move fixtures and payload test to feature * fix tests * styling * make methods protected
1 parent 287b929 commit 80916ff

16 files changed

+158
-26
lines changed

src/JobPayload.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Mail\SendQueuedMailable;
99
use Illuminate\Notifications\SendQueuedNotifications;
1010
use Illuminate\Support\Arr;
11+
use Laravel\Horizon\Contracts\Silenced;
1112

1213
class JobPayload implements ArrayAccess
1314
{
@@ -78,6 +79,16 @@ public function retryOf()
7879
return $this->decoded['retry_of'] ?? null;
7980
}
8081

82+
/**
83+
* Determine if the job has been silenced.
84+
*
85+
* @return bool
86+
*/
87+
public function isSilenced()
88+
{
89+
return $this->decoded['silenced'] ?? false;
90+
}
91+
8192
/**
8293
* Prepare the payload for storage on the queue by adding tags, etc.
8394
*
@@ -89,6 +100,7 @@ public function prepare($job)
89100
return $this->set([
90101
'type' => $this->determineType($job),
91102
'tags' => $this->determineTags($job),
103+
'silenced' => $this->shouldBeSilenced($job),
92104
'pushedAt' => str_replace(',', '.', microtime(true)),
93105
]);
94106
}
@@ -129,6 +141,48 @@ protected function determineTags($job)
129141
);
130142
}
131143

144+
/**
145+
* Determine if the underlying job class should be silenced.
146+
*
147+
* @param mixed $job
148+
* @return bool
149+
*/
150+
protected function shouldBeSilenced($job)
151+
{
152+
if (! $job) {
153+
return false;
154+
}
155+
156+
$underlyingJob = $this->underlyingJob($job);
157+
158+
$jobClass = is_string($underlyingJob) ? $underlyingJob : get_class($underlyingJob);
159+
160+
return in_array($jobClass, config('horizon.silenced', [])) ||
161+
is_a($jobClass, Silenced::class, true);
162+
}
163+
164+
/**
165+
* Get the underlying queued job.
166+
*
167+
* @param mixed $job
168+
* @return mixed
169+
*/
170+
protected function underlyingJob($job)
171+
{
172+
switch (true) {
173+
case $job instanceof BroadcastEvent:
174+
return $job->event;
175+
case $job instanceof CallQueuedListener:
176+
return $job->class;
177+
case $job instanceof SendQueuedMailable:
178+
return $job->mailable;
179+
case $job instanceof SendQueuedNotifications:
180+
return $job->notification;
181+
default:
182+
return $job;
183+
}
184+
}
185+
132186
/**
133187
* Set the given key / value pairs on the payload.
134188
*

src/Listeners/MarkJobAsComplete.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Laravel\Horizon\Listeners;
44

55
use Laravel\Horizon\Contracts\JobRepository;
6-
use Laravel\Horizon\Contracts\Silenced;
76
use Laravel\Horizon\Contracts\TagRepository;
87
use Laravel\Horizon\Events\JobDeleted;
98

@@ -44,10 +43,7 @@ public function __construct(JobRepository $jobs, TagRepository $tags)
4443
*/
4544
public function handle(JobDeleted $event)
4645
{
47-
$isSilenced = in_array($event->payload->commandName(), config('horizon.silenced', [])) ||
48-
is_a($event->payload->commandName(), Silenced::class, true);
49-
50-
$this->jobs->completed($event->payload, $event->job->hasFailed(), $isSilenced);
46+
$this->jobs->completed($event->payload, $event->job->hasFailed(), $event->payload->isSilenced());
5147

5248
if (! $event->job->hasFailed() && count($this->tags->monitored($event->payload->tags())) > 0) {
5349
$this->jobs->remember($event->connectionName, $event->queue, $event->payload);

tests/Unit/Fixtures/FakeEvent.php renamed to tests/Feature/Fixtures/FakeEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Horizon\Tests\Unit\Fixtures;
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
44

55
class FakeEvent
66
{

tests/Unit/Fixtures/FakeEventWithModel.php renamed to tests/Feature/Fixtures/FakeEventWithModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Horizon\Tests\Unit\Fixtures;
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
44

55
class FakeEventWithModel
66
{

tests/Unit/Fixtures/FakeJobWithEloquentCollection.php renamed to tests/Feature/Fixtures/FakeJobWithEloquentCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Horizon\Tests\Unit\Fixtures;
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
44

55
class FakeJobWithEloquentCollection
66
{

tests/Unit/Fixtures/FakeJobWithEloquentModel.php renamed to tests/Feature/Fixtures/FakeJobWithEloquentModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Horizon\Tests\Unit\Fixtures;
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
44

55
class FakeJobWithEloquentModel
66
{

tests/Unit/Fixtures/FakeJobWithTagsMethod.php renamed to tests/Feature/Fixtures/FakeJobWithTagsMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Horizon\Tests\Unit\Fixtures;
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
44

55
class FakeJobWithTagsMethod
66
{

tests/Unit/Fixtures/FakeListener.php renamed to tests/Feature/Fixtures/FakeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Horizon\Tests\Unit\Fixtures;
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
44

55
class FakeListener
66
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
4+
5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Laravel\Horizon\Contracts\Silenced;
7+
8+
class FakeListenerSilenced implements Silenced
9+
{
10+
/**
11+
* @var Dispatcher
12+
*/
13+
protected $dispatcher;
14+
15+
/**
16+
* @var FakeEventWithModel
17+
*/
18+
protected $fakeModel;
19+
20+
public function __construct(Dispatcher $dispatcher)
21+
{
22+
$this->dispatcher = $dispatcher;
23+
}
24+
25+
public function handle(FakeEventWithModel $fakeEventWithModel): void
26+
{
27+
$this->fakeModel = $fakeEventWithModel->model;
28+
}
29+
}

tests/Unit/Fixtures/FakeListenerWithProperties.php renamed to tests/Feature/Fixtures/FakeListenerWithProperties.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Laravel\Horizon\Tests\Unit\Fixtures;
3+
namespace Laravel\Horizon\Tests\Feature\Fixtures;
44

55
use Illuminate\Contracts\Events\Dispatcher;
66

0 commit comments

Comments
 (0)