func handleAgent(ll *log.Log, cc io.ReadWriteCloser) { defer cc.Close() client := osprocess.New(nil) agent := rpc.RepresentAgent(cc, client) for i := 0; ; i++ { ll.Info("starting program") res, err := agent.StartProcess(&rpc.StartProcessReq{ ProgramName: fmt.Sprintf("echoer v%d i was told to do this by the supervisor", i), }) if err != nil { ll.Err(err).Error("couldn't send command to remote agent") return } ll.KV("process.id", res.ProcessID).Info("agent is running program") time.Sleep(3 * time.Second) ll.Info("stopping program") _, err = agent.StopProcess(&rpc.StopProcessReq{ProcessID: res.ProcessID, Timeout: time.Second}) if err != nil { ll.Err(err).Error("couldn't send command to remote agent") return } } }
func main() { supervisord := flag.String("supervisord", "127.0.0.1:1337", "address where the supervisor can be reached") flag.Parse() policy := agent.PolicyAllAtOnce() policy = agent.PolicyStartBeforeStop(policy) policy = agent.PolicyStopTimeout(policy, time.Second) ll := log.KV("app", appName) ll.Info("starting") defer ll.Info("all done") client := osprocess.New(osprocess.NopInstaller()) // client = container.Log(client, log.KV("container", "osprocess")) ag := agent.New(client) for { cc, err := net.DialTimeout("tcp", *supervisord, 10*time.Second) if err != nil { ll.Err(err).Error("can't dial supervisord") continue } if err := rpc.OperateAgent(ag, client, cc); err != nil { ll.Err(err).Error("can't operate agent over RPC") } } }
func main() { policy := agent.PolicyAllAtOnce() policy = agent.PolicyStartBeforeStop(policy) policy = agent.PolicyStopTimeout(policy, time.Second) ll := log.KV("app", appName) ll.Info("starting") // client, err := docker.New(os.Getenv("DOCKERD_PORT"), "") // if err != nil { // log.Err(err).Fatal("can't create docker client") // } // client = container.Log(client, log.KV("container", "docker")) client := osprocess.New(osprocess.NopInstaller()) // client = container.Log(client, log.KV("container", "osprocess")) img := client.ProgramID("echoer v1") ll = ll.KV("program.id", img) ag := agent.New(client) ll.Info("starting program") if _, err := ag.StartProcess(img); err != nil { ll.Err(err).Fatal("couldn't start image") } time.Sleep(3 * time.Second) if _, err := ag.StartProcess(img); err != nil { ll.Err(err).Fatal("couldn't start image") } time.Sleep(3 * time.Second) ll.Info("restarting") if err := ag.RestartProgram(policy, img); err != nil { ll.Err(err).Fatal("couldn't restart image") } ll.Info("restarted, running") time.Sleep(3 * time.Second) newImg := client.ProgramID("echoer v2") ll.Info("upgrading") if err := ag.UpgradeProgram(policy, img, newImg); err != nil { ll.Err(err).Fatal("couldn't restart image") } ll.Info("upgraded, running") time.Sleep(3 * time.Second) ll.Info("stopping all processes") if err := ag.StopProgram(newImg, 10*time.Second); err != nil { ll.Err(err).Fatal("couldn't stop image") } ll.Info("all done!") }