1.9 KiB · text History bbb2a48
# Vigil's own Oban cron jobs, reported through sentry-elixir's Oban cron
# integration, unchanged: the integration listens to Oban's job telemetry,
# so this emits the events Oban emits for a cron job (a job whose meta has
# "cron" and "cron_expr", as Oban's Cron plugin inserts them) without
# running Oban itself. One success (the partition maintenance) and one
# failure (the alert evaluation). Run by VigilWeb.SdkCronConformanceTest;
# the DSN comes from SENTRY_DSN.
Mix.install(
  [
    {:sentry, path: System.fetch_env!("SENTRY_ELIXIR_PATH")},
    {:jason, "~> 1.4"},
    {:finch, "~> 0.21"},
    {:oban, "~> 2.20"}
  ],
  config: [
    sentry: [
      environment_name: "test",
      release: "vigil@0.1.0",
      integrations: [oban: [cron: [enabled: true]]]
    ]
  ]
)

run = fn id, worker, cron_expr, outcome ->
  # struct!/2, not %Oban.Job{}: the script is compiled before Mix.install
  # fetches Oban.
  job =
    struct!(Oban.Job,
      id: id,
      worker: worker,
      queue: "maintenance",
      args: %{},
      attempt: 1,
      max_attempts: 1,
      meta: %{"cron" => true, "cron_expr" => cron_expr, "cron_name" => worker}
    )

  :telemetry.execute([:oban, :job, :start], %{system_time: System.system_time()}, %{job: job})
  Process.sleep(20)
  duration = System.convert_time_unit(20, :millisecond, :native)

  case outcome do
    :success ->
      :telemetry.execute([:oban, :job, :stop], %{duration: duration}, %{job: job, state: :success})

    :exception ->
      :telemetry.execute([:oban, :job, :exception], %{duration: duration}, %{
        job: job,
        state: :failure,
        kind: :error,
        reason: %RuntimeError{message: "evaluation failed"},
        stacktrace: []
      })
  end
end

run.(101, "Vigil.Partitions.MaintainWorker", "17 3 * * *", :success)
run.(102, "Vigil.Alerts.EvaluateWorker", "* * * * *", :exception)

Sentry.flush(timeout: 10_000)
IO.puts("done")