crowdsecurity/crowdsec · error
while starting flushMetrics scheduler: %w
Error message
while starting flushMetrics scheduler: %w
What it means
StartFlushScheduler registers a gocron job that runs flushMetrics every minute, deleting metrics older than metrics_max_age. This error wraps a scheduler.NewJob registration failure and aborts startup.
Source
Thrown at pkg/database/flush.go:131
}
}
_, err = scheduler.NewJob(
gocron.DurationJob(flushInterval),
gocron.NewTask(c.FlushAgentsAndBouncers, ctx, config.AgentsGC, config.BouncersGC),
gocron.WithSingletonMode(gocron.LimitModeReschedule),
)
if err != nil {
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) {View on GitHub (pinned to 909b515798)
Solutions
- Ensure the Client is non-nil and fully initialized.
- Inspect the wrapped error for the gocron-level cause and correct that input.
- Confirm earlier jobs in StartFlushScheduler did not leave the scheduler in a bad state.
- Check config.MetricsMaxAge resolves to a sane (non-negative) duration.
Example fix
// before
scheduler, err := c.StartFlushScheduler(ctx, nil)
// after
if flushCfg == nil {
flushCfg = csconfig.DefaultFlushDBCfg()
}
scheduler, err := c.StartFlushScheduler(ctx, flushCfg) Defensive patterns
Strategy: try-catch
Validate before calling
if config != nil && config.MetricsMaxAge < 0 {
return errors.New("metrics_max_age must not be negative")
} 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(), "flushMetrics scheduler") {
// scheduler registration failed
}
return err
} Prevention
- Verify metrics_max_age is a valid non-negative duration in config.
- Initialize the DB client before scheduler startup.
- Treat any wrapped gocron error as fatal at startup, not recoverable mid-run.
When it happens
Trigger: scheduler.NewJob fails when registering the flushMetrics task — invalid task construction, scheduler in an invalid state, or gocron rejecting the job definition.
Common situations: Nil database Client making the method value unusable; time.Duration(config.MetricsMaxAge) computed from a config typo in a custom build; gocron option misuse.
Related errors
- while starting FlushAlerts scheduler: %w
- while starting FlushAgentsAndBouncers scheduler: %w
- while starting FlushAllowlists scheduler: %w
- empty cti key
- no listen_uri or listen_socket specified
AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06).
Data as JSON: /api/errors/567b8d939c8fc0bd.
Report an issue: GitHub.