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

Allow queue jobs with batches #4127

Open
wants to merge 10 commits into
base: 3.1
Choose a base branch
from
Open

Allow queue jobs with batches #4127

wants to merge 10 commits into from

Conversation

dazza-dev
Copy link

1️⃣ Why should it be added? What are the benefits of this change?
Allow import and export files with batches (New laravel feature for job batching).
This also works for Laravel < 8

2️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out.
No

3️⃣ Does it include tests, if possible?
No

4️⃣ Any drawbacks? Possible breaking changes?
No

5️⃣ Mark the following tasks as done:

  • Checked the codebase to ensure that your feature doesn't already exist.
  • Take note of the contributing guidelines.
  • Checked the pull requests to ensure that another person hasn't already submitted a fix.
  • Added tests to ensure against regression.

Copy link
Member

@patrickbrouwers patrickbrouwers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like what you did here. It seems it supports older versions and no breaking changes.
Some small remarks as comments.

Could you check if you could add a unit test for ShouldBatch interface like the ShouldQueue . Would be great to have in place. Those tests can be skipped if the Illuminate\Bus\Batchable trait doesn't exist

// Check if the import class is batchable
if ($import instanceof ShouldBatch) {
return Bus::batch([
$jobs->toArray(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By doing this, it will execute as a chain with in the batch right? It won't run parallel?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. This creates a batch and returns a PendingBatch.

}
} else {
trait BatchableTrait
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can add the methods we need else where like batchCancelled here as well, so we don't have to do a method_exist check there. We can return false here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -57,6 +59,13 @@ public function setDependencies(Collection $jobs)

public function handle()
{
// Determine if the batch has been cancelled...
if (method_exists($this, 'batchCancelled')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment in trait

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered implementing this with a job middleware, but there are versions of Laravel that do not support this, so I discarded the idea.

@stale stale bot added the stale label Aug 6, 2024
Copy link

stale bot commented Aug 8, 2024

This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.

@stale stale bot closed this Aug 8, 2024
@dazza-dev
Copy link
Author

Hi @patrickbrouwers, please let me know if anything else is needed to finalize this.

@stale stale bot removed the stale label Aug 8, 2024
@patrickbrouwers
Copy link
Member

See my previous comment about the unit test.

@dazza-dev
Copy link
Author

See my previous comment about the unit test.

Done

@keithbrink
Copy link
Contributor

@dazza-dev I started using this in my project, and it works great for exports, but one issue I found was that the ExcelFake returns a PendingDispatch instead of a PendingBatch.

@dazza-dev
Copy link
Author

Hi @keithbrink thanks for pointing that out! I haven't used ExcelFake before, so it would be really helpful if you could provide an example of your code where this issue occurs. That way, I can try to reproduce the problem on my end and work on a solution. Thanks in advance!

@keithbrink
Copy link
Contributor

For sure! It would look something like this:

Excel::fake();

$pending_batch = Excel::queue(new BatchExport, 'filename');

// This will work without the fake, and fail with the fake
$pending_batch->catch(fn(Batch $batch) => \Log::info($batch->id));

Excel::assertQueued('filename');

@dazza-dev
Copy link
Author

Hi @keithbrink, The issue has been resolved.

@keithbrink
Copy link
Contributor

Brilliant, thank you.

One more comment here, there could be some tweaks to improve the usefulness of the batches. The jobs are currently executed in a chain.

The order should be:

  1. QueueExport
  2. ...AppendQueryToSheet jobs, in any order
  3. CloseSheet

And the batch is being created something like this:

Bus::batch([
    [
        QueueExport
        ...AppendQueryToSheet (multiple)
        CloseSheet
   ]
]);

That makes it into a chain, so AppendQueryToSheet jobs cannot be run in parallel.

Using something like this would make them run in parallel:

$pending_batch = Bus::batch(...AppendQueryToSheet)->before(QueueExport)->then(CloseSheet)

However, it could be that AppendQueryToSheet cannot be run in parallel?

@patrickbrouwers
Copy link
Member

I think depending on the user's use-case it could run in parallel. Perhaps we should indicate with a concern or method if the batch should be execute in parallel or sequence.

@patrickbrouwers
Copy link
Member

Actually on second thought, that will give issues, as we re-open the file, it will overwrite itself when appending rows, I don't think that will work very well.
For imports it should be easier to run in parallel.

@dazza-dev
Copy link
Author

Hi @patrickbrouwers, I hope you're doing well. I wanted to check if the PR I submitted is ready to be merged, or if there are any additional changes or improvements you'd like me to make. If there's anything else that needs to be done, please let me know so I can complete it as soon as possible.

@POWRFULCOW89
Copy link

Excellent work @dazza-dev, I was in the middle of implementing my own batched chunks workaround and this fits my use case perfectly. I would love to see this merged soon. 🙌

@henryortiz8
Copy link

@patrickbrouwers I’ve integrated this into my project, and it functions excellently for exports and imports, when its going to be merged?

@dazza-dev
Copy link
Author

Hi @patrickbrouwers Please let me know if there’s anything else I need to adjust to get it merged. I’d be happy to make any necessary changes.

@patrickbrouwers
Copy link
Member

I still have to dive into this (currently on holiday), but haven't gotten around to it. Will let you know. Thanks for your work on this again.

@ZRktty
Copy link

ZRktty commented Nov 23, 2024

Hello @patrickbrouwers !
I was wondering if there has been any progress on this PR or if there’s anything blocking it from being merged.
Thanks, Zoltan

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.

6 participants