Skip to content

Commit

Permalink
Implement Silenced interface (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Jan 16, 2023
1 parent 50f9fd9 commit 355ff05
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Contracts/Silenced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Laravel\Horizon\Contracts;

interface Silenced
{
}
6 changes: 5 additions & 1 deletion src/Listeners/MarkJobAsComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 @@ -43,7 +44,10 @@ public function __construct(JobRepository $jobs, TagRepository $tags)
*/
public function handle(JobDeleted $event)
{
$this->jobs->completed($event->payload, $event->job->hasFailed(), in_array($event->payload->commandName(), config('horizon.silenced', [])));
$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);

if (! $event->job->hasFailed() && count($this->tags->monitored($event->payload->tags())) > 0) {
$this->jobs->remember($event->connectionName, $event->queue, $event->payload);
Expand Down
84 changes: 84 additions & 0 deletions tests/Feature/Listeners/MarkJobAsCompleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Laravel\Horizon\Tests\Feature\Listeners;

use Illuminate\Queue\Jobs\RedisJob;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Contracts\Silenced;
use Laravel\Horizon\Contracts\TagRepository;
use Laravel\Horizon\Events\JobDeleted;
use Laravel\Horizon\JobPayload;
use Laravel\Horizon\Listeners\MarkJobAsComplete;
use Laravel\Horizon\Tests\IntegrationTest;
use Mockery as m;

class MarkJobAsCompleteTest extends IntegrationTest
{
protected function tearDown(): void
{
parent::tearDown();

m::close();
}

protected function getEnvironmentSetUp($app): void
{
parent::getEnvironmentSetUp($app);

$app['config']->set('horizon.silenced', [
'App\\Jobs\\ConfigJob',
]);
}

public function test_it_can_mark_a_job_as_complete(): void
{
$this->runScenario('App\\Jobs\\TestJob', false);
}

public function test_it_can_handle_silenced_jobs_from_the_config(): void
{
$this->runScenario('App\\Jobs\\ConfigJob', true);
}

public function test_it_can_handle_silenced_jobs_from_an_interface(): void
{
$this->runScenario(SilencedJob::class, true);
}

public function test_it_can_handle_jobs_which_are_not_silenced(): void
{
$this->runScenario(NonSilencedJob::class, false);
}

public function runScenario(string $job, bool $silenced): void
{
$payload = m::mock(JobPayload::class);
$payload->shouldReceive('commandName')->andReturn($job);
$payload->shouldReceive('tags')->andReturn([]);

$job = m::mock(RedisJob::class);
$job->shouldReceive('hasFailed')->andReturn(false);

$event = m::mock(JobDeleted::class);
$event->payload = $payload;
$event->job = $job;

$jobs = m::mock(JobRepository::class);
$jobs->shouldReceive('completed')->once()->with($payload, false, $silenced);

$tags = m::mock(TagRepository::class);
$tags->shouldReceive('monitored')->once()->with([])->andReturn([]);

$listener = new MarkJobAsComplete($jobs, $tags);

$listener->handle($event);
}
}

class SilencedJob implements Silenced
{
}

class NonSilencedJob
{
}

0 comments on commit 355ff05

Please sign in to comment.