# Cron

A cron job is a TypeScript file that runs on a schedule.

The cron tool discovers jobs from all apps and runs them automatically.

## creating a cron job

Add a file to `cron/` in any app:

```ts
// apps/my-app/cron/daily-cleanup.ts
export const schedule = "day"

export default async function() {
  console.log("Running cleanup...")
}
```

That's it. The cron tool picks it up within 60 seconds.

## schedules

| value | when |
|-------|------|
| `1 minute` | every minute |
| `5 minutes` | every 5 minutes |
| `15 minutes` | every 15 minutes |
| `30 minutes` | every 30 minutes |
| `hour` | top of every hour |
| `noon` | 12:00 daily |
| `midnight` / `day` | 00:00 daily |
| `week` / `sunday` | 00:00 Sunday |
| `monday` - `saturday` | 00:00 that day |

Alternate forms work too: `"30minutes"`, `30`, `"30 minutes"`.

## environment

Jobs run with the app's working directory and inherit all env vars.

- `APPS_DIR` - path to `/apps` directory

## ui

The cron tool shows:
- Job name (`app/job`)
- Schedule
- Last run time
- Next run time
- Run Now button

Click **New Job** to create one from the UI.

## manual runs

Hit the **Run Now** button or POST to the tool:

```bash
curl -X POST http://localhost:3001/run/my-app/daily-cleanup
```

## job state

Jobs track:
- `idle` / `running` / `disabled`
- Last run timestamp
- Last duration
- Last exit code
- Last error (if any)
- Next scheduled run

## discovery

The cron tool:
1. Scans `APPS_DIR/*/cron/*.ts`
2. Imports each file to read `schedule`
3. Validates the schedule
4. Registers with croner

Re-scans every 60 seconds to pick up new/changed/removed jobs.
