diff --git a/main.go b/main.go index 49880b9..671fc3a 100644 --- a/main.go +++ b/main.go @@ -116,6 +116,10 @@ func main() { resources.eventVMWatching() }() + go func() { + resources.eventPrempVMWatching() + }() + resources.process(pusherCfg) for { diff --git a/utils.go b/utils.go index 4b190fe..dcbfb35 100644 --- a/utils.go +++ b/utils.go @@ -133,8 +133,29 @@ func (r *resources) eventVMWatching() { if err := myProcess.Signal(syscall.SIGTERM); err != nil { fmt.Println(err) } + break } time.Sleep(time.Duration(2) * time.Second) } } } + +func (r *resources) eventPrempVMWatching() { + httpClient, _ := NewRetryHttpClient("", "") + pid := os.Getpid() + + myProcess, _ := os.FindProcess(pid) + + for { + if resp, err := httpClient.Do(context.Background(), nil, "http://metadata.google.internal/computeMetadata/v1/instance/preempted", map[string]string{"Metadata-Flavor": "Google"}, "GET"); err == nil { + if string(resp) != "FALSE" { + if err := myProcess.Signal(syscall.SIGTERM); err != nil { + fmt.Println(err) + } + break + } + time.Sleep(time.Duration(2) * time.Second) + } + } +} +