From 5b456afb49bfa6ff1567510bc1d9362377d32216 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Sun, 4 Oct 2020 22:58:03 +0200 Subject: New config format and new features We're now using json as a config format instead of our weird own format. The weather icon is now optional, simply don't define WeatherPlace in your config. You can now specify which subreddit to get background images from. The default is still /r/EarthPorn. --- interval/interval.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 interval/interval.go (limited to 'interval') diff --git a/interval/interval.go b/interval/interval.go new file mode 100644 index 0000000..35996bb --- /dev/null +++ b/interval/interval.go @@ -0,0 +1,33 @@ +package interval + +import ( + "time" +) + +// IntervalRunner is used to run a function again only after a certain time +type IntervalRunner struct { + durationSuccess time.Duration + durationFailure time.Duration + lastRun time.Time + success bool +} + +func NewIntervalRunner(durationSuccess, durationFailure time.Duration) *IntervalRunner { + return &IntervalRunner{durationSuccess: durationSuccess, durationFailure: durationFailure} +} + +// Run runs the passed in fn function, if the last run is a certain duration ago. +func (ir *IntervalRunner) Run(fn func() bool) { + var duration time.Duration + if ir.success { + duration = ir.durationSuccess + } else { + duration = ir.durationFailure + } + + now := time.Now() + if ir.lastRun.Add(duration).Before(now) { + ir.success = fn() + ir.lastRun = now + } +} -- cgit v1.2.3-70-g09d2