# `ADK.Oban.ScheduledJob`
[🔗](https://github.com/zeroasterisk/adk-elixir/blob/main/lib/adk/oban/scheduled_job.ex#L2)

Oban worker for recurring/scheduled agent runs via Oban cron.

A thin convenience wrapper around `ADK.Oban.AgentWorker` that defaults to
the `:scheduled` queue and is designed for use with the Oban cron plugin.

## One-Shot Delayed Scheduling

    # Schedule an agent to run in 60 seconds
    ADK.Oban.ScheduledJob.schedule(MyApp.Agents.Cleanup, schedule_in: 60)

    # Schedule with inline agent config
    ADK.Oban.ScheduledJob.schedule(
      %{
        "agent_name" => "cleanup",
        "model" => "gemini-flash-latest",
        "instruction" => "You are a cleanup agent.",
        "message" => "Run cleanup now",
        "user_id" => "system"
      },
      schedule_in: 3600
    )

## Recurring via Oban Cron Plugin

    # config/config.exs
    config :my_app, Oban,
      repo: MyApp.Repo,
      queues: [scheduled: 5],
      plugins: [
        {Oban.Plugins.Cron,
         crontab: [
           # Run cleanup agent every day at midnight
           {"0 0 * * *", ADK.Oban.ScheduledJob,
            args: %{
              "agent_module" => "MyApp.Agents.Cleanup",
              "app_name" => "my_app",
              "user_id" => "system",
              "message" => "Run daily cleanup"
            }},
           # Run monitoring agent every hour
           {"0 * * * *", ADK.Oban.ScheduledJob,
            args: %{
              "agent_module" => "MyApp.Agents.Monitor",
              "app_name" => "my_app",
              "user_id" => "system",
              "message" => "Run hourly monitoring check"
            }}
         ]}
      ]

## Agent Module

Define an agent module that exports `agent/0`:

    defmodule MyApp.Agents.Cleanup do
      def agent do
        ADK.Agent.LlmAgent.new(
          name: "cleanup",
          model: "gemini-flash-latest",
          instruction: "You are a cleanup agent. Remove stale data and free resources."
        )
      end
    end

## Inline Agent Config

Alternatively, pass agent config directly in args:

    %{
      "agent_name" => "monitor",
      "model" => "gemini-flash-latest",
      "instruction" => "You are a monitoring agent.",
      "message" => "Run health check",
      "user_id" => "system"
    }
    |> ADK.Oban.ScheduledJob.new()
    |> Oban.insert()

## Telemetry

Emits the following events on job execution:

  * `[:adk, :scheduled_job, :start]` — before agent run, metadata includes `args`
  * `[:adk, :scheduled_job, :stop]` — after agent run, measurements include `duration`

# `schedule`

```elixir
@spec schedule(
  module() | map(),
  keyword()
) :: Ecto.Changeset.t()
```

Build an Oban changeset for a scheduled agent job.

The first argument can be:
  - A module atom (e.g. `MyApp.Agents.Cleanup`) — resolves via `agent_module`
  - A map of inline agent config args

## Options

  * `:schedule_in` - seconds from now to schedule the job
  * `:scheduled_at` - exact `DateTime` for scheduling
  * `:queue` - Oban queue (default: `:scheduled`)
  * `:max_attempts` - max retry count (default: 3)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
