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

Stream passed to Oban.insert_all is materialized twice? #1189

Closed
michallepicki opened this issue Nov 20, 2024 · 1 comment
Closed

Stream passed to Oban.insert_all is materialized twice? #1189

michallepicki opened this issue Nov 20, 2024 · 1 comment
Labels
area:pro Related to Oban Pro closed:duplicate This is a duplicate issue kind:bug Something isn't working

Comments

@michallepicki
Copy link
Contributor

Environment

  • Oban Version - 2.18.3 , oban_pro 1.4.13
  • PostgreSQL Version - 14.13
  • Elixir & Erlang/OTP Versions - Elixir 1.17.2, Erlang 27.0.1
  • ecto - 3.12.4
  • ecto_sql - 3.12.1
  • postgrex - 0.19.2

Current Behavior

Hi! When optimizing some issues we encountered with passing a big list of jobs to Oban.insert_all, we thought of using a Stream instead. Oban documentation has this example:

(fn -> MyApp.Worker.new(%{}))
|> Stream.repeatedly()
|> Stream.take(100)
|> Oban.insert_all()

Stream.take returns a lazy Stream, so it suggests that Oban should work nicely all streams here.

But when reading the oban_pro code I noticed a pattern that suggests the stream would get executed twice. Simplifying a bit, here is roughly the pattern I saw, with some inspect logging added:

Repo.transaction(fn ->
  s = Repo.stream(User)  |> Stream.map(fn x -> IO.inspect(x.id); x end)
  
  # Oban.insert_all(s) start
  
  Enum.reduce(s, 0, fn u, a -> a + 1 end)
  Enum.map(s, fn u -> u.id end)
  
  # Oban.insert_all(s) end
end)

Expected Behavior

Ideally Oban.insert_all wouldn't run the stream passed to it twice. If that's not possible, I would expect this to be documented as a warning.

And I'm sorry if I mis-read the code! 🙏🏻

@sorentwo
Copy link
Member

Job insertion is largely rewritten in Pro v1.5 and no longer iterates through the stream multiple times. However, Stream support is provided as a convenience more than an optimization (internally it uses Enum rather than Stream to iterate and the full stream)

@sorentwo sorentwo added kind:bug Something isn't working area:pro Related to Oban Pro closed:duplicate This is a duplicate issue labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:pro Related to Oban Pro closed:duplicate This is a duplicate issue kind:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants