Skip to content

Commit

Permalink
Fix :get_dynamic_repo with releases
Browse files Browse the repository at this point in the history
Lave Ducia here, long-time user, first-time contributor!

It seems that the recommendation for a [separate Oban pool](https://hexdocs.pm/oban/scaling.html#pooling) does not work
with OTP releases, as anonymous functions cannot be used in
configuration.

This PR allows an MFA tuple to be passed instead, and updates the
documentation to recommend this instead.
  • Loading branch information
davydog187 committed Jan 31, 2025
1 parent e0915f0 commit b6b0f3c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
20 changes: 19 additions & 1 deletion guides/advanced/scaling.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,32 @@ defmodule MyApp.ObanRepo do
end
```

Then add a function to your main Repo

```diff
defmodule MyApp.Repo do
use Ecto.Repo,
adapter: Ecto.Adapters.Postgres,
otp_app: :my_app

+ def use_dynamic_repo? do
+ if in_transaction?() do
+ MyApp.Repo
+ else
+ MyApp.ObanRepo
+ end
+ end
end
```

Then switch the configured `repo`, and use `get_dynamic_repo` to ensure the same repo is used
within a transaction:

```diff
config :my_app, Oban,
- repo: MyApp.Repo,
+ repo: MyApp.ObanRepo,
+ get_dynamic_repo: fn -> if MyApp.Repo.in_transaction?(), do: MyApp.Repo, else: MyApp.ObanRepo end
+ get_dynamic_repo: {MyApp.Repo, :use_dynamic_repo?, []}
...
```

Expand Down
2 changes: 1 addition & 1 deletion lib/oban.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Oban do
@type option ::
{:dispatch_cooldown, pos_integer()}
| {:engine, module()}
| {:get_dynamic_repo, nil | (-> pid() | atom())}
| {:get_dynamic_repo, nil | (-> pid() | atom()) | {module(), atom(), list()}}
| {:log, false | Logger.level()}
| {:name, name()}
| {:node, oban_node()}
Expand Down
15 changes: 13 additions & 2 deletions lib/oban/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Oban.Config do
@type t :: %__MODULE__{
dispatch_cooldown: pos_integer(),
engine: module(),
get_dynamic_repo: nil | (-> pid() | atom()),
get_dynamic_repo: nil | (-> pid() | atom()) | {module(), atom(), list()},
insert_trigger: boolean(),
log: false | Logger.level(),
name: Oban.name(),
Expand Down Expand Up @@ -140,7 +140,8 @@ defmodule Oban.Config do
Validation.validate_schema(opts,
dispatch_cooldown: :pos_integer,
engine: {:behaviour, Oban.Engine},
get_dynamic_repo: {:or, [:falsy, {:function, 0}]},
get_dynamic_repo:
{:or, [:falsy, {:function, 0}, {:custom, &validate_mfa(:get_dynamic_repo, &1)}]},
insert_trigger: :boolean,
log: {:enum, @log_levels},
name: :any,
Expand Down Expand Up @@ -384,4 +385,14 @@ defmodule Oban.Config do
end

defp normalize_plugins(plugins), do: plugins || []

defp validate_mfa(key, {module, function, args})
when is_atom(module) and is_atom(function) and is_list(args) do
if function_exported?(module, function, length(args)) do
:ok
else
{:error,
"expected #{inspect(Exception.format_mfa(module, function, length(args)))} to exist for #{key} but it does not exist"}
end
end
end
11 changes: 9 additions & 2 deletions lib/oban/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,19 @@ defmodule Oban.Repo do
with_dynamic_repo(conf, name, args ++ [opts])
end

defp with_dynamic_repo(%{get_dynamic_repo: fun} = conf, name, args) when is_function(fun, 0) do
defp with_dynamic_repo(%{get_dynamic_repo: fun} = conf, name, args)
when is_function(fun, 0) or is_tuple(fun) do
prev_instance = conf.repo.get_dynamic_repo()

dynamic_repo =
case fun do
{module, func, args} -> apply(module, func, args)
fun -> fun.()
end

try do
if not in_transaction?(conf, prev_instance) do
conf.repo.put_dynamic_repo(fun.())
conf.repo.put_dynamic_repo(dynamic_repo)
end

apply(conf.repo, name, args)
Expand Down
18 changes: 17 additions & 1 deletion test/oban/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ defmodule Oban.RepoTest do

@moduletag :unboxed

test "querying with a dynamic repo" do
test "querying with a dynamic repo (MFA)" do
{:ok, repo_pid} = start_supervised({DynamicRepo, name: nil})

DynamicRepo.put_dynamic_repo(nil)

name =
start_supervised_oban!(
get_dynamic_repo: {DynamicRepo, :use_dynamic_repo, [repo_pid]},
repo: DynamicRepo
)

conf = Oban.config(name)

Oban.Repo.insert!(conf, Worker.new(%{ref: 1, action: "OK"}))
end

test "querying with a dynamic repo (anonymous function)" do
{:ok, repo_pid} = start_supervised({DynamicRepo, name: nil})

DynamicRepo.put_dynamic_repo(nil)
Expand Down
4 changes: 4 additions & 0 deletions test/support/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ defmodule Oban.Test.DynamicRepo do
otp_app: :oban,
adapter: Ecto.Adapters.Postgres

def use_dynamic_repo(pid) do
pid
end

def init(_, _) do
{:ok, Oban.Test.Repo.config()}
end
Expand Down

0 comments on commit b6b0f3c

Please sign in to comment.