crowdsecurity/crowdsec · error

while starting FlushAgentsAndBouncers scheduler: %w

Error message

while starting FlushAgentsAndBouncers scheduler: %w

What it means

scheduler.NewJob for the periodic FlushAgentsAndBouncers garbage-collection task returned an error while StartFlushScheduler was wiring the gocron job. This is a setup-level failure (invalid job options) more than a runtime one; the flush scheduler could not be created, so stale agents/bouncers are not GC'd.

Source

Thrown at pkg/database/flush.go:122

			if err != nil {
				return nil, fmt.Errorf("while parsing bouncers api auto-delete duration: %w", err)
			}

			config.BouncersGC.ApiDuration = &duration
		}

		if config.BouncersGC.LoginPassword != nil {
			c.Log.Warning("bouncers auto-delete for login/password auth is not supported (use cert or api)")
		}
	}

	_, 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)

View on GitHub (pinned to 909b515798)

Solutions

  1. Verify the database Client is initialized before StartFlushScheduler.
  2. Read the wrapped gocron error for the concrete cause and fix the corresponding input.
  3. Confirm the scheduler itself was created successfully and Start() has not been called with a shutdown in between.
  4. If AgentsGC/BouncersGC are absent from config, they are legitimately nil — this path still registers the job, so failure indicates a gocron-level problem, not a config one.

Example fix

// before
c := &database.Client{}
scheduler, err := c.StartFlushScheduler(ctx, flushCfg) // nil internals

// after
c, err := database.NewClient(cfg)
if err != nil {
    return err
}
scheduler, err := c.StartFlushScheduler(ctx, flushCfg)
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(), "FlushAgentsAndBouncers scheduler") {
        // scheduler registration failed; abort startup
    }
    return err
}

Prevention

When it happens

Trigger: scheduler.NewJob fails to register the FlushAgentsAndBouncers task — rejected task construction (e.g. invalid arguments passed to NewTask), scheduler already in a bad state, or invalid job options.

Common situations: Nil/zero Client so the task method value is invalid; passing nil AgentsGC/BouncersGC pointers into the task in a custom build; scheduler shut down by a prior error path.

Related errors


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