improvement: support providing an otp app to schedule and run triggers

This commit is contained in:
Zach Daniel 2023-09-15 15:42:30 -04:00
parent 99a97b379d
commit 06c340569f

View file

@ -1,26 +1,10 @@
defmodule AshOban.Test do
@moduledoc "Helpers for testing ash_oban triggers"
@mix_app Mix.Project.config()[:app]
def schedule_and_run_triggers() do
@mix_app
|> IO.inspect()
|> Application.get_env(:ash_apis, [])
|> IO.inspect()
|> List.wrap()
|> Enum.reduce(%{}, fn api, acc ->
api
|> schedule_and_run_triggers()
|> Map.merge(acc, fn _key, left, right ->
left + right
end)
end)
end
def schedule_and_run_triggers(resource_or_api) do
if Spark.Dsl.is?(resource_or_api, Ash.Api) do
resource_or_api
def schedule_and_run_triggers(resource_or_api_or_otp_app) do
cond do
Spark.Dsl.is?(resource_or_api_or_otp_app, Ash.Api) ->
resource_or_api_or_otp_app
|> Ash.Api.Info.resources()
|> Enum.reduce(%{}, fn resource, acc ->
resource
@ -29,12 +13,13 @@ defmodule AshOban.Test do
left + right
end)
end)
else
Spark.Dsl.is?(resource_or_api_or_otp_app, Ash.Resource) ->
triggers =
AshOban.Info.oban_triggers(resource_or_api)
AshOban.Info.oban_triggers(resource_or_api_or_otp_app)
Enum.each(triggers, fn trigger ->
AshOban.schedule(resource_or_api, trigger)
AshOban.schedule(resource_or_api_or_otp_app, trigger)
end)
queues =
@ -50,6 +35,18 @@ defmodule AshOban.Test do
left + right
end)
end)
true ->
resource_or_api_or_otp_app
|> Application.get_env(:ash_apis, [])
|> List.wrap()
|> Enum.reduce(%{}, fn api, acc ->
api
|> schedule_and_run_triggers()
|> Map.merge(acc, fn _key, left, right ->
left + right
end)
end)
end
end
end