{"record":{"id":"99b17eda7aad1118","repo":"crowdsecurity/crowdsec","slug":"while-starting-flushallowlists-scheduler-w","errorCode":null,"errorMessage":"while starting FlushAllowlists scheduler: %w","messagePattern":"while starting FlushAllowlists scheduler: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/flush.go","lineNumber":140,"sourceCode":"\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)\n\t}\n\n\tscheduler.Start()\n\n\treturn scheduler, nil\n}\n\n// flushMetrics deletes metrics older than maxAge, regardless if they have been pushed to CAPI or not\nfunc (c *Client) flushMetrics(ctx context.Context, maxAge time.Duration) {\n\tif maxAge == 0 {\n\t\tmaxAge = defaultMetricsMaxAge\n\t}\n\n\tc.Log.Debugf(\"flushing metrics older than %s\", maxAge)\n\n\tdeleted, err := c.Ent.Metric.Delete().Where(\n\t\tmetric.ReceivedAtLTE(time.Now().UTC().Add(-maxAge)),\n\t).Exec(ctx)","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/flush.go#L122-L158","documentation":"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.","triggerScenarios":"scheduler.NewJob fails when registering the flushAllowlists task — invalid job definition or scheduler state issue.","commonSituations":"Nil database Client so c.flushAllowlists is not a valid method value; gocron option conflicts; scheduler previously stopped.","solutions":["Initialize the database Client before calling StartFlushScheduler.","Check the wrapped error's message from gocron for the exact rejection reason.","Verify no earlier code path shut down the scheduler.","If the problem persists, update gocron to the version pinned in go.mod — behavior differs across versions."],"exampleFix":"// before\nc, _ := database.NewClient(cfg) // err ignored\n\n// after\nc, err := database.NewClient(cfg)\nif err != nil {\n    return fmt.Errorf(\"while creating database client: %w\", err)\n}","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(), \"FlushAllowlists scheduler\") {\n        // registration of the allowlists flush job failed\n    }\n    return err\n}","preventionTips":["Check constructor errors on the DB client before scheduling jobs.","Keep job registration order intact — earlier failures can leave the scheduler unusable.","Pin gocron/v2 to the tested version in go.mod."],"tags":["go","scheduler","crowdsec","allowlists"],"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"}