Skip to content

Commit

Permalink
improvement: support apis/resources for ash_oban
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Sep 15, 2023
1 parent 2d03e0f commit 8775a1c
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions lib/test.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
defmodule AshOban.Test do
@moduledoc "Helpers for testing ash_oban triggers"

def schedule_and_run_triggers(resource) do
triggers =
AshOban.Info.oban_triggers(resource)
@mix_app Mix.Project.config()[:app]

Enum.each(triggers, fn trigger ->
AshOban.schedule(resource, trigger)
end)

queues =
triggers
|> Enum.map(& &1.queue)
|> Enum.uniq()

# we drain each queue twice to do schedulers and then workers
Enum.reduce(queues ++ queues, %{}, fn queue, acc ->
[queue: queue]
|> Oban.drain_queue()
def schedule_and_run_triggers() do
@mix_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

def schedule_and_run_triggers(resource_or_api) do
if Spark.Dsl.is?(resource_or_api, Ash.Api) do
resource_or_api
|> 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)
else
triggers =
AshOban.Info.oban_triggers(resource_or_api)

Enum.each(triggers, fn trigger ->
AshOban.schedule(resource_or_api, trigger)
end)

queues =
triggers
|> Enum.map(& &1.queue)
|> Enum.uniq()

# 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)
end
end
end

0 comments on commit 8775a1c

Please sign in to comment.