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

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

Closed
adamzapasnik opened this issue Jul 26, 2024 · 3 comments · Fixed by #506
Closed

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

adamzapasnik opened this issue Jul 26, 2024 · 3 comments · Fixed by #506

Comments

@adamzapasnik
Copy link
Contributor

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
Copy link
Contributor

throw(:abort) should do the trick:

completed = catch(:abort) do
iterate_with_enumerator(enumerator, arguments)
end

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

@adamzapasnik
Copy link
Contributor Author

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
Copy link
Contributor

I think, PR is welcome.

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 a pull request may close this issue.

3 participants