improvement: support Oban.Pro.Testing.drain_jobs

This commit is contained in:
Zach Daniel 2024-02-26 10:31:28 -05:00
parent b745d5ca9b
commit 758784277b
2 changed files with 25 additions and 1 deletions

View file

@ -6,6 +6,8 @@ config :spark, :formatter,
"Ash.Registry": [], "Ash.Registry": [],
"Ash.Resource": [] "Ash.Resource": []
config :ash_oban, test: true
if Mix.env() == :test do if Mix.env() == :test do
config :ash_oban, ecto_repos: [AshOban.Test.Repo] config :ash_oban, ecto_repos: [AshOban.Test.Repo]

View file

@ -1,6 +1,8 @@
defmodule AshOban do defmodule AshOban do
require Logger require Logger
@pro Application.compile_env(:ash_oban, :pro?) || false
defmodule Trigger do defmodule Trigger do
@moduledoc """ @moduledoc """
A configured trigger. A configured trigger.
@ -784,7 +786,7 @@ defmodule AshOban do
:with_scheduled :with_scheduled
]) ])
) )
|> Oban.drain_queue() |> drain_queue()
|> Map.put(:queues_not_drained, []) |> Map.put(:queues_not_drained, [])
|> merge_results(acc) |> merge_results(acc)
end) end)
@ -794,6 +796,26 @@ defmodule AshOban do
end end
end end
if @pro do
defp drain_queue(opts) do
Oban.Pro.Testing.drain_jobs(opts)
end
else
if Application.compile_env(:ash_oban, :test) || Mix.env() == :test do
defp drain_queue(opts) do
Oban.drain_queue(opts)
end
else
defp drain_queue(_opts) do
raise ArgumentError, """
Cannot use the `drain_queues?: true` option outside of the test environment, unless you are also using oban pro.
For more information, see this github issue: https://github.com/sorentwo/oban/issues/1037#issuecomment-1962928460
"""
end
end
end
defp default_acc do defp default_acc do
%{ %{
discard: 0, discard: 0,