Skip to content

Task Scheduling ​

The scheduler decides when a task runs. Add a schedule to the App from Save and List Photos that checks the saved photo count every five seconds. Use this short interval to see a run locally.

When to Use Scheduler ​

Use an interval for elapsed time and a cron or calendar schedule for work tied to a time of day. Give each schedule a stable name and have it call the service that owns the work.

Default Recommendation ​

Call a service directly for short recurring work. When work needs a retry policy or an independently scaled worker, have the schedule dispatch a job. Use Events versus Queues to choose where work begins.

Generate a Schedule ​

Add scheduler to the existing render.components list in .goforj.yml. Stop any running forj dev session, then run these commands from the Project root.

bash
forj render &&
  forj make:schedule photos:audit --every 5s --no-open

GoForj writes internal/photos/audit_schedule.go and registers photos.NewAuditSchedule in app/wire/inject_schedules_app.go. The App's app/schedules.go registry receives it through AppSchedules. The generator uses a 1h interval when you omit --every. See make:schedule for the generated files, options, and removal behavior.

In internal/photos/audit_schedule.go, add "log" to the imports and replace the struct, constructor, and handler. The generated Name and Interval methods supply the schedule name and the five-second interval.

go
// AuditSchedule records the saved photo count at each run.
type AuditSchedule struct {
	service *Service
}

// NewAuditSchedule receives the App's photo service.
func NewAuditSchedule(service *Service) *AuditSchedule {
	return &AuditSchedule{service: service}
}

// Handle checks the saved rows and records how many are visible.
func (s *AuditSchedule) Handle(ctx context.Context) error {
	photos, err := s.service.ListNewest(ctx)
	if err != nil {
		return err
	}
	log.Printf("photos:audit found %d saved photos", len(photos))
	return nil
}

Wire supplies *Service from the provider you added in Save Photo Metadata.

Naming Schedules ​

Choose a stable name that describes the action, such as photos:audit or sessions:cleanup. A name like reports:daily can describe the cadence when that distinction matters. See Naming Conventions.

Start Scheduler ​

With forj dev stopped, build and run the scheduler directly.

bash
forj build &&
  forj scheduler

Wait for Scheduler started and a due run. If the SQLite database contains one saved photo, the App logs photos:audit found 1 saved photos. Press Ctrl+C after observing the run. Use a longer interval for ongoing local work.

Use forj admin scheduler to select an additional App, or forj app to run enabled roles together in the default App. See Local Development for running the combined App under the watcher.

Use a generated schedule for one interval. For cron expressions, calendar times, or several schedules that call the same workflow, add separate named entries in app/schedules.go. Keep the call that registers AppSchedules so generated schedules are registered as well. Scheduler covers the fluent API and calendar helpers.

Cron and calendar schedules follow the App timezone configured through APP_TZ.

Observability ​

Check the schedule name and outcome in logs. Metrics, Inspects, and Lighthouse can provide more detail when enabled. For a focused test, call Interval and Handle directly and assert the cadence, delegated work, and error. Add an App-level check when you need to verify schedule registration.

Production ​

One scheduler process can own recurring work. When runs can overlap in that process, configure WithoutOverlapping(). When separate scheduler processes need to coordinate, configure WithoutOverlappingWithLocker(...) with a shared locker or choose one scheduler owner.

Scheduler Processes covers the supervised command, metrics port, and a safe due run and shutdown check.

Next Steps ​

  • Jobs covers dispatch and retries when recurring work needs independent execution.
  • Scheduler covers standalone scheduling and locking options.