Shopify / job-iteration

Makes your background jobs interruptible and resumable by design.
https://www.rubydoc.info/gems/job-iteration
MIT License
1.14k stars 43 forks source link

How to return early or finish a job without further iterations and retries? #491

Closed adamzapasnik closed 1 month ago

adamzapasnik commented 3 months ago

I tried to find it in the docs but couldn't.

Let's say you have a job that iterates over N elements and midway you decided that you don't want to iterate over the remaining items and want to exit the job successfully (without retries). How to achieve this, I couldn't find it in the docs but it seems like someone must have already faced this problem before.

def each_iteration(items)
  if smth
    do_smth_with_items
  else
    end_this_job
  end
end
bdewater commented 3 months ago

throw(:abort) should do the trick: https://github.com/Shopify/job-iteration/blob/ae5828f1ecfaa45555593b4694dffb699629e7fd/lib/job-iteration/iteration.rb#L155-L157

ThrottleEnumerator uses it for example, and throwing abort without additional data is unit tested.

adamzapasnik commented 3 months ago

I see @bdewater it's very simple, thanks. Does it make sense to document this, cause it's a bit hidden and even obscure if someone doesn't know of throw and catch - which are rarely used 🤔

fatkodima commented 2 months ago

I think, PR is welcome.