Troubleshooting
This page provides solutions for common issues encountered when integrating or deploying d-cron.
Leadership and Locking Issues
Problem: Leadership repeatedly cycling or dropping unexpectedly
Solutions:
- Verify connection pooler mode: If you are using PgBouncer, ensure it is not operating in transaction mode for
d-cron. Use a direct PostgreSQL connection for the lock viadcron.WithDedicatedLockDSN("postgres://...")or configure a dedicated PgBouncer database alias in session mode. See the Production Guide. - Prevent connection recycling: If passing a shared
*sql.DBconnection pool, ensuredb.SetConnMaxLifetime(0)is set on the lock connection handle so Go's connection pool does not close the session periodically. - Check TCP keepalive settings: Ensure PostgreSQL connection keepalives (
keepalives=1&keepalives_idle=10&keepalives_interval=3&keepalives_count=3) are configured in the DSN to detect dropped connections quickly.
Problem: dcron: session stability not asserted; refusing to start
Solutions:
- Explicit assertion required:
d-cronrequires session-bound connections for PostgreSQL advisory locks to guarantee safety. When using PgBouncer in transaction pooling mode, connection recycling can silently orphan locks or create multiple simultaneous leaders. - Direct connection or session mode: If your
*sql.DBconnects directly to PostgreSQL or a session-mode pooler, adddcron.WithSessionStableConnection()to yourdcron.New(...)constructor call. - Dedicated lock connection: If your application pool connects through transaction-mode PgBouncer, provide an independent direct lock connection with
dcron.WithDedicatedLockDSN("postgres://...")ordcron.WithDedicatedLockDriver("pgx", "postgres://...").
Problem: dcron: not the leader (HTTP 409 Conflict) on job mutations
Solutions:
- Standby mutation rejection: Administrative mutations (
Pause,Resume,Remove,AddDynamic) modify the active cluster scheduling loop and must execute against the current active Leader replica. - Configure mutation forwarding: Pass
dcron.WithLeaderForwardURL("http://dcron-leader:8080")when initializingdcron.New(...). Standby replicas will automatically proxy administrative mutation requests to the active leader and return its response. - Direct leader routing: Alternatively, route administrative HTTP traffic through a Kubernetes service targeting the active leader pod.
Problem: Jobs executing simultaneously on multiple pods (Duplicate runs)
Solutions:
- Check namespace configuration: All replicas of the same service must share the exact same namespace string.
d-cronderives the 64-bit advisory lock key fromdcron.WithNamespace("service-name"). If pod A uses"service-a"and pod B uses"service-b", both will elect themselves leader. - Verify single database instance: Ensure all replicas connect to the same PostgreSQL primary database. If replicas connect to different read replicas or partitioned databases, each will acquire the advisory lock independently.
Fencing and Concurrency Issues
Problem: Fencing rejection error ("fencing error: epoch X is stale")
Solutions:
- Recognize intended safety behavior: This error indicates that a long GC pause, process freeze, or network partition occurred. PostgreSQL dropped the session lock, promoted another standby replica to a higher epoch, and the demoted replica attempted to execute a database write. The fencing mechanism successfully prevented data corruption.
- Abort stale handlers: Handle
dcron.Fence(ctx, tx)errors by rolling back the transaction and aborting the handler cleanly. - Tune drain timeouts: Ensure
dcron.WithDrainTimeout(d)allows sufficient grace period for in-flight jobs to complete during graceful shutdown.
Problem: High database CPU or lock polling spikes
Solutions:
- Adjust poll interval: Standby replicas poll
TryLockperiodically. Ensuredcron.WithPollIntervalis not set to an aggressive sub-second interval. The recommended default is3sto5s. - Verify jitter:
d-cronadds randomized jitter (±20%) to polling intervals automatically to prevent synchronized polling storms. Avoid overriding this with custom fixed timers.
Execution and Job Issues
Problem: Missed jobs after restart or deployment
Solutions:
- Configure missed run policy: By default,
d-cronusesMissedSkip. If jobs must catch up after an application restart, configuredcron.WithMissedRunPolicy(dcron.MissedCatchUp)anddcron.WithHistory(...). - Set lookback limit: Use
dcron.WithMaxLookback(24 * time.Hour)to prevent executing runs that are too old.
Problem: Long-running jobs blocking subsequent schedules
Solutions:
- Configure overlap policy: By default, overlapping runs of the same job are skipped (
dcron.OverlapSkip). If you need jobs to queue or run concurrently, passdcron.WithOverlapPolicy(dcron.OverlapQueue)ordcron.WithOverlapPolicy(dcron.OverlapAllow). - Set timeouts: Enforce maximum execution duration per job using
dcron.WithTimeout(10 * time.Minute).
Getting Help
If you are still experiencing issues:
- Check the Introduction for basic integration examples
- Enable structured debug logging: pass
dcron.WithLogger(slog.Default())todcron.New - Inspect Prometheus metrics: observe
dcron_leadership_status,dcron_leader_transitions_total, anddcron_job_executions_total - Check the GitHub Issues for similar problems