Skip to content

EnableJob does not compute next_run_at — enabled cron jobs never fire #378

@duhd-vnpay

Description

@duhd-vnpay

Bug

PGCronStore.EnableJob() sets enabled = true but does not update next_run_at. Since GetDueJobs() filters by next_run_at <= now, the job never fires after being re-enabled via the UI toggle (cron.toggle RPC).

Root Cause

// cron_crud.go — current code
func (s *PGCronStore) EnableJob(jobID string, enabled bool) error {
    _, err = s.db.Exec("UPDATE cron_jobs SET enabled = $1, updated_at = $2 WHERE id = $3", enabled, time.Now(), id)
    // ...
}

When enabling: next_run_at stays NULL (cleared when disabled), so GetDueJobs skips it forever. recomputeStaleJobs() only runs at startup, not after UI toggle.

When disabling: next_run_at is not cleared, so the scheduler may still pick it up briefly.

Expected Behavior

  • Enable: read schedule fields from DB, call computeNextRun(), set next_run_at
  • Disable: set next_run_at = NULL immediately

Suggested Fix

func (s *PGCronStore) EnableJob(jobID string, enabled bool) error {
    if !enabled {
        // Clear next_run_at so GetDueJobs skips immediately
        s.db.Exec("UPDATE cron_jobs SET enabled = false, next_run_at = NULL, updated_at = $1 WHERE id = $2", now, id)
        s.cacheLoaded = false
        return nil
    }
    // Enable: read schedule, compute next_run_at
    row := s.db.QueryRow(`SELECT schedule_kind, cron_expression, run_at, timezone, interval_ms FROM cron_jobs WHERE id = $1`, id)
    // ... scan into CronSchedule ...
    next := computeNextRun(&schedule, now, defaultTZ)
    s.db.Exec("UPDATE cron_jobs SET enabled = true, next_run_at = $1, updated_at = $2 WHERE id = $3", next, now, id)
    s.cacheLoaded = false
    return nil
}

Steps to Reproduce

  1. Create a cron job (e.g., 0 7 * * *)
  2. Disable it via UI → cron.toggle RPC
  3. Re-enable it via UI → cron.toggle RPC
  4. Wait for scheduled time — job never fires
  5. Check DB: next_run_at is NULL despite enabled = true

Environment

  • GoClaw v1.74.1
  • PostgreSQL-backed cron store (PGCronStore)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions