2023-08-05 08:40:21 +12:00
|
|
|
defmodule AshOban.Test do
|
|
|
|
@moduledoc "Helpers for testing ash_oban triggers"
|
|
|
|
|
2023-09-16 07:42:30 +12:00
|
|
|
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
|
|
|
|
|> schedule_and_run_triggers()
|
|
|
|
|> Map.merge(acc, fn _key, left, right ->
|
|
|
|
left + right
|
|
|
|
end)
|
|
|
|
end)
|
2023-08-05 08:40:21 +12:00
|
|
|
|
2023-09-16 07:42:30 +12:00
|
|
|
Spark.Dsl.is?(resource_or_api_or_otp_app, Ash.Resource) ->
|
|
|
|
triggers =
|
2023-09-16 07:48:33 +12:00
|
|
|
resource_or_api_or_otp_app
|
|
|
|
|> AshOban.Info.oban_triggers()
|
|
|
|
|> Enum.filter(fn trigger ->
|
|
|
|
trigger.scheduler
|
|
|
|
end)
|
2023-09-16 07:32:47 +12:00
|
|
|
|
2023-09-16 07:42:30 +12:00
|
|
|
Enum.each(triggers, fn trigger ->
|
|
|
|
AshOban.schedule(resource_or_api_or_otp_app, trigger)
|
2023-09-16 07:32:47 +12:00
|
|
|
end)
|
|
|
|
|
2023-09-16 07:42:30 +12:00
|
|
|
queues =
|
|
|
|
triggers
|
|
|
|
|> Enum.map(& &1.queue)
|
|
|
|
|> Enum.uniq()
|
2023-09-16 07:32:47 +12:00
|
|
|
|
2023-09-16 07:42:30 +12:00
|
|
|
# we drain each queue twice to do schedulers and then workers
|
|
|
|
Enum.reduce(queues ++ queues, %{}, fn queue, acc ->
|
|
|
|
[queue: queue]
|
|
|
|
|> Oban.drain_queue()
|
|
|
|
|> Map.merge(acc, fn _key, left, right ->
|
|
|
|
left + right
|
|
|
|
end)
|
|
|
|
end)
|
2023-09-16 07:32:47 +12:00
|
|
|
|
2023-09-16 07:42:30 +12:00
|
|
|
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)
|
2023-09-16 07:32:47 +12:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2023-08-05 08:40:21 +12:00
|
|
|
end
|