laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

[Proposal] Conditionally dispatch jobs to the queue or synchronous #2377

Open marijoo opened 4 years ago

marijoo commented 4 years ago

Motivation

In the past I have often implemented functionality that adds a job to the queue when a certain condition is met or otherwise executes it synchronously.

Example

When importing data it could be useful to execute the job asynchronous or synchronous based on the data amount. Today I’d do something like:

if ($data->count() < 50) {
    ImportJob::dispatchSync($data, $anotherVar);
} else {
    ImportJob::dispatch($data, $anotherVar);
}

Proposal

I’d like to implement a convenience-method like dispatchIf or dispatchUnless although I'm still undecided on a good name.

ImportJob::dispatchSyncIf($data->count() < 50, $data, $anotherVar);

Thanks for your time. Would be my first contribution, so I’d be grateful for feedback, if this is a reasonable idea and if it’d be worth my time.