Skip to main content

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 via dcron.WithDedicatedLockDSN("postgres://...") or configure a dedicated PgBouncer database alias in session mode. See the Production Guide.
  • Prevent connection recycling: If passing a shared *sql.DB connection pool, ensure db.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-cron requires 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.DB connects directly to PostgreSQL or a session-mode pooler, add dcron.WithSessionStableConnection() to your dcron.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://...") or dcron.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 initializing dcron.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-cron derives the 64-bit advisory lock key from dcron.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 TryLock periodically. Ensure dcron.WithPollInterval is not set to an aggressive sub-second interval. The recommended default is 3s to 5s.
  • Verify jitter: d-cron adds 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-cron uses MissedSkip. If jobs must catch up after an application restart, configure dcron.WithMissedRunPolicy(dcron.MissedCatchUp) and dcron.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, pass dcron.WithOverlapPolicy(dcron.OverlapQueue) or dcron.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:

  1. Check the Introduction for basic integration examples
  2. Enable structured debug logging: pass dcron.WithLogger(slog.Default()) to dcron.New
  3. Inspect Prometheus metrics: observe dcron_leadership_status, dcron_leader_transitions_total, and dcron_job_executions_total
  4. Check the GitHub Issues for similar problems