summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/github.com/okzk/sdnotify@v0.0.0-20180710141335-d9becc38acbd/sample/main.go
blob: dd9d49e78f92bd760f80e97bcad0bf08b5200c32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/okzk/sdnotify"
)

func reload() {
	// Tells the service manager that the service is reloading its configuration.
	sdnotify.Reloading()

	log.Println("reloading...")
	time.Sleep(time.Second)
	log.Println("reloaded.")

	// The service must also send a "READY" notification when it completed reloading its configuration.
	sdnotify.Ready()
}

func main() {
	log.Println("starting...")
	time.Sleep(time.Second)
	log.Println("started.")

	// Tells the service manager that service startup is finished.
	sdnotify.Ready()

	go func() {
		tick := time.Tick(30 * time.Second)
		for {
			<-tick
			log.Println("watchdog reporting")
			sdnotify.Watchdog()
		}
	}()

	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	for sig := range sigCh {
		if sig == syscall.SIGHUP {
			reload()
		} else {
			break
		}
	}

	// Tells the service manager that the service is beginning its shutdown.
	sdnotify.Stopping()

	log.Println("existing...")
	time.Sleep(time.Second)
}