Skip to content

Commit

Permalink
added pause feature
Browse files Browse the repository at this point in the history
  • Loading branch information
frese committed Nov 2, 2020
1 parent b59a8ad commit f4c6dbd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion src/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
},
{
Expand All @@ -42,6 +42,8 @@ var menuItems = []trayhost.MenuItem{
}

var menuItemsCopy = []trayhost.MenuItem{}
var pause = false
var pauseStopTime time.Time

func appInit() {

Expand Down Expand Up @@ -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 {

This comment has been minimized.

Copy link
@shivas

shivas Nov 3, 2020

add return on true cause and remove else nesting?

This comment has been minimized.

Copy link
@frese

frese Nov 3, 2020

Author Contributor

Hmm, tbh, I think it's easier to read like this - compiler will optimize anyway ?

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()
Expand Down
22 changes: 20 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -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!'
Expand All @@ -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)
}
Expand Down

0 comments on commit f4c6dbd

Please sign in to comment.