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

[11.x] Add Skip middleware for Queue Jobs #52645

Merged
merged 3 commits into from
Sep 6, 2024

Conversation

KennedyTedesco
Copy link
Contributor

@KennedyTedesco KennedyTedesco commented Sep 4, 2024

I noticed a pattern where I was frequently using closure middleware to skip some jobs after they were queued (some of my jobs are delayed):

class MyJob implements ShouldQueue
{
    use Queueable;

    public function handle(): void
    {
        // TODO
    }

    public function middleware(): array
    {
        return [
            function ($job, $next) {
                if ($someCondition) {
                    $next($job);
                }
            },
        ];
    }
}

So, I created a Skip middleware for Queue Jobs to replace those use cases:

class MyJob implements ShouldQueue
{
    use Queueable;

    public function handle(): void
    {
        // TODO
    }

    public function middleware(): array
    {
        return [
            Skip::when($someCondition),
        ];
    }
}

I thought it might be useful to include this in the core. This middleware can be used by passing a bool or a closure:

class MyJob implements ShouldQueue
{
    use Queueable;

    public function handle(): void
    {
        // TODO
    }

    public function middleware(): array
    {
        return [
            Skip::when($someCondition),

            Skip::unless($someCondition),

            Skip::when(function(): bool {
                if ($someCondition) {
                    return true;
                }
                return false;
            }),
        ];
    }
}

@henzeb
Copy link
Contributor

henzeb commented Sep 5, 2024

i'd say use "when" instead of "if". This is more in line with other parts of laravel. (Conditionable trait)

@KennedyTedesco
Copy link
Contributor Author

@henzeb Thanks for the suggestion! I'm open to using when if desirable.

@taylorotwell taylorotwell merged commit b7c6287 into laravel:11.x Sep 6, 2024
31 checks passed
@KennedyTedesco KennedyTedesco deleted the job-skip branch September 7, 2024 00:44
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.

3 participants