Lifecycle Overview
The pb-ext server follows a predictable lifecycle with distinct phases:1
Initialization
Create server with
New() and apply options2
Bootstrap Phase
Initialize infrastructure: JobLogger → JobManager → System Jobs
3
Serve Phase
Register routes, analytics, middleware, and static files
4
Runtime
Handle requests and execute scheduled jobs
Phase 1: Initialization
The server is created using theNew() constructor with functional options:
cmd/server/main.go
Server Creation
TheNew() function creates a Server instance wrapping PocketBase:
core/server/server.go
At this point, no collections are created and no routes are registered. This happens in the bootstrap and serve phases.
Phase 2: Bootstrap
The bootstrap phase initializes core infrastructure before the HTTP server starts. This is triggered by PocketBase’sOnBootstrap() hook.
Job System Initialization
core/server/server.go
Job System Bootstrap Flow
The job system initializes in a specific order:1
Create Job Logger
jobs.Initialize() creates a Logger that manages log persistence2
Setup Collections
Creates the
_job_logs PocketBase collection for execution logs3
Create Job Manager
Instantiates the
Manager that orchestrates cron jobs4
Register System Jobs
Registers built-in cleanup jobs (
__pbExtLogClean__, __pbExtAnalyticsClean__)5
Create Job Handlers
Instantiates HTTP handlers for the job management API
core/jobs/manager.go
System Jobs
pb-ext automatically registers two system jobs during bootstrap: Log Cleanup Job (__pbExtLogClean__):
__pbExtAnalyticsClean__):
Phase 3: Serve
The serve phase initializes HTTP components after the server starts. This is triggered by PocketBase’sOnServe() hook.
Request Tracking Middleware
The firstOnServe hook registers global request tracking:
core/server/server.go
Route Registration
The serve phase registers all HTTP routes:core/server/server.go
Analytics Initialization
The analytics system creates collections and starts background workers:core/analytics/analytics.go
Health Dashboard
The health dashboard at/_/_ is registered during serve:
core/server/health.go
Hook Binding Patterns
Internal Hooks (Framework)
The framework binds hooks in theStart() method:
User Hooks (Application)
Users bind hooks before callingStart():
cmd/server/main.go
Hook Execution Order
1
User OnBootstrap Hooks
Registered before
Start() via srv.App().OnBootstrap().BindFunc()2
Framework OnBootstrap Hook
Registered in
Start() - initializes jobs, logging, etc.3
User OnServe Hooks
Registered before
Start() via srv.App().OnServe().BindFunc()4
Framework OnServe Hooks
Registered in
Start() - registers routes, analytics, middlewareAccessing the Job Manager
The job manager is available as a global singleton after bootstrap:Lifecycle Diagram
Next Steps
Configuration
Learn about server options and configuration patterns
Job Management
Explore cron job registration and execution