App Lifecycle
The same App lifecycle applies to short-lived commands and long-running servers. route:list prints its table and exits. api serves requests until the server stops.
The Runtime Path
- Load settings. The launcher reads the environment and applies the App timezone.
- Construct the App. Wire supplies its dependencies. Root help and scaffolding finish before this step.
- Run the command. The App runs startup hooks, then executes the command.
- Shut down. When the command finishes, the App runs shutdown hooks and releases its dependencies.
The HTTP listener opens when the command runs. Check readiness before sending traffic.
Add an App-Owned Hook
Use app/lifecycle.go for process-wide resources your App introduces and manages. Built-in managers already own their setup and cleanup. Keep LifecycleRegistry.Register and edit the phase methods you need. Constructor dependencies for this registry belong in Wire providers.
During startup, use BeforeStartup to validate prerequisites, Startup to start resources, and AfterStartup for work that depends on those resources. Hooks run in registration order within each phase.
During shutdown, use BeforeShutdown to stop accepting work, Shutdown to close resources, and AfterShutdown for final cleanup.
For example, an App-owned OpenTelemetry trace provider can flush buffered spans in Shutdown. Pass the hook's context to that cleanup call.
Execute and Verify
To observe the lifecycle, add "log" to the imports in app/lifecycle.go and replace these methods. This fragment uses the existing context import and LifecycleRegistry type.
// BeforeStartup marks the start of App initialization for this check.
func (r *LifecycleRegistry) BeforeStartup(_ context.Context) error {
log.Print("app startup hook")
return nil
}
// Shutdown marks the end of the command for this check.
func (r *LifecycleRegistry) Shutdown(_ context.Context) error {
log.Print("app shutdown hook")
return nil
}From the Project root, run the route-list command against current source.
forj route:listExpect the startup marker, the route table, and then the shutdown marker. After forj build, ./bin/app --help prints root help before App hooks run. Restore the two methods and remove the log import after this check.
Boundaries
Each process has its own lifecycle. In the default App, cmd/app/main.go calls wire.LaunchApplication(). Keep request, job, and schedule behavior in their owning packages. Runtime commands manage listeners and workers.
If startup fails, the App skips the command and lifecycle shutdown hooks. Release any partially opened resources before returning a startup error. Wire provider cleanup and telemetry cleanup still run.
Shutdown Budgets
After successful startup, shutdown hooks run in reverse registration order within each phase. Resources started later can close while earlier dependencies remain available. The App collects errors and continues running the remaining hooks.
Cleanup receives a context bounded by APP_SHUTDOWN_TIMEOUT. Pass that context to blocking cleanup calls. To check HTTP shutdown, stop forj dev if it holds the port, run forj api, then press Ctrl+C and check that cleanup completes.
Next Steps
Runtime Processes covers process roles, shutdown budgets, and supervision. Run the App explains how those roles can run together or separately.