Skip to main content
pb-ext includes a powerful cron job system with automatic execution logging, structured output capture, and a management API. All job executions are tracked in the _job_logs collection with automatic retention management.

Quick Start

Register a job using the global GetManager() singleton:

Cron Expression Syntax

pb-ext uses standard cron expression syntax:

Common Patterns

ExecutionLogger Methods

The ExecutionLogger provides structured logging methods for job output:

Basic Logging

Status Indicators

Completion Methods

Statistics Reporting

Real-World Examples

Example 1: Daily Cleanup Job

From cmd/server/jobs.go:55:

Example 2: Weekly Statistics Report

From cmd/server/jobs.go:115:

Example 3: Simple Periodic Task

From cmd/server/jobs.go:35:

Job Management API

All endpoints require superuser authentication.

List Jobs

Response:

Trigger Job Manually

Response:

Remove Job

Get Scheduler Status

Response:

Update Timezone

Get Execution Logs

System Jobs

pb-ext automatically registers internal maintenance jobs. These appear in the dashboard with a “System” badge.

__pbExtLogClean__

From core/jobs/manager.go:287:
  • Schedule: 0 0 * * * (daily at midnight)
  • Purpose: Purges _job_logs records older than 72 hours
  • Retention: 72 hours

__pbExtAnalyticsClean__

From core/jobs/manager.go:353:
  • Schedule: 0 3 * * * (daily at 3 AM)
  • Purpose: Deletes _analytics rows older than 90 days
  • Retention: 90 days

Job Execution Storage

Collection Schema

Job logs are stored in the _job_logs system collection:

Automatic Cleanup

From core/jobs/logger.go:343:
  • Logs older than 72 hours are automatically deleted
  • Cleanup runs during flush operations
  • Orphaned jobs (stuck in “started” status) are marked as “timeout” on startup

Buffer and Flush

From core/jobs/logger.go:14:
  • Logs are buffered in memory
  • Flush interval: 30 seconds
  • Batch size: 100 records
  • Manual flush via ForceFlush()

Error Handling

Jobs that panic are automatically recovered:
From core/jobs/manager.go:417:

Best Practices

  1. Use descriptive job IDs and names for easy identification in logs
  2. Always call el.Start() and el.Complete() or el.Fail() to properly track execution
  3. Use el.Statistics() for structured data instead of multiple Info calls
  4. Handle errors gracefully with el.Error() and el.Fail()
  5. Test jobs manually using the /api/cron/jobs/{id}/run endpoint
  6. Keep job execution time reasonable (under 1 minute preferred)
  7. Use Progress() for long-running operations to show intermediate status
  8. Validate collection existence before performing database operations

Dashboard Integration

View job status and logs in the pb-ext dashboard at /_/_:
  • Recent job executions with status indicators
  • Success/failure rates per job
  • Average execution time
  • Manual job triggering
  • Live execution logs

Advanced: Manual Execution

From core/jobs/manager.go:86: