Skip to main content

Go API Reference

Complete API reference for d-cron core packages: dcron, executor, metrics, and otel.

Package dcron​

The primary package github.com/mindfiredigital/d-cron/dcron contains the Scheduler type, constructor options, job registration methods, and fencing utilities.

dcron.New​

func New(db *sql.DB, opts ...Option) (*Scheduler, error)

Constructs a new distributed Scheduler bound to a PostgreSQL database handle.

Configuration Options (dcron.Option)​

OptionSignatureDescription
WithNamespaceWithNamespace(namespace string) OptionSets cluster namespace; derives 64-bit advisory lock key. Default: "default".
WithLocationWithLocation(loc *time.Location) OptionSets timezone for cron evaluation. Default: time.UTC.
WithPollIntervalWithPollInterval(d time.Duration) OptionStandby lock polling interval (with ±20% jitter). Default: 5s.
WithDrainTimeoutWithDrainTimeout(d time.Duration) OptionGrace period on Stop before cancelling in-flight jobs. Default: 30s.
WithStartupTimeoutWithStartupTimeout(d time.Duration) OptionBounds initial DB operations (lock connection, migrations, keepalives). Default: 15s.
WithPollTimeoutWithPollTimeout(d time.Duration) OptionPer-attempt timeout for advisory lock probe queries. Default: 5s.
WithDBTimeoutWithDBTimeout(d time.Duration) OptionPer-operation timeout for history store and admin DB queries. Default: 5s.
WithOpTimeoutWithOpTimeout(d time.Duration) OptionAlias for WithDBTimeout. Default: 5s.
WithLoggerWithLogger(l *slog.Logger) OptionSets structured logger for scheduler diagnostics. Default: slog.Default().
WithSessionStableConnectionWithSessionStableConnection() OptionAsserts DB handle is session-stable (direct Postgres or session-mode pooler).
WithDedicatedLockDSNWithDedicatedLockDSN(dsn string) OptionOpens dedicated direct Postgres connection for advisory locks, bypassing shared pool.
WithDedicatedLockDriverWithDedicatedLockDriver(driverName, dsn string) OptionOpens dedicated direct connection with custom registered driver (e.g. "pgx").
WithDedicatedLockConnWithDedicatedLockConn(open func(ctx context.Context) (*sql.Conn, error)) OptionCustom opener returning dedicated connection for lock.
WithLeaderForwardURLWithLeaderForwardURL(url string) OptionTarget URL to proxy admin mutation requests from standby to active leader.
WithJobHandlerWithJobHandler(name string, fn JobFunc) OptionRegisters named handler for restoring dynamic database jobs across failover.
WithSecondsFieldWithSecondsField() OptionEnables 6-field cron expressions with second-level precision.
WithMetricsWithMetrics(rec metrics.Recorder) OptionWires an observability recorder to receive lifecycle and execution metrics.
WithTracerWithTracer(t otel.Tracer) OptionAttaches OpenTelemetry tracer for instrumenting job executions.
WithHistoryWithHistory(retention time.Duration) OptionEnables durable execution history in PostgreSQL with automatic pruning.
WithSchemaWithSchema(schema string) OptionSets PostgreSQL schema used for history and epoch tables. Default: "dcron".
WithHooksWithHooks(hooks ...Hook) OptionRegisters notification hooks (e.g. WebhookHook).

Methods on Scheduler​

// Registration
func (s *Scheduler) Add(name, spec string, fn JobFunc, opts ...JobOption) error
func (s *Scheduler) AddOnce(name string, at time.Time, fn JobFunc, opts ...JobOption) error
func (s *Scheduler) AddDynamic(name, spec string, fn JobFunc, opts ...JobOption) error
func (s *Scheduler) RegisterHandler(name string, fn JobFunc)

// Job Controls (Durable in PostgreSQL, failover safe)
func (s *Scheduler) Pause(name string) error
func (s *Scheduler) Resume(name string) error
func (s *Scheduler) Remove(name string) error

// Lifecycle
func (s *Scheduler) Start(ctx context.Context) error
func (s *Scheduler) Stop(ctx context.Context) error
func (s *Scheduler) Leadership() LeadershipState
func (s *Scheduler) HealthCheck(ctx context.Context) error

// Introspection
func (s *Scheduler) Jobs() []JobStatus
func (s *Scheduler) Epoch() int64
func (s *Scheduler) Key() int64
func (s *Scheduler) InstanceID() string
func (s *Scheduler) Namespace() string

Job Status Model​

type JobStatus struct {
Name string
Spec string
NextRun time.Time
LastRun time.Time
LastOutcome string
LastError string
LastDuration time.Duration
Running bool
Paused bool
}

// JobInfo is an exported alias for JobStatus.
type JobInfo = JobStatus

Fencing Functions​

// Read fencing metadata from job context
func Epoch(ctx context.Context) int64
func IdempotencyKey(ctx context.Context) string
func NamespaceKey(ctx context.Context) string

