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

Implement GoodJob::Batch #712

Merged
merged 8 commits into from
Feb 4, 2023
Merged

Implement GoodJob::Batch #712

merged 8 commits into from
Feb 4, 2023

Conversation

bensheldon
Copy link
Owner

@bensheldon bensheldon commented Sep 12, 2022

Connects to #417, #691.

  • Add description field
  • Add Dashboard view
  • Add cleanup for batches
  • Rename batch params to properties
  • Make migration-less patch-release compatible

@bensheldon bensheldon temporarily deployed to goodjob-batches-tdd-bnm711tdg0 September 12, 2022 23:12 Inactive
@bensheldon bensheldon temporarily deployed to goodjob-batches-tdd-bnm711tdg0 September 17, 2022 15:59 Inactive
@bensheldon bensheldon temporarily deployed to goodjob-batches-tdd-bnm711tdg0 September 19, 2022 12:59 Inactive
@bensheldon bensheldon temporarily deployed to goodjob-batches-tdd-bnm711tdg0 September 19, 2022 13:50 Inactive
@bensheldon bensheldon requested a deployment to goodjob-batches-tdd-bnm711tdg0 September 19, 2022 13:55 Abandoned
@bensheldon bensheldon temporarily deployed to goodjob-batches-tdd-bnm711tdg0 September 19, 2022 14:10 Inactive
@bensheldon bensheldon temporarily deployed to goodjob-batches-tdd-bnm711tdg0 September 19, 2022 14:41 Inactive
@bensheldon bensheldon mentioned this pull request Oct 6, 2022
README.md Outdated
end
```

- Jobs can be added to an existing batch. Jobs in a batch are enqueued and performed immediately, though the final callback job will not be enqueued until `GoodJob::Batch#enqueue` is called.
Copy link

@mollerhoj mollerhoj Oct 7, 2022

Choose a reason for hiding this comment

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

I find this instruction unclear. To me, it looks like the code below contains a race condition. What happens if all the jobs finishes before enqueue is called?.

On another note: What happens if you call enqueue twice on the same batch?
What happens if you call enqueue on a batch without any jobs?

Copy link
Owner Author

@bensheldon bensheldon Oct 10, 2022

Choose a reason for hiding this comment

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

Great callout! I will cover that in the documentation. Briefly:

  • the callback job won't be enabled until #enqueue is called on the batch.
  • calling #enqueue on a batch without any jobs will immediately enqueue the callback job
  • #enqueue can be called multiple times, which will re-enable the callback job to be enqueued if it had previously been triggered because all jobs had finished.
  • #add will add jobs to the batch without triggering an enqueue

Do you think this kind of example makes sense for creating a multistage batch?

class MyBatchJob < ApplicationJob
  def perform(batch, options = {})
    if batch.properties[:stage] == 1
      batch.properties[:stage] = 2
      batch.enqueue do
        10.times { OtherJob.perform_later}
      end
    elsif batch.properties[:stage] == 2
      batch.properties[:stage] = 3
      batch.enqueue do
        OtherJob.perform_later
      end
    else
      # all done
    end 
  end
end

GoodJob::Batch.enqueue(MyBatchJob, stage: 1)

Copy link

@mollerhoj mollerhoj left a comment

Choose a reason for hiding this comment

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

I have not have time yet to go over the code in detail, but I've written some questions that popped up when reading the documentation.

All in all, this looks very, very good to me. Nice API, seems like a pretty clean implementation, and an absolute KILLER feature.

I think the documentation would be even better with an example of a complex workflow (using batches to run both serial and parallel jobs in succession.) E.g, show how one would implement this workflow:

workflow

README.md Outdated Show resolved Hide resolved
Implementation of GoodJob::Batch

Clean up thread globals as much as possible

Remove `jitter:` which is not broadly Rails compatible and fix renamed kwarg

Fix migration generator spec

Fix Rspec example_app directory

Update GoodJob::Batches Dashboard; rename finished/discarded

Add description; add static helper in batches extension, fix migration

Add batch#show screen

Add batch cleanup

Fix duplicate description key

Fix linting

Fix cleanup_preserved_jobs query

Allow compatibility without database migration

Ensure Batch#properties is updated on save

Add documentation
@bensheldon bensheldon changed the title Draft: Expeditionary Batch implementation Implement Batches Feb 4, 2023
@bensheldon bensheldon marked this pull request as ready for review February 4, 2023 18:20
@bensheldon bensheldon changed the title Implement Batches Implement GoodJob::Batch Feb 4, 2023
@bensheldon bensheldon merged commit 675647a into main Feb 4, 2023
@bensheldon bensheldon deleted the batches_tdd branch February 4, 2023 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging this pull request may close these issues.

2 participants