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

Fix :get_dynamic_repo with releases #1233

Merged
merged 7 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 oban_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, :oban_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
4 changes: 2 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,7 @@ 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}, :mfa]},
insert_trigger: :boolean,
log: {:enum, @log_levels},
name: :any,
Expand Down
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
9 changes: 9 additions & 0 deletions lib/oban/validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ defmodule Oban.Validation do
end
end

defp validate_type(:mfa, key, {module, func, args})
when is_atom(module) and is_atom(func) and is_list(args) do
if function_exported?(module, func, length(args)) do
:ok
else
{:error, "missing function #{Exception.format_mfa(module, func, length(args))} for #{key}"}
end
end

defp validate_type(:schedule, key, val) do
case Expression.parse(val) do
{:ok, _cron} ->
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
Loading