Skip to content

Commit

Permalink
Merge pull request #7 from iobear/catch-sigterm
Browse files Browse the repository at this point in the history
Catch sigterm
  • Loading branch information
iobear authored Aug 7, 2024
2 parents 9031219 + 5143c23 commit ff035fa
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/dashgoat/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type (

func fromAlertmanager(c echo.Context) error {

if !isDashGoatReady() {
return c.NoContent(http.StatusServiceUnavailable)
}

dec := json.NewDecoder(c.Request().Body)
defer c.Request().Body.Close()

Expand Down
4 changes: 4 additions & 0 deletions cmd/dashgoat/buddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func findBuddy(buddyConfig []Buddy) {
}
}

if isDashGoatShutdown() {
break
}

if !isDashGoatReady() {
setDashGoatReady(true)
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/dashgoat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
// heartBeat update
func heartBeat(c echo.Context) error {

if !isDashGoatReady() {
return c.NoContent(http.StatusServiceUnavailable)
}

ss.mutex.Lock()
defer ss.mutex.Unlock()

Expand Down Expand Up @@ -89,6 +93,10 @@ func heartBeat(c echo.Context) error {
// updateStatus - service update
func updateStatus(c echo.Context) error {

if !isDashGoatReady() {
return c.NoContent(http.StatusServiceUnavailable)
}

ss.mutex.Lock()
defer ss.mutex.Unlock()

Expand Down
22 changes: 22 additions & 0 deletions cmd/dashgoat/hostfacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ type (
Items HostFact
mutex sync.RWMutex
}
DGshutdown struct {
shutdown bool
mutex sync.RWMutex
}
)

var host_facts HostFacts
var dg_shutdown DGshutdown

func generateHostFacts() {
host_facts.mutex.Lock()
Expand Down Expand Up @@ -130,3 +135,20 @@ func setDashGoatReady(ready bool) {
defer host_facts.mutex.Unlock()
host_facts.Items.Ready = ready
}

func isDashGoatShutdown() bool {
dg_shutdown.mutex.RLock()
defer dg_shutdown.mutex.RUnlock()
return dg_shutdown.shutdown
}

func setDashGoatShutdown(state bool) {
dg_shutdown.mutex.Lock()
defer dg_shutdown.mutex.Unlock()
dg_shutdown.shutdown = state

if state {
setDashGoatReady(false)
}

}
18 changes: 17 additions & 1 deletion cmd/dashgoat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import (
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"

"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -87,6 +90,7 @@ func main() {
flag.StringVar(&config.IPport, "ipport", ":2000", "Specify <ip>:<port>")
flag.StringVar(&config.WebPath, "webpath", "/", "Specify added url http://host:port/<path> Default: /")
flag.StringVar(&config.UpdateKey, "updatekey", "changeme", "Specify key to API update")
flag.StringVar(&config.UrnKey, "urnkey", "", "Specify key for Heartbeat and Alertmanager")
flag.StringVar(&config.DashName, "dashname", "", "Dashboard name")
flag.StringVar(&configfile, "configfile", "dashgoat.yaml", "Name of configfile")
flag.StringVar(&buddy_cli.Url, "buddyurl", "", "Buddy url")
Expand Down Expand Up @@ -161,7 +165,19 @@ func main() {
go findBuddy(config.BuddyHosts)
go initPagerDuty()

//catch interrupt or sigterm
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
go func() {
for sig := range sigs {
logger.Error("main", "signal", sig)
fmt.Println(sig)
setDashGoatShutdown(true)
time.Sleep(2 * time.Second)
os.Exit(0)
}
}()

// Start server
e.Logger.Fatal(e.Start(config.IPport))

}
3 changes: 3 additions & 0 deletions cmd/dashgoat/timeoutprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func lostProbeTimer() {
}
}
time.Sleep(time.Duration(interval) * time.Second)
if isDashGoatShutdown() {
return
}
}

}
Expand Down
3 changes: 3 additions & 0 deletions cmd/dashgoat/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func ttlHousekeeping() {
defer ticker.Stop()

for range ticker.C {
if isDashGoatShutdown() {
break
}
statusList := readStatusList()
currentUnixTimestamp := time.Now().Unix()
for key, serviceState := range statusList {
Expand Down

0 comments on commit ff035fa

Please sign in to comment.