Overview
TheJobManager orchestrates cron job registration and execution with automatic logging to PocketBase. It provides structured execution logging, manual job triggers, and comprehensive job analytics.
Type Definition
The JobManager wraps PocketBase’s built-in cron system with automatic execution logging and metadata tracking. All logs are stored in the
_job_logs collection.Initialization
Initialize
core.App
required
PocketBase application instance
*Manager- Initialized manager instanceerror- Error if logger initialization fails
core/jobs/manager.go:32
Process:
- Initializes the job logger
- Creates the
_job_logscollection - Sets up background flush workers
- Marks orphaned jobs (from crashes) as timeout
- Sets the global singleton
GetManager
core/jobs/manager.go:456
Example:
Job Registration
RegisterJob
string
required
Unique job identifier
string
Human-readable job name (uses jobID if empty)
string
Job description for documentation
string
required
Cron expression (e.g.,
"0 0 * * *" for daily at midnight)func(*ExecutionLogger)
required
Job execution function with structured logging
error- Error if registration or cron scheduling fails
core/jobs/manager.go:46
Example:
RegisterInternalSystemJobs
__pbExtLogClean__- Cleans job logs older than 72 hours (daily at midnight)__pbExtAnalyticsClean__- Deletes analytics older than 90 days (daily at 3 AM)
core/jobs/manager.go:286
Job Execution
ExecuteJobManually
string
required
Job identifier
string
User or system that triggered execution (for audit trail)
core/jobs/manager.go:86
Example:
Job Information
GetJobs
ListOptions
required
Filter options for job listing
core/jobs/manager.go:174
Example:
GetJobMetadata
core/jobs/manager.go:210
GetSystemStatus
core/jobs/manager.go:245
Job Management
RemoveJob
core/jobs/manager.go:235
Example:
UpdateTimezone
string
required
IANA timezone name (e.g., “America/New_York”)
core/jobs/manager.go:275
Example:
Logger
core/jobs/manager.go:394
Complete Examples
Basic Job Registration
Job with Error Handling
Progress Tracking
Manual Execution via API
Conditional Job Registration
System Jobs
These job IDs are treated as system jobs:__pbLogsCleanup__- PocketBase log cleanup__pbOTPCleanup__- PocketBase OTP cleanup__pbMFACleanup__- PocketBase MFA cleanup__pbDBOptimize__- PocketBase database optimization__pbRateLimitersCleanup__- PocketBase rate limiter cleanup__pbExtLogClean__- pb-ext job log cleanup__pbExtAnalyticsClean__- pb-ext analytics cleanup
core/jobs/types.go:22
Best Practices
- Job IDs: Use descriptive, unique IDs (e.g.,
"daily_cleanup", not"job1") - Structured Logging: Always use
ExecutionLoggermethods, not directfmt.Println - Error Handling: Always call
log.Fail(err)on errors - Statistics: Use
log.Statistics()for metrics (processed count, duration, etc.) - Progress Updates: Call
log.Progress()for long-running jobs - Timezone Awareness: Set timezone early in app lifecycle
- Manual Triggers: Require authentication for manual execution endpoints
Related
- ExecutionLogger - Structured job logging
- Job HTTP Handlers - REST API for job management
- Cron Expression Guide - Scheduling syntax