diff --git a/client/client.go b/client/client.go index dca3a89..ed3d1f7 100644 --- a/client/client.go +++ b/client/client.go @@ -2,6 +2,7 @@ package client import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -32,9 +33,25 @@ import ( ) type runcommand struct { - Exec string `json:"exec"` - Args []string `json:"args"` - Version string `json:"version"` + Exec string `json:"exec"` + Args []string `json:"args"` + Version string `json:"version"` + AutoRestart bool `json:"auto_restart"` +} + +type easyruncommand runcommand + +func (t *runcommand) UnmarshalJSON(b []byte) error { + easy := easyruncommand{ + Version: "latest", + AutoRestart: true, + } + if err := json.Unmarshal(b, &easy); err != nil { + return err + } + + *t = runcommand(easy) + return nil } type clientConfig struct { @@ -79,6 +96,7 @@ type procmeta struct { args []string version string verpath string + recover bool state int32 stdin io.WriteCloser } @@ -459,16 +477,40 @@ func NewClient(standalone bool) (HoustonClient, error) { id64, _ := strconv.ParseInt(idstr, 10, 0) id := int32(id64) + var found *procmeta hc.childProcs = gocommon.ShrinkSlice(hc.childProcs, func(e *procmeta) bool { if e.id == id { - e.cmd.Wait() - e.cmd.Process.Release() + found = e return true } return false }) - op.Refresh(context.Background(), hc.makeOperationQueryRequest()) + if found != nil { + found.cmd.Wait() + found.cmd.Process.Release() + + if found.recover { + time.Sleep(time.Second) + sr := shared.StartProcessRequest{ + Name: found.name, + Version: found.version, + Args: found.args, + AutoRestart: found.recover, + } + + if err := hc.startChildProcess(&sr); err != nil { + logger.Println("startChildProcess failed by autorun :", err) + logger.ErrorWithCallStack(err) + } else { + logger.Println("recover success :", sr) + } + } + } + + if op != nil { + op.Refresh(context.Background(), hc.makeOperationQueryRequest()) + } } } } @@ -541,9 +583,10 @@ func (hc *houstonClient) Start() { // service 서비스 for i := 0; i < count; i++ { sr := shared.StartProcessRequest{ - Name: service, - Version: cmd.Version, - Args: append([]string{cmd.Exec}, cmd.Args...), + Name: service, + Version: cmd.Version, + Args: append([]string{cmd.Exec}, cmd.Args...), + AutoRestart: cmd.AutoRestart, } if err := hc.startChildProcess(&sr); err != nil { diff --git a/client/operation.go b/client/operation.go index e71c757..10a2ad8 100644 --- a/client/operation.go +++ b/client/operation.go @@ -216,6 +216,7 @@ func prepareProcessLaunch(storageRoot string, req *shared.StartProcessRequest) ( name: req.Name, args: req.Args, version: req.Version, + recover: req.AutoRestart, verpath: verpath, state: int32(protos.ProcessState_Stopped), stdin: stdin, diff --git a/shared/operator.go b/shared/operator.go index e7c5bee..e702e68 100644 --- a/shared/operator.go +++ b/shared/operator.go @@ -27,9 +27,10 @@ type WithdrawRequest struct { } type StartProcessRequest struct { - Name string - Version string - Args []string + Name string + Version string + Args []string + AutoRestart bool } type StopProcessRequest struct {