| 29 | ) |
| 30 | |
| 31 | func (c *Client) StartFlushScheduler(ctx context.Context, config *csconfig.FlushDBCfg) (gocron.Scheduler, error) { |
| 32 | maxItems := 0 |
| 33 | |
| 34 | if config.MaxItems != nil && *config.MaxItems <= 0 { |
| 35 | return nil, errors.New("max_items can't be zero or negative") |
| 36 | } |
| 37 | |
| 38 | if config.MaxItems != nil { |
| 39 | maxItems = *config.MaxItems |
| 40 | } |
| 41 | |
| 42 | // Init & Start cronjob every minute for alerts |
| 43 | scheduler, err := gocron.NewScheduler( |
| 44 | gocron.WithLocation(time.UTC), |
| 45 | gocron.WithLogger(logging.GoCronLoggerAdapter{Logger: c.Log}), |
| 46 | ) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | _, err = scheduler.NewJob( |
| 52 | gocron.DurationJob(1*time.Minute), |
| 53 | gocron.NewTask(c.FlushAlerts, ctx, time.Duration(config.MaxAge), maxItems), |
| 54 | gocron.WithSingletonMode(gocron.LimitModeReschedule), |
| 55 | ) |
| 56 | if err != nil { |
| 57 | return nil, fmt.Errorf("while starting FlushAlerts scheduler: %w", err) |
| 58 | } |
| 59 | |
| 60 | // Init & Start cronjob every hour for bouncers/agents |
| 61 | if config.AgentsGC != nil { |
| 62 | if config.AgentsGC.Cert != nil { |
| 63 | duration, err := cstime.ParseDurationWithDays(*config.AgentsGC.Cert) |
| 64 | if err != nil { |
| 65 | return nil, fmt.Errorf("while parsing agents cert auto-delete duration: %w", err) |
| 66 | } |
| 67 | |
| 68 | config.AgentsGC.CertDuration = &duration |
| 69 | } |
| 70 | |
| 71 | if config.AgentsGC.LoginPassword != nil { |
| 72 | duration, err := cstime.ParseDurationWithDays(*config.AgentsGC.LoginPassword) |
| 73 | if err != nil { |
| 74 | return nil, fmt.Errorf("while parsing agents login/password auto-delete duration: %w", err) |
| 75 | } |
| 76 | |
| 77 | config.AgentsGC.LoginPasswordDuration = &duration |
| 78 | } |
| 79 | |
| 80 | if config.AgentsGC.Api != nil { |
| 81 | c.Log.Warning("agents auto-delete for API auth is not supported (use cert or login_password)") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if config.BouncersGC != nil { |
| 86 | if config.BouncersGC.Cert != nil { |
| 87 | duration, err := cstime.ParseDurationWithDays(*config.BouncersGC.Cert) |
| 88 | if err != nil { |