mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 02:26:11 +00:00
Add restart service option
This commit is contained in:
+24
-13
@@ -35,6 +35,7 @@ type EnitService struct {
|
|||||||
StartCmd string `yaml:"start_cmd"`
|
StartCmd string `yaml:"start_cmd"`
|
||||||
ExitMethod string `yaml:"exit_method"`
|
ExitMethod string `yaml:"exit_method"`
|
||||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||||
|
Restart bool `yaml:"restart,omitempty"`
|
||||||
ServiceRunPath string
|
ServiceRunPath string
|
||||||
stopChannel chan bool
|
stopChannel chan bool
|
||||||
}
|
}
|
||||||
@@ -118,8 +119,15 @@ func Init() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
service := EnitService{
|
service := EnitService{
|
||||||
StopCmd: "",
|
Name: "",
|
||||||
stopChannel: make(chan bool),
|
Description: "",
|
||||||
|
Type: "",
|
||||||
|
StartCmd: "",
|
||||||
|
ExitMethod: "",
|
||||||
|
StopCmd: "",
|
||||||
|
Restart: false,
|
||||||
|
ServiceRunPath: "",
|
||||||
|
stopChannel: make(chan bool),
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
||||||
log.Printf("Could not read service file at %s!", path.Join(serviceConfigDir, "services", entry.Name()))
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := service.setProcessID(cmd.Process.Pid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = service.setCurrentState(EnitServiceRunning)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := cmd.Wait()
|
err := cmd.Wait()
|
||||||
select {
|
select {
|
||||||
@@ -245,20 +263,13 @@ func (service *EnitService) StartService() error {
|
|||||||
_ = service.setCurrentState(EnitServiceCompleted)
|
_ = service.setCurrentState(EnitServiceCompleted)
|
||||||
}
|
}
|
||||||
_ = service.setCurrentState(EnitServiceCrashed)
|
_ = 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user