Skip to content

Instantly share code, notes, and snippets.

@pims

pims/runner.go Secret

Created Jan 21, 2021
Embed
What would you like to do?
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/oklog/run"
)
func main() {
args := os.Args[1:]
fmt.Println("[runner] starting:", strings.Join(args, ","))
cmd := exec.CommandContext(context.Background(), "envoy", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
var g run.Group
{
g.Add(func() error {
return cmd.Run()
}, func(err error) {
fmt.Println("[runner] cancelling")
time.Sleep(1 * time.Second)
resp, err = http.Post("http://fly-local-6pn:9901/drain_listeners?graceful", "application/json", nil)
if err != nil {
log.Println(err)
os.Exit(1)
}
fmt.Println("[runner] GOT HTTP: ", resp.StatusCode)
fmt.Println("[runner] done drain_listeners?graceful, sleeping 10s")
time.Sleep(10 * time.Second)
if err := syscall.Kill(cmd.Process.Pid, syscall.SIGKILL); err != nil {
fmt.Println("[runner] failed to kill envoy", err)
os.Exit(1)
}
fmt.Println("[runner] exiting")
os.Exit(0)
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
execute, interrupt := run.SignalHandler(ctx, syscall.SIGINT)
g.Add(execute, interrupt)
}
log.Printf("exit: %v\n", g.Run())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment