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

Idea: extend Laravel's batch functionality #1

Open
emilklindt opened this issue Nov 7, 2024 · 1 comment
Open

Idea: extend Laravel's batch functionality #1

emilklindt opened this issue Nov 7, 2024 · 1 comment

Comments

@emilklindt
Copy link

Interesting package, @itsemon245. Looking through the repository and documentation, I have a few comments and some feedback. Take them for what they are and feel free to discard them.

Since campaign emails will likely to be processed by the queue using a job we can no longer pause the queue or the jobs for this specific campaign because they have already been dispatched.

Given the use case described, Laravel’s built-in Job Batching could provide a solid foundation to build upon. Batching is typically the preferred approach for scenarios like this. While batches support cancellation, they currently lack a pause functionality.

$batch->pause(); // Call to undefined method

Tying the state to a batch instead of an Eloquent model would allow for greater flexibility in package usage. You can avoid the $this->setPausedBy inside the job, and still allow for easily pausing the batch from inside the job:

/**
 * Execute the job.
 */
public function handle(): void
{
    $this->batch()->pause();
}

In order to retrieve a batch instance you may query the Laravel command bus after creation. Pausing after dispatch would be as straight forward as:

use App\Jobs\SendEmail;
use Illuminate\Support\Facades\Bus;
 
$batch = Bus::batch([
    new SendEmail($campaign),
    new SendEmail($campaign),
])->dispatch();

$batch->pause();
$batch->resume();

I'll leave the decision and implementation to you. If you want to experiment with making any job pausable, take a look at laravel/framework#46163 (comment). Remaining explicit with the Pausable trait may be better though.

See laravel/ideas#735 if you haven't already. Good luck with your first Laravel package!

@itsemon245
Copy link
Owner

Thank you for the valuable insight.

The original idea behind this package solely revolves around the polymorphic relationship based on the use cases I had at that time.The idea of using the batch to pause sounds more efficient specially because I don't have to bother with attaching any model anymore.
And I didn't know there was a proposal this far back. Seems like the laravel team haven't really bought into the idea.

I will take your advice and hopefully come up with a sane implementation.

Wish me luck and Thanks again.

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

No branches or pull requests

2 participants