-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.go
49 lines (42 loc) · 1.04 KB
/
cleanup.go
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
package nrod
import (
"os"
"os/signal"
"github.com/michaelraskansky/ntokin/pkg/dts"
)
func cleanUpOnInterrupt(ctx *dts.Ctx) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
select {
case <-ctx.KillSignal:
cleanUpAndExit(ctx)
case <-c:
cleanUpAndExit(ctx)
case <-ctx.Context.Done():
cleanUpAndExit(ctx)
}
}()
}
func cleanUpAndExit(ctx *dts.Ctx) {
ctx.Log.Infof("Cleaning-up...")
cleanUp(ctx)
ctx.Log.Infof("Cleaned-up; exiting.")
os.Exit(1)
}
func cleanUp(ctx *dts.Ctx) {
for subscriptionName, subscription := range subscriptions {
if subscription != nil {
ctx.Log.Infof("Unsubscribing from subscription %s...", subscriptionName)
subscription.Unsubscribe()
ctx.Log.Infof("Successfully unsubscribed from subscription %s.", subscriptionName)
delete(subscriptions, subscriptionName)
subscriptionWaitGroup.Done()
}
}
if connection != nil {
ctx.Log.Infof("Disconnecting from connection...")
connection.Disconnect()
ctx.Log.Infof("Sucessfully disconnected from connection.")
}
}