From 0d4a39a68b81007bdfa5c741d9134b83420a776e Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sun, 23 Feb 2025 20:19:41 +0200 Subject: [PATCH] Add restart service option --- cmd/esvm/main.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/cmd/esvm/main.go b/cmd/esvm/main.go index b2ab7cb..ecd1924 100644 --- a/cmd/esvm/main.go +++ b/cmd/esvm/main.go @@ -35,6 +35,7 @@ type EnitService struct { StartCmd string `yaml:"start_cmd"` ExitMethod string `yaml:"exit_method"` StopCmd string `yaml:"stop_cmd,omitempty"` + Restart bool `yaml:"restart,omitempty"` ServiceRunPath string stopChannel chan bool } @@ -118,8 +119,15 @@ func Init() error { } service := EnitService{ - StopCmd: "", - stopChannel: make(chan bool), + Name: "", + Description: "", + Type: "", + StartCmd: "", + ExitMethod: "", + StopCmd: "", + Restart: false, + ServiceRunPath: "", + stopChannel: make(chan bool), } if err := yaml.Unmarshal(bytes, &service); err != nil { log.Printf("Could not read service file at %s!", path.Join(serviceConfigDir, "services", entry.Name())) @@ -235,6 +243,16 @@ func (service *EnitService) StartService() error { return err } + err := service.setProcessID(cmd.Process.Pid) + if err != nil { + return err + } + + err = service.setCurrentState(EnitServiceRunning) + if err != nil { + return err + } + go func() { err := cmd.Wait() select { @@ -245,20 +263,13 @@ func (service *EnitService) StartService() error { _ = service.setCurrentState(EnitServiceCompleted) } _ = service.setCurrentState(EnitServiceCrashed) + + if service.Restart { + _ = service.StartService() + } } - }() - err := service.setProcessID(cmd.Process.Pid) - if err != nil { - return err - } - - err = service.setCurrentState(EnitServiceRunning) - if err != nil { - return err - } - return nil }