crowdsecurity/crowdsec · error

while starting FlushAllowlists scheduler: %w

Error message

while starting FlushAllowlists scheduler: %w

What it means

StartFlushScheduler registers a gocron job that runs flushAllowlists every minute to expire allowlist entries. This error wraps a scheduler.NewJob registration failure and aborts startup before scheduler.Start() is reached.

Source

Thrown at pkg/database/flush.go:140

		return nil, fmt.Errorf("while starting FlushAgentsAndBouncers scheduler: %w", err)
	}

	_, err = scheduler.NewJob(
		gocron.DurationJob(flushInterval),
		gocron.NewTask(c.flushMetrics, ctx, time.Duration(config.MetricsMaxAge)),
		gocron.WithSingletonMode(gocron.LimitModeReschedule),
	)
	if err != nil {
		return nil, fmt.Errorf("while starting flushMetrics scheduler: %w", err)
	}

	_, err = scheduler.NewJob(
		gocron.DurationJob(flushInterval),
		gocron.NewTask(c.flushAllowlists, ctx),
		gocron.WithSingletonMode(gocron.LimitModeReschedule),
	)
	if err != nil {
		return nil, fmt.Errorf("while starting FlushAllowlists scheduler: %w", err)
	}

	scheduler.Start()

	return scheduler, nil
}

// flushMetrics deletes metrics older than maxAge, regardless if they have been pushed to CAPI or not
func (c *Client) flushMetrics(ctx context.Context, maxAge time.Duration) {
	if maxAge == 0 {
		maxAge = defaultMetricsMaxAge
	}

	c.Log.Debugf("flushing metrics older than %s", maxAge)

	deleted, err := c.Ent.Metric.Delete().Where(
		metric.ReceivedAtLTE(time.Now().UTC().Add(-maxAge)),
	).Exec(ctx)

View on GitHub (pinned to 909b515798)

Solutions

  1. Initialize the database Client before calling StartFlushScheduler.
  2. Check the wrapped error's message from gocron for the exact rejection reason.
  3. Verify no earlier code path shut down the scheduler.
  4. If the problem persists, update gocron to the version pinned in go.mod — behavior differs across versions.

Example fix

// before
c, _ := database.NewClient(cfg) // err ignored

// after
c, err := database.NewClient(cfg)
if err != nil {
    return fmt.Errorf("while creating database client: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if c == nil || c.Ent == nil {
    return errors.New("database client must be initialized before StartFlushScheduler")
}

Type guard

if c == nil || c.Ent == nil {
    return errors.New("database client not initialized")
}

Try / catch

scheduler, err := c.StartFlushScheduler(ctx, flushCfg)
if err != nil {
    if strings.Contains(err.Error(), "FlushAllowlists scheduler") {
        // registration of the allowlists flush job failed
    }
    return err
}

Prevention

When it happens

Trigger: scheduler.NewJob fails when registering the flushAllowlists task — invalid job definition or scheduler state issue.

Common situations: Nil database Client so c.flushAllowlists is not a valid method value; gocron option conflicts; scheduler previously stopped.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/99b17eda7aad1118. Report an issue: GitHub.