@@ -82,11 +82,29 @@ func NewService(thanos ThanosSource, store DataStore, resources ResourceProvider
8282func (s * Service ) Start (ctx context.Context ) error {
8383 telemetry .Info ("service_started" , "interval" , Interval .String ())
8484
85- // 3. Main Loop
85+ // Run immediate first batch
86+ s .RunBatch (ctx )
87+
88+ // Calculate time until next interval boundary
89+ now := time .Now ().UTC ()
90+ next := now .Truncate (Interval ).Add (Interval )
91+ sleepDuration := next .Sub (now )
92+
93+ telemetry .Info ("waiting_for_time_boundary" , "sleep" , sleepDuration .String (), "next_run" , next .Format (time .RFC3339 ))
94+
95+ // Wait for the boundary or shutdown
96+ select {
97+ case <- time .After (sleepDuration ):
98+ case <- ctx .Done ():
99+ telemetry .Info ("service_shutting_down" )
100+ return nil
101+ }
102+
103+ // Now we are aligned, start the regular ticker
86104 ticker := time .NewTicker (Interval )
87105 defer ticker .Stop ()
88106
89- // Run immediate first batch
107+ // Run the first aligned batch immediately
90108 s .RunBatch (ctx )
91109
92110 for {
@@ -122,7 +140,7 @@ func (s *Service) RunBatch(ctx context.Context) {
122140
123141 telemetry .AddInt64Counter (ctx , collTotal , 1 )
124142
125- end := time .Now ().UTC ().Truncate (time . Minute )
143+ end := time .Now ().UTC ().Truncate (Interval )
126144 start := end .Add (- Interval )
127145
128146 // 1. Get Host Metadata directly from mounted host files
0 commit comments