{"record":{"id":"6ca72c4570bf3fc7","repo":"crowdsecurity/crowdsec","slug":"while-starting-flushagentsandbouncers-scheduler","errorCode":null,"errorMessage":"while starting FlushAgentsAndBouncers scheduler: %w","messagePattern":"while starting FlushAgentsAndBouncers scheduler: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/flush.go","lineNumber":122,"sourceCode":"\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"while parsing bouncers api auto-delete duration: %w\", err)\n\t\t\t}\n\n\t\t\tconfig.BouncersGC.ApiDuration = &duration\n\t\t}\n\n\t\tif config.BouncersGC.LoginPassword != nil {\n\t\t\tc.Log.Warning(\"bouncers auto-delete for login/password auth is not supported (use cert or api)\")\n\t\t}\n\t}\n\n\t_, err = scheduler.NewJob(\n\t\tgocron.DurationJob(flushInterval),\n\t\tgocron.NewTask(c.FlushAgentsAndBouncers, ctx, config.AgentsGC, config.BouncersGC),\n\t\tgocron.WithSingletonMode(gocron.LimitModeReschedule),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"while starting FlushAgentsAndBouncers scheduler: %w\", err)\n\t}\n\n\t_, err = scheduler.NewJob(\n\t\tgocron.DurationJob(flushInterval),\n\t\tgocron.NewTask(c.flushMetrics, ctx, time.Duration(config.MetricsMaxAge)),\n\t\tgocron.WithSingletonMode(gocron.LimitModeReschedule),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"while starting flushMetrics scheduler: %w\", err)\n\t}\n\n\t_, err = scheduler.NewJob(\n\t\tgocron.DurationJob(flushInterval),\n\t\tgocron.NewTask(c.flushAllowlists, ctx),\n\t\tgocron.WithSingletonMode(gocron.LimitModeReschedule),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"while starting FlushAllowlists scheduler: %w\", err)","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/flush.go#L104-L140","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the database Client is initialized before StartFlushScheduler.","Read the wrapped gocron error for the concrete cause and fix the corresponding input.","Confirm the scheduler itself was created successfully and Start() has not been called with a shutdown in between.","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."],"exampleFix":"// before\nc := &database.Client{}\nscheduler, err := c.StartFlushScheduler(ctx, flushCfg) // nil internals\n\n// after\nc, err := database.NewClient(cfg)\nif err != nil {\n    return err\n}\nscheduler, err := c.StartFlushScheduler(ctx, flushCfg)","handlingStrategy":"try-catch","validationCode":"if c == nil || c.Ent == nil {\n    return errors.New(\"database client must be initialized before StartFlushScheduler\")\n}","typeGuard":"if c == nil || c.Ent == nil {\n    return errors.New(\"database client not initialized\")\n}","tryCatchPattern":"scheduler, err := c.StartFlushScheduler(ctx, flushCfg)\nif err != nil {\n    if strings.Contains(err.Error(), \"FlushAgentsAndBouncers scheduler\") {\n        // scheduler registration failed; abort startup\n    }\n    return err\n}","preventionTips":["Construct the Client properly; never pass a zero-value struct.","Keep gocron at the go.mod-pinned version to avoid behavioral changes.","Log wrapped errors fully rather than only the outer message."],"tags":["go","scheduler","crowdsec","database"],"backgroundTag":"scheduler-job-registration-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}