-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: support apis/resources for ash_oban
- Loading branch information
1 parent
2d03e0f
commit 8775a1c
Showing
1 changed file
with
43 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |