| 807 | } |
| 808 | |
| 809 | func (r *Ruler) syncRules(ctx context.Context, reason string) error { |
| 810 | level.Info(r.logger).Log("msg", "syncing rules", "reason", reason) |
| 811 | r.rulerSync.WithLabelValues(reason).Inc() |
| 812 | timer := prometheus.NewTimer(nil) |
| 813 | |
| 814 | defer func() { |
| 815 | ruleGroupSyncDuration := timer.ObserveDuration().Seconds() |
| 816 | r.ruleGroupSyncDuration.Set(ruleGroupSyncDuration) |
| 817 | }() |
| 818 | |
| 819 | loadedConfigs, backupConfigs, err := r.loadRuleGroups(ctx) |
| 820 | if err != nil { |
| 821 | return err |
| 822 | } |
| 823 | |
| 824 | if ctx.Err() != nil { |
| 825 | level.Info(r.logger).Log("msg", "context is canceled. not syncing rules") |
| 826 | return err |
| 827 | } |
| 828 | // This will also delete local group files for users that are no longer in 'configs' map. |
| 829 | r.manager.SyncRuleGroups(ctx, loadedConfigs) |
| 830 | |
| 831 | if r.cfg.RulesBackupEnabled() { |
| 832 | r.manager.BackUpRuleGroups(ctx, backupConfigs) |
| 833 | } |
| 834 | |
| 835 | return nil |
| 836 | } |
| 837 | |
| 838 | func (r *Ruler) loadRuleGroups(ctx context.Context) (map[string]rulespb.RuleGroupList, map[string]rulespb.RuleGroupList, error) { |
| 839 | timer := prometheus.NewTimer(nil) |