Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Allow any queued class to be silenced #1241

Merged
merged 6 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/JobPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Notifications\SendQueuedNotifications;
use Illuminate\Support\Arr;
use Laravel\Horizon\Contracts\Silenced;

class JobPayload implements ArrayAccess
{
Expand Down Expand Up @@ -78,6 +79,16 @@ public function retryOf()
return $this->decoded['retry_of'] ?? null;
}

/**
* Determine if the job has been silenced.
*
* @return bool
*/
public function isSilenced()
{
return $this->decoded['silenced'] ?? false;
}

/**
* Prepare the payload for storage on the queue by adding tags, etc.
*
Expand All @@ -89,6 +100,7 @@ public function prepare($job)
return $this->set([
'type' => $this->determineType($job),
'tags' => $this->determineTags($job),
'silenced' => $this->shouldBeSilenced($job),
'pushedAt' => str_replace(',', '.', microtime(true)),
]);
}
Expand Down Expand Up @@ -129,6 +141,48 @@ protected function determineTags($job)
);
}

/**
* Determine if the underlying job class should be silenced.
*
* @param mixed $job
* @return bool
*/
protected function shouldBeSilenced($job)
{
if (! $job) {
return false;
}

$underlyingJob = $this->underlyingJob($job);

$jobClass = is_string($underlyingJob) ? $underlyingJob : get_class($underlyingJob);

return in_array($jobClass, config('horizon.silenced', [])) ||
is_a($jobClass, Silenced::class, true);
}

/**
* Get the underlying queued job.
*
* @param mixed $job
* @return mixed
*/
protected function underlyingJob($job)
{
switch (true) {
case $job instanceof BroadcastEvent:
return $job->event;
case $job instanceof CallQueuedListener:
return $job->class;
case $job instanceof SendQueuedMailable:
return $job->mailable;
case $job instanceof SendQueuedNotifications:
return $job->notification;
default:
return $job;
}
}

