Skip to main content

Lifecycle Overview

The pb-ext server follows a predictable lifecycle with distinct phases:
1

Initialization

Create server with New() and apply options
2

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 the New() constructor with functional options:
cmd/server/main.go

Server Creation

The New() 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’s OnBootstrap() 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 persistence
2

Setup Collections

Creates the _job_logs PocketBase collection for execution logs
3

Create Job Manager

Instantiates the Manager that orchestrates cron jobs
4

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__):
Analytics Cleanup Job (__pbExtAnalyticsClean__):
Bootstrap runs before the HTTP server starts. Don’t register routes or middleware here.

Phase 3: Serve

The serve phase initializes HTTP components after the server starts. This is triggered by PocketBase’s OnServe() hook.

Request Tracking Middleware

The first OnServe 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 the Start() method:

User Hooks (Application)

Users bind hooks before calling Start():
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, middleware
Always register user hooks before calling srv.Start(). The framework’s internal hooks are registered during Start().

Accessing 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