add --no-cache flag #2

develop
Norwin 6 years ago
parent d8e6d642fc
commit bc542eb506

@ -117,6 +117,7 @@ var cfgFile string
func init() { func init() {
var ( var (
debug bool debug bool
noCache bool
shouldNotify string shouldNotify string
logFormat string logFormat string
api string api string
@ -137,6 +138,7 @@ Notifications for failing checks are sent only once,
and then cached until the issue got resolved. and then cached until the issue got resolved.
To clear the cache, delete the file ~/.osem_notify_cache.yaml. To clear the cache, delete the file ~/.osem_notify_cache.yaml.
`) `)
rootCmd.PersistentFlags().BoolVarP(&noCache, "no-cache", "", false, "send all notifications, ignoring results from previous runs. also don't update the cache.")
viper.BindPFlags(rootCmd.PersistentFlags()) // let flags override config viper.BindPFlags(rootCmd.PersistentFlags()) // let flags override config

@ -64,7 +64,8 @@ func checkAndNotify(boxIds []string) error {
return fmt.Errorf("invalid value %s for \"notify\"", notify) return fmt.Errorf("invalid value %s for \"notify\"", notify)
} }
return results.SendNotifications(types) useCache := !viper.GetBool("no-cache")
return results.SendNotifications(types, useCache)
} }
return nil return nil
} }

@ -39,9 +39,10 @@ func (box Box) GetNotifier() (AbstractNotifier, error) {
return notifier.New(box.NotifyConf.Notifications.Options) return notifier.New(box.NotifyConf.Notifications.Options)
} }
func (results BoxCheckResults) SendNotifications(notifyTypes []string) error { func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache bool) error {
// TODO: expose flag to not use cache if useCache {
results = results.filterChangedFromCache() results = results.filterChangedFromCache()
}
toCheck := results.Size(notifyTypes) toCheck := results.Size(notifyTypes)
if toCheck == 0 { if toCheck == 0 {
@ -90,11 +91,13 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string) error {
} }
// update cache (with /all/ changed results to reset status) // update cache (with /all/ changed results to reset status)
notifyLog.Debug("updating cache") if useCache {
cacheError := updateCache(box, resultsBox) notifyLog.Debug("updating cache")
if cacheError != nil { cacheError := updateCache(box, resultsBox)
notifyLog.Error("could not cache notification results: ", cacheError) if cacheError != nil {
errs = append(errs, cacheError.Error()) notifyLog.Error("could not cache notification results: ", cacheError)
errs = append(errs, cacheError.Error())
}
} }
if len(resultsDue) != 0 { if len(resultsDue) != 0 {

Loading…
Cancel
Save