/**
* Set the given key / value pairs on the payload.
*
Expand Down
6 changes: 1 addition & 5 deletions src/Listeners/MarkJobAsComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laravel\Horizon\Listeners;

use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Contracts\Silenced;
use Laravel\Horizon\Contracts\TagRepository;
use Laravel\Horizon\Events\JobDeleted;

Expand Down Expand Up @@ -44,10 +43,7 @@ public function __construct(JobRepository $jobs, TagRepository $tags)
*/
public function handle(JobDeleted $event)
{
$isSilenced = in_array($event->payload->commandName(), config('horizon.silenced', [])) ||
is_a($event->payload->commandName(), Silenced::class, true);

$this->jobs->completed($event->payload, $event->job->hasFailed(), $isSilenced);
$this->jobs->completed($event->payload, $event->job->hasFailed(), $event->payload->isSilenced());

if (! $event->job->hasFailed() && count($this->tags->monitored($event->payload->tags())) > 0) {
$this->jobs->remember($event->connectionName, $event->queue, $event->payload);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeEvent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeEventWithModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeJobWithEloquentCollection
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeJobWithEloquentModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeJobWithTagsMethod
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeListener
{
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/Fixtures/FakeListenerSilenced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Laravel\Horizon\Tests\Feature\Fixtures;

use Illuminate\Contracts\Events\Dispatcher;
use Laravel\Horizon\Contracts\Silenced;

class FakeListenerSilenced implements Silenced
{
/**
* @var Dispatcher
*/
protected $dispatcher;

/**
* @var FakeEventWithModel
*/
protected $fakeModel;

public function __construct(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

public function handle(FakeEventWithModel $fakeEventWithModel): void
{
$this->fakeModel = $fakeEventWithModel->model;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

use Illuminate\Contracts\Events\Dispatcher;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

use Illuminate\Contracts\Events\Dispatcher;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravel\Horizon\Tests\Unit\Fixtures;
namespace Laravel\Horizon\Tests\Feature\Fixtures;

use Illuminate\Database\Eloquent\Model;

Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/Fixtures/FakeSilencedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Laravel\Horizon\Tests\Feature\Fixtures;

use Laravel\Horizon\Contracts\Silenced;

class FakeSilencedJob implements Silenced
{
}
9 changes: 9 additions & 0 deletions tests/Feature/Fixtures/SilencedMailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Laravel\Horizon\Tests\Feature\Fixtures;

use Illuminate\Contracts\Mail\Mailable;

interface SilencedMailable extends Mailable
{
}
1 change: 1 addition & 0 deletions tests/Feature/Listeners/MarkJobAsCompleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function runScenario(string $job, bool $silenced): void
$payload = m::mock(JobPayload::class);
$payload->shouldReceive('commandName')->andReturn($job);
$payload->shouldReceive('tags')->andReturn([]);
$payload->shouldReceive('isSilenced')->andReturn($silenced);

$job = m::mock(RedisJob::class);
$job->shouldReceive('hasFailed')->andReturn(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
<?php

namespace Laravel\Horizon\Tests\Unit;
namespace Laravel\Horizon\Tests\Feature;

use Illuminate\Broadcasting\BroadcastEvent;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Events\CallQueuedListener;
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Notifications\SendQueuedNotifications;
use Laravel\Horizon\Contracts\Silenced;
use Laravel\Horizon\JobPayload;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeEvent;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeEventWithModel;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeJobWithEloquentCollection;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeJobWithEloquentModel;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeJobWithTagsMethod;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeListener;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeListenerWithProperties;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeListenerWithTypedProperties;
use Laravel\Horizon\Tests\Unit\Fixtures\FakeModel;
use Laravel\Horizon\Tests\UnitTest;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeEvent;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeEventWithModel;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeJobWithEloquentCollection;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeJobWithEloquentModel;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeJobWithTagsMethod;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListener;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerSilenced;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithProperties;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithTypedProperties;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeModel;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeSilencedJob;
use Laravel\Horizon\Tests\Feature\Fixtures\SilencedMailable;
use Laravel\Horizon\Tests\IntegrationTest;
use Mockery;
use StdClass;

class RedisPayloadTest extends UnitTest
class RedisPayloadTest extends IntegrationTest
{
public function test_type_is_correctly_determined()
{
Expand Down Expand Up @@ -152,4 +156,34 @@ public function test_jobs_can_have_tags_method_to_override_auto_tagging()
$JobPayload->prepare(new FakeJobWithTagsMethod);
$this->assertEquals(['first', 'second'], $JobPayload->decoded['tags']);
}

public function test_it_determines_if_job_is_silenced_correctly()
{
$JobPayload = new JobPayload(json_encode(['id' => 1]));

$JobPayload->prepare(new BroadcastEvent(new class implements Silenced {}));
$this->assertTrue($JobPayload->isSilenced());

$JobPayload->prepare(new CallQueuedListener(FakeListenerSilenced::class, 'handle', [new FakeEventWithModel(42)]));
$this->assertTrue($JobPayload->isSilenced());

$JobPayload->prepare(new SendQueuedNotifications([], new class implements Silenced {}, ['mail']));
$this->assertTrue($JobPayload->isSilenced());

$JobPayload->prepare(new FakeSilencedJob);
$this->assertTrue($JobPayload->isSilenced());

$JobPayload->prepare(new BroadcastEvent(new class {}));
$this->assertFalse($JobPayload->isSilenced());
}

public function test_it_determines_if_job_is_silenced_correctly_for_mailable()
{
$JobPayload = new JobPayload(json_encode(['id' => 1]));

$mailableMock = Mockery::mock(SilencedMailable::class);
config(['horizon.silenced' => [get_class($mailableMock)]]);
$JobPayload->prepare(new SendQueuedMailable($mailableMock));
$this->assertTrue($JobPayload->isSilenced());
}
}