# Background jobs

Source: https://bogie-go.com/docs/jobs.html

Jobs are opt-in, but not a one-time choice: add River at `new` time with `--jobs`, or later, the moment you generate the first job.

```
bogie new blog --jobs
```

This runs [River](https://riverqueue.com/) on the same Postgres the app already has: no Redis, a transactional enqueue, typed args. It adds a worker role, `app/jobs`, and `g job`.

## Adding jobs to an app that has none

`bogie g job NAME` on an app made without `--jobs` adds jobs first, as if `--jobs` had been passed to `new`, then generates the job as usual:

```
bogie g job send_welcome
```

```
        note  this app has no app/jobs yet; adding --jobs
     updated  AGENTS.md
     updated  Dockerfile
     updated  Makefile
     updated  README.md
      merged  app/application.go
        kept  app/controllers/application.go
        kept  app/controllers/routes.go
      create  app/jobs/jobs.go
      create  app/jobs/jobs_suite_test.go
      create  app/jobs/jobs_test.go
     updated  app/services/migrate/migrate.go
     updated  bin/ci
     updated  config/deploy.production.yml
     updated  config/deploy.yml
     updated  db/adapter/store.go
        kept  db/seeds/seeds.go
      merged  main.go
     updated  test/testapp/testapp.go
     updated  test/testdb/testdb.go
      insert  main.go: "worker": runWorker,
      insert  app/application.go: import "blog/app/jobs"
     updated  bogie.toml: jobs = true
         run  go mod tidy
      create  app/jobs/send_welcome.go
      create  app/jobs/send_welcome_test.go
      insert  app/jobs/jobs.go: registered += add(workers, &SendWelcomeWorker{Log: log})
```

It is the same trick as `bogie app:update`: render the app twice, once without jobs and once with, and merge the difference in. A file you have already edited merges rather than being overwritten; a real overlap leaves conflict markers, the same as `app:update`. Two refusals guard it, for the same reasons `app:update` has them:

- A dirty git tree, since this touches several files in one diff:

  ```
  bogie: generate job: enable jobs: the git tree has uncommitted changes; commit or stash them, since this touches several files in one diff
  ```
- An app not yet on this Bogie's version, so the merge's base is rendered from the right templates and no unrelated template change shows up as noise in what should be a jobs-only diff:

  ```
  bogie: generate job: enable jobs: this app is at bogie v0.5.0 and this bogie is v0.5.5; run `bogie app:update` first
  ```

Once jobs are on, every later `g job` just writes the job and registers it, the same as an app made with `--jobs` from the start.

To see what a first job would do without changing the app, add `--pretend`. Since v0.6.3 it writes nothing at all, not even `app/jobs`, and says so:

```
bogie g job send_welcome --pretend
        note  this app has no app/jobs yet; --jobs would be added first
      create  app/jobs/send_welcome.go
      create  app/jobs/send_welcome_test.go
      insert  app/jobs/jobs.go: registered += add(workers, &SendWelcomeWorker{Log: log})
```

## Writing one

```
bogie g job send_welcome
```

```
      create  app/jobs/send_welcome.go
      create  app/jobs/send_welcome_test.go
      insert  app/jobs/jobs.go: registered += add(workers, &SendWelcomeWorker{Log: log})
```

```
type SendWelcomeArgs struct {
	// ID string `json:"id"`
}

func (SendWelcomeArgs) Kind() string { return "send_welcome" }

type SendWelcomeWorker struct {
	river.WorkerDefaults[SendWelcomeArgs]
	Log *slog.Logger
}

func (w *SendWelcomeWorker) Work(ctx context.Context, job *river.Job[SendWelcomeArgs]) error {
	w.Log.InfoContext(ctx, "send_welcome: not implemented yet", "job", job.ID)
	return nil
}
```

An `Args` type with a `Kind`, and a `Worker` with `Work(ctx, job)`. Args are stored as JSON in the job row, so keep them to ids and small values; the worker loads whatever else it needs. Never change `Kind` once jobs of it have been queued: a queued job of the old kind would have no worker to run it. Dependencies go on the worker as fields, the smallest interface it needs (rule 2), passed on the registration line.

## The marker

The generator registers the worker above `// bogie:jobs` in `app/jobs/jobs.go`, the same idempotent, checked-before-written insertion every generator uses:

```
registered += add(workers, &SendWelcomeWorker{Log: log})
// bogie:jobs
```

## Periodic jobs

Since v0.7.0, `--every` also registers the job as a River periodic job on a cron schedule:

```
bogie g job daily_digest --every "0 9 * * *"
```

```
      create  app/jobs/daily_digest.go
      create  app/jobs/daily_digest_test.go
      insert  app/jobs/jobs.go: registered += add(workers, &DailyDigestWorker{Log: log})
      insert  app/jobs/jobs.go: periodic = append(periodic, DailyDigestPeriodic())
         run  go get github.com/robfig/cron/v3@v3.0.1
```

Without `--every`, `g job` still writes the worker and the same one registration line as before. The schedule is robfig/cron's `ParseStandard`: five fields (minute, hour, day of month, month, day of week), a descriptor such as `@daily` or `@every 1h`, and an optional `CRON_TZ=UTC` prefix. With no prefix it is the process's local time. `0 9 * * *` is 09:00 every day. Generated apps that use `--every` gain `github.com/robfig/cron/v3`.

The generator writes a `DailyDigestPeriodic` helper in the job file and a second line above `// bogie:jobs`. `jobs.Workers` returns that slice, and `app/application.go` sets `river.Config.PeriodicJobs`:

```
registered += add(workers, &DailyDigestWorker{Log: log})
periodic = append(periodic, DailyDigestPeriodic())
// bogie:jobs
```

```
func DailyDigestPeriodic() *river.PeriodicJob {
	schedule, err := cron.ParseStandard("0 9 * * *")
	if err != nil {
		// bogie g job only writes an expression ParseStandard accepts.
		panic(err)
	}
	return river.NewPeriodicJob(
		schedule,
		func() (river.JobArgs, *river.InsertOpts) {
			return DailyDigestArgs{}, nil
		},
		&river.PeriodicJobOpts{RunOnStart: false},
	)
}
```

`RunOnStart` is off, so a deploy or a new leader does not enqueue one immediately. Enqueue one by hand the same way as any other job, with `Insert`. River elects one leader per database and schema, and only that client inserts periodic jobs, so several worker processes do not each enqueue a copy. The web process never starts its River client, so it does not lead. The leader holds the schedule in memory, so after a restart the new leader starts from the current time and a run can be skipped.

An app generated before v0.7.0 has no periodic slice for the schedule line to append to. `--every` then stops, before any write, and says to run `bogie app:update`. See [Upgrading an app](https://bogie-go.com/docs/upgrading.html#v0-7).

`--pretend` with `--every` prints the create and insert lines, including `periodic = append(...)`, and writes nothing: no files, no registration edit, no `go get`.

`bogie d job daily_digest` removes the schedule line with the worker.

## Enqueuing

From anywhere that holds the application, including inside a transaction so the job commits with the row it depends on or not at all:

```
app.Jobs.Insert(ctx, jobs.SendWelcomeArgs{}, nil)

// or, so the enqueue commits with the row:
app.Jobs.InsertTx(ctx, tx, jobs.SendWelcomeArgs{}, nil)
```

## Running the worker

```
bogie worker
```

Or `make worker`. It is its own process, separate from `bogie server`, so a backlog in the queue never slows a request. In development `bogie worker` runs under Air and builds to its own binary, so it rebuilds and restarts on save independently of the server; `make worker` is one boot, no reload. River's own schema (the tables it needs to track jobs) is applied by `migrate`, through `rivermigrate`, not copied into `db/migrate`: it arrives the same way the app's own migrations do, at boot or with `bogie db:migrate`.

The first `bogie db:migrate` after jobs are added applies River's schema before your own:

```
time=2026-11-05T08:59:34.576+07:00 level=INFO msg="river schema applied" service=blog versions=8
time=2026-11-05T08:59:34.584+07:00 level=INFO msg="schema up to date" service=blog version=20261105015917
```

With the worker running, these two lines say it is up (the debug lines between them are trimmed, and the client id differs on every run):

```
time=2026-11-05T08:59:36.115+07:00 level=INFO msg="worker started" service=blog env=development
time=2026-11-05T08:59:36.115+07:00 level=INFO msg="River client started" service=blog client_id=...
```

## Removing one

```
bogie d job send_welcome
```

Removes the file and the registration line. If the job was registered with `--every`, the periodic line is removed with it; any other job's lines are left alone.