// Verify epoch in PostgreSQL transaction under FOR SHARE lock
func Fence(ctx context.Context, tx Querier) error
func FenceSchema(ctx context.Context, tx Querier, schema string) error

Error Sanitization​

// RedactError sanitizes DSN credentials, passwords, Bearer tokens,
// and API keys from error messages while preserving non-sensitive context.
func RedactError(err error) string

Admin REST API & Authentication​

// AuthFunc validates incoming administrative requests.
type AuthFunc func(r *http.Request) bool

// RequireBearerToken returns an AuthFunc that validates against a static token
// using constant-time comparison. Empty token safely rejects all requests.
func RequireBearerToken(token string) AuthFunc

// AdminHandler exposes cluster inspection and job control REST endpoints.
// Note: If auth is nil, endpoints are unauthenticated.
func AdminHandler(auth AuthFunc, s *Scheduler) http.Handler

Package executor: Job Options & Policies​

Job functions registered with d-cron implement the standard signature:

type JobFunc func(ctx context.Context) error

Job Configuration Options (JobOption)​

OptionSignatureDescription
WithTimeoutWithTimeout(d time.Duration) JobOptionSets maximum execution duration per run.
WithRetryWithRetry(r Retry) JobOptionConfigures exponential backoff retry behavior.
WithNoRetryWithNoRetry() JobOptionConfigures job to run at most once without retries (Attempts: 1).
WithNoOverlapWithNoOverlap() JobOptionShorthand for WithOverlapPolicy(OverlapSkip).
WithOverlapPolicyWithOverlapPolicy(p OverlapPolicy) JobOptionOverlapSkip, OverlapQueue, or OverlapAllow.
WithMissedRunPolicyWithMissedRunPolicy(p MissedRunPolicy) JobOptionMissedSkip or MissedCatchUp.
WithMaxLookbackWithMaxLookback(d time.Duration) JobOptionBounds catch-up window for missed runs.
WithMaxCatchUpRunsWithMaxCatchUpRuns(n int) JobOptionCaps maximum executions dispatched during catch-up.
WithSinceLastSuccessWithSinceLastSuccess(d time.Duration) JobOptionSchedules next run relative to completion of previous successful run.

Retry Configuration Struct​

type Retry struct {
Attempts int // Total execution attempts (Attempts == 1 means 0 retries)
Backoff time.Duration // Initial retry delay (e.g. 500ms)
Factor float64 // Multiplier (e.g. 2.0)
MaxBackoff time.Duration // Maximum delay cap (e.g. 10s)
Jitter bool // Add randomized jitter
Timeout time.Duration // Per-attempt timeout
Retryable func(error) bool // Optional predicate: return false to abort retries early
}

Note on Retry Defaults: The default zero-value (Attempts: 0) retains backward compatibility and defaults to 5 attempts. To disable retries explicitly for a non-idempotent job, pass dcron.WithNoRetry() or dcron.WithRetry(dcron.Retry{Attempts: 1}).

Terminal Execution Result & Hooks​

type Result struct {
Name string
Outcome string
Error error
Attempts int
Duration time.Duration
}

const (
OutcomeOK = "ok"
OutcomeFailed = "failed"
OutcomePanicked = "panicked"
OutcomeTimedOut = "timed_out"
OutcomeCanceled = "canceled"
OutcomeUnknown = "unknown"
)

type Hook interface {
Fire(ctx context.Context, res Result) error
}

Package metrics: Observability Seam​

const (
KeyIsLeader = "dcron_is_leader"
KeyLeaderTransitions = "dcron_leader_transitions_total"
KeyJobExecutions = "dcron_job_executions_total"
KeyJobDuration = "dcron_job_duration_seconds"
KeyJobLastSuccess = "dcron_job_last_success_timestamp"
KeyJobsRunning = "dcron_jobs_running"
KeyFencedWrites = "dcron_fenced_writes_total"
KeyMissedRuns = "dcron_missed_runs_total"
KeyRecordFailures = "dcron_record_failures_total"
KeyBackendErrors = "dcron_backend_errors_total"
)

type Recorder interface {
SetLeader(instance string, isLeader bool)
LeaderTransition(instance string)
JobStarted(job string)
JobFinished(job string, outcome Outcome, duration time.Duration, success bool)
FencedWrite()
MissedRun(job string)
}

type HistoryRecorder interface {
RecordFailure(job string)
}

type BackendErrorRecorder interface {
BackendError(operation string)
}

Execution Outcomes (metrics.Outcome)​

const (
OutcomeUnknown Outcome = iota
OutcomeOK // "success"
OutcomeFailed // "failed"
OutcomePanicked // "panicked"
OutcomeTimedOut // "timeout"
OutcomeCanceled // "canceled"
OutcomeSkipped // "skipped"
)

Package otel: Distributed Tracing​

type Tracer interface {
StartSpan(ctx context.Context, jobName string, scheduledAt time.Time, attempt int, epoch int64) (context.Context, func(outcome string, err error))
}

Emitted spans capture job.name, attempt, and the leader.epoch fencing token. Wire into dcron.New using dcron.WithTracer(tracer).