defmodule Oban.Pro.Flusher do
  @moduledoc false

  alias Oban.{Config, Job}

  @doc """
  Construct an MFA for flushing accumulated operations after the Smart engine transaction commits.

  By convention, the function should be named `:on_flush`.
  """
  @callback to_flush_mfa(Job.t(), Config.t()) :: :ignore | mfa()

  @doc """
  Extract flush handlers from all registered flushing modules.

  The flush order is important. Workflow flushing must complete before Batch flushing to ensure
  batch wrapped workflow handlers are triggered.
  """
  def get_flush_handlers(job, conf) do
    [Oban.Pro.Workflow, Oban.Pro.Batch, Oban.Pro.Stages.Chain]
    |> Enum.map(fn handler -> handler.to_flush_mfa(job, conf) end)
    |> Enum.reject(&(&1 == :ignore))
  end
end
