defmodule Tarakan.Infestations.RefreshPattern do
  @moduledoc "Full recompute of one infestation pattern_key into rollup tables."

  use Oban.Worker,
    queue: :infestations,
    max_attempts: 5,
    unique: [
      period: 30,
      fields: [:args, :worker],
      keys: [:pattern_key],
      states: :incomplete
    ]

  alias Tarakan.Infestations

  @impl Oban.Worker
  def perform(%Oban.Job{args: %{"pattern_key" => pattern_key}})
      when is_binary(pattern_key) and pattern_key != "" do
    Infestations.refresh_pattern!(pattern_key)
    :ok
  end

  def perform(%Oban.Job{args: %{pattern_key: pattern_key}})
      when is_binary(pattern_key) and pattern_key != "" do
    Infestations.refresh_pattern!(pattern_key)
    :ok
  end

  def perform(_job), do: :ok
end
