Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
taylorotwell merged 3 commits into
laravel:11.xfrom
KennedyTedesco:job-skip
Sep 6, 2024
Merged

[11.x] Add Skip middleware for Queue Jobs#52645
taylorotwell merged 3 commits into
laravel:11.xfrom
KennedyTedesco:job-skip

Conversation

@KennedyTedesco

@KennedyTedesco KennedyTedesco commented Sep 4, 2024

Copy link
Copy Markdown
Contributor

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

henzeb commented Sep 5, 2024

Copy link
Copy Markdown
Contributor

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

@KennedyTedesco

Copy link
Copy Markdown
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
@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