Skip to content

Commit

Permalink
Merge pull request #9 from suker200/support_watch_preemptible_events
Browse files Browse the repository at this point in the history
Support watch preemptible events
  • Loading branch information
suker200 authored Dec 6, 2021
2 parents 662bbec + 48daf75 commit c0e32d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func main() {
resources.eventVMWatching()
}()

go func() {
resources.eventPrempVMWatching()
}()

resources.process(pusherCfg)

for {
Expand Down
21 changes: 21 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit c0e32d2

Please sign in to comment.