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] Implement Silenced interface #1236

Merged
merged 1 commit into from
Jan 16, 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
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
{
}