edozi.ai@edozi.ai
BEGINNER BUILD · 20 min

Run a scheduled agent

Use a short-lived scheduled job for research scans, briefs, reports, and other periodic work.

Use this for periodic work

A scheduled job is usually the safest first version of “always on.” It has a clear start and finish, a natural timeout, and no idle process to babysit.

1. Create a one-run entry point

Your script should reserve at most one due job or one bounded batch. It must persist completion before exiting.

agent/run-once.mjs
const job = await reserveDueJob();
if (!job) process.exit(0);

try {
  const result = await runAgent({
    jobId: job.id,
    input: job.input,
    deadlineMs: 10 * 60_000,
    requireApprovalFor: ["publish", "send", "spend", "delete"],
  });
  await completeJob(job.id, result);
} catch (error) {
  await failJob(job.id, { retryable: isRetryable(error) });
  throw error;
}

2. Add a scheduler

This GitHub Actions example runs hourly at an offset minute. For tighter latency or private networking, use your cloud platform’s cron service with the same one-run entry point.

.github/workflows/scheduled-agent.yml
name: scheduled-agent
on:
  schedule:
    - cron: "17 * * * *"
  workflow_dispatch:
concurrency:
  group: scheduled-agent
  cancel-in-progress: false
jobs:
  run:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: node agent/run-once.mjs
        env:
          AGENT_API_KEY: ${{ secrets.AGENT_API_KEY }}
          DATABASE_URL: ${{ secrets.DATABASE_URL }}

3. Add state before frequency

  • Use a unique idempotency key for the logical job.
  • Reserve work atomically so overlapping runs cannot claim it twice.
  • Store the source cursor or last successful watermark.
  • Cap attempts and move exhausted jobs to a review queue.
  • Alert after consecutive failures, not on every transient retry.

Acceptance test

  1. Trigger it manually with a harmless test job.
  2. Trigger it twice and confirm only one result exists.
  3. Force a timeout and confirm the lease expires or the job becomes retryable.
  4. Remove a secret and confirm the run fails closed without partial external action.

Continue building

All tutorials ↗