10BC0 [12.x] feat: allow queued listeners to be unique by calebdw · Pull Request #58402 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@calebdw
Copy link
Contributor
@calebdw calebdw F440 commented Jan 16, 2026

Hello!

This PR allows queued listeners to implement ShouldBeUnique and ShouldBeUniqueUntilProcessing, similar to jobs. E.g,

<?php

namespace App\Listeners;

use App\Events\LicenseSaved;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;

class AcquireProductKey implements ShouldQueue, ShouldBeUnique
{
    public function uniqueId(LicenseSaved $event): string
    {
        return $event->license->id;
    }

    public function shouldQueue(LicenseSaved $event): bool
    {
        return $event->license->product_key === null;
    }

    public function __invoke(LicenseSaved $event): void
    {
        // ...
    }
}

Motivation

I just had a bug in production where a listener was dispatched to the queue in rapid succession when it should not have been. Unfortunately, queued listeners could not implement the ShouldBeUnique and ShouldBeUniqueUntilProcessing contracts---they can only use the WithoutOverlapping middleware which prevents two jobs from running at the same time, but this does nothing to prevent duplicate jobs from being dispatched to the queue in the first place.

Thanks!

@taylorotwell
Copy link
Member
taylorotwell commented Jan 24, 2026

@calebdw should we actually serialize the cache repository onto the queue in CallQueuedListener? Feels like that could cause issues with connections that can't be serialized, etc.? Any other options there?

Just storing docs PR here for myself for reference: laravel/docs#10996

@calebdw calebdw force-pushed the calebdw/push-umtknlumwytr branch from 00b42f5 to 92b54e5 Compare January 27, 2026 20:45
@calebdw
Copy link
Contributor Author
calebdw commented Jan 27, 2026

@taylorotwell, apologies for the delayed reply---I was without power for several days.

Good catch on not serializing the cache, I was just following the job pattern without thinking too much 😅

I updated the CallQueuedListener to pass through to the listener method at runtime instead of serializing the property on the job

@calebdw calebdw force-pushed the calebdw/push-umtknlumwytr branch 2 times, most recently from ed575a2 to 4656cd9 Compare January 27, 2026 20:51
@calebdw calebdw force-pushed the calebdw/push-umtknlumwytr branch from 4656cd9 to 5f9bc6c Compare January 28, 2026 11:56
@taylorotwell taylorotwell merged commit b32b3d4 into laravel:12.x Jan 28, 2026
20 of 70 checks passed
@calebdw calebdw deleted the calebdw/push-umtknlumwytr branch January 28, 2026 15:19
@calebdw
Copy link
Contributor Author
calebdw commented Jan 28, 2026

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0