defmodule Microwaveprop.Workers.GridCachePruneWorker do
  @moduledoc """
  Prunes stale rows from the node-local `Microwaveprop.Weather.GridCache` ETS
  table. Without this cron the cache grew unbounded: every hourly
  `PropagationGridWorker` run plus peer `broadcast_put` added an entry per
  forecast hour (~92k points * 19 hours), so long-lived pods eventually OOM'd.

  Runs on the shared `:propagation` queue alongside `PropagationPruneWorker`
  at a lower priority so the hourly grid chain never waits behind it.
  """

  use Oban.Worker,
    queue: :propagation,
    priority: 5,
    max_attempts: 3,
    unique: [period: 300, states: [:available, :scheduled, :executing, :retryable]]

  alias Microwaveprop.Weather.GridCache

  @cutoff_hours 3

  @impl Oban.Worker
  def perform(%Oban.Job{}) do
    cutoff = DateTime.add(DateTime.utc_now(), -@cutoff_hours, :hour)
    _removed = GridCache.prune_older_than(cutoff)
    :ok
  end
end
