From f4c6dbdc416a2a370e806ab78e9d4c664f23caa3 Mon Sep 17 00:00:00 2001 From: Allan Frese Date: Mon, 2 Nov 2020 08:51:35 +0100 Subject: [PATCH] added pause feature --- README.md | 8 ++++++++ src/app.go | 37 ++++++++++++++++++++++++++++++++++++- src/main.go | 22 ++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7ad905e..28fec63 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,14 @@ Create your token here: https://<your-company>.pagerduty.com/api_keys Example configuration file (which is the default one you will get the first time you run the notifier.): ``` +[main] +# default timezone is UTC +timezone = Europe/Copenhagen +# default pause timeout is "0", which is no timeout +pause.timeout = 60 +# clear.on.unpause defines if notifier should clear last timestamp and start notifying from unpause time. +clear.on.unpause = false + [pagerduty] # You need to add your Pagerduty API token token = your-secret-token-here diff --git a/src/app.go b/src/app.go index 27ceb20..3a2cfad 100644 --- a/src/app.go +++ b/src/app.go @@ -25,7 +25,7 @@ var menuItems = []trayhost.MenuItem{ { Title: "Pause", Handler: func() { - appNotify("Pagerduty Notifier", "This is not implemeted yet.", "", nil, 30*time.Second) + togglePause() }, }, { @@ -42,6 +42,8 @@ var menuItems = []trayhost.MenuItem{ } var menuItemsCopy = []trayhost.MenuItem{} +var pause = false +var pauseStopTime time.Time func appInit() { @@ -78,6 +80,39 @@ func appInit() { trayhost.Initialize("Pagerduty Notifier", iconData, menuItems) } +func togglePause() { + if pause { + appNotify("Pagerduty Notifier", "Unpausing notifications", "", nil, 10*time.Second) + log.Println("Stop pause ...") + + for i, m := range menuItemsCopy { + if m.Title == "√ Pause" { + menuItemsCopy[i].Title = "Pause" + } + } + trayhost.UpdateMenu(menuItemsCopy) + pause = false + if clearOnUnpause { + writeTimestamp(time.Now()) + } + } else { + msg := "Pausing notifications" + if (pauseTimeout > 0) { + msg = fmt.Sprintf("%s for %d minutes", msg, pauseTimeout) + pauseStopTime = time.Now().Add(time.Duration(pauseTimeout) * time.Minute) + } + appNotify("Pagerduty Notifier", msg, "", nil, 10*time.Second) + log.Println("Start pause ...") + + for i, m := range menuItemsCopy { + if m.Title == "Pause" { + menuItemsCopy[i].Title = "√ Pause" + } + } + trayhost.UpdateMenu(menuItemsCopy) + pause = true + } +} func toggleStartup() { if existsLaunchConf() { deleteLaunchConf() diff --git a/src/main.go b/src/main.go index 19f985d..2240ae8 100644 --- a/src/main.go +++ b/src/main.go @@ -1,10 +1,14 @@ package main import ( + "log" "runtime" "time" ) +var pauseTimeout int +var clearOnUnpause bool + func main() { // We need to lock the go threads to avoid NSInternalInconsistencyException from 'NSWindow drag regions should only be invalidated on the Main Thread!' @@ -22,10 +26,24 @@ func main() { interval = 30 } + pauseTimeout, err = cfg.Section("main").Key("pause.timeout").Int() + if err != nil { pauseTimeout = 0 } + clearOnUnpause, err = cfg.Section("main").Key("clear.on.unpause").Bool() + if err != nil {clearOnUnpause = true} + go func() { for { - for _, incident := range pdGetIncidents(cfg) { - pdNotify(incident) + if pause { + if (pauseTimeout > 0) { + if time.Now().After(pauseStopTime) { + log.Println("Pause timeout ...") + togglePause() + } + } + } else { + for _, incident := range pdGetIncidents(cfg) { + pdNotify(incident) + } } time.Sleep(time.Duration(interval) * time.Second) }