Add restart service option

This commit is contained in:
EnumDev 2025-02-23 20:19:41 +02:00
parent 5f9f7ab825
commit 0d4a39a68b

View File

@ -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,7 +119,14 @@ func Init() error {
} }
service := EnitService{ service := EnitService{
Name: "",
Description: "",
Type: "",
StartCmd: "",
ExitMethod: "",
StopCmd: "", StopCmd: "",
Restart: false,
ServiceRunPath: "",
stopChannel: make(chan bool), stopChannel: make(chan bool),
} }
if err := yaml.Unmarshal(bytes, &service); err != nil { if err := yaml.Unmarshal(bytes, &service); err != nil {
@ -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
} }