Run starts monitoring and enforcement of this spec's on disk PKI.
(ctx context.Context)
| 331 | |
| 332 | // Run starts monitoring and enforcement of this spec's on disk PKI. |
| 333 | func (spec *Spec) Run(ctx context.Context) { |
| 334 | // initialize our run At this point an observer knows this spec is 'alive' and being enforced. |
| 335 | SpecExpiresBeforeThreshold.WithLabelValues(spec.Name).Set(float64(spec.Before.Seconds())) |
| 336 | |
| 337 | // cleanup our runtime metrics on the way out so the observer knows we're no longer enforcing. |
| 338 | defer spec.WipeMetrics() |
| 339 | |
| 340 | err := spec.UpdateIfNeeded() |
| 341 | if err != nil { |
| 342 | log.Errorf("spec %s: continuing despite failed initial validation due to %s", spec, err) |
| 343 | } |
| 344 | |
| 345 | rng := rand.New(rand.NewSource(time.Now().UTC().UnixNano())) |
| 346 | sleepPeriod := spec.Interval |
| 347 | if spec.InitialSplay != 0 { |
| 348 | sleepPeriod = time.Duration(rng.Float64() * float64(spec.InitialSplay.Nanoseconds())) |
| 349 | log.Infof("spec %s: initial splay will be used.", spec) |
| 350 | } |
| 351 | for { |
| 352 | log.Infof("spec %s: Next check will be in %s", spec, sleepPeriod) |
| 353 | SpecNextWake.WithLabelValues(spec.Name).Set(float64(time.Now().Add(sleepPeriod).Unix())) |
| 354 | |
| 355 | select { |
| 356 | case <-time.After(sleepPeriod): |
| 357 | log.Debugf("spec %s: woke, starting enforcement", spec) |
| 358 | |
| 359 | // fire wake notifications so things like "spec no longer exists on disk" are checked |
| 360 | // and logged if relevant. |
| 361 | for _, callback := range spec.WakeCallbacks { |
| 362 | callback() |
| 363 | } |
| 364 | |
| 365 | err := spec.UpdateIfNeeded() |
| 366 | if err != nil { |
| 367 | log.Errorf("failed processing %s due to %s", spec, err) |
| 368 | } |
| 369 | sleepPeriod = spec.Interval |
| 370 | if spec.IntervalSplay != 0 { |
| 371 | i := sleepPeriod.Nanoseconds() |
| 372 | i += int64(rng.Float64() * float64(spec.IntervalSplay.Nanoseconds())) |
| 373 | sleepPeriod = time.Duration(int64(i)) |
| 374 | } |
| 375 | case <-ctx.Done(): |
| 376 | log.Debugf("spec %s: stopping monitoring due to %s", spec, ctx.Err()) |
| 377 | return |
| 378 | } |
| 379 | } |
| 380 | } |