Add restart limit to automatically restarting services
This commit is contained in:
parent
c639daca5a
commit
70df28d90e
@ -37,6 +37,7 @@ type EnitService struct {
|
|||||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||||
Restart bool `yaml:"restart,omitempty"`
|
Restart bool `yaml:"restart,omitempty"`
|
||||||
ServiceRunPath string
|
ServiceRunPath string
|
||||||
|
restartCount int
|
||||||
stopChannel chan bool
|
stopChannel chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,6 +143,7 @@ func Init() {
|
|||||||
StopCmd: "",
|
StopCmd: "",
|
||||||
Restart: false,
|
Restart: false,
|
||||||
ServiceRunPath: "",
|
ServiceRunPath: "",
|
||||||
|
restartCount: 0,
|
||||||
stopChannel: make(chan bool),
|
stopChannel: make(chan bool),
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
||||||
@ -281,15 +283,18 @@ func (service *EnitService) StartService() error {
|
|||||||
err := cmd.Wait()
|
err := cmd.Wait()
|
||||||
select {
|
select {
|
||||||
case <-service.stopChannel:
|
case <-service.stopChannel:
|
||||||
|
service.restartCount = 0
|
||||||
_ = service.setCurrentState(EnitServiceStopped)
|
_ = service.setCurrentState(EnitServiceStopped)
|
||||||
default:
|
default:
|
||||||
if service.Type == "simple" && err == nil {
|
if service.Type == "simple" && err == nil {
|
||||||
|
service.restartCount = 0
|
||||||
_ = service.setCurrentState(EnitServiceCompleted)
|
_ = service.setCurrentState(EnitServiceCompleted)
|
||||||
}
|
}
|
||||||
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
||||||
_ = service.setCurrentState(EnitServiceCrashed)
|
_ = service.setCurrentState(EnitServiceCrashed)
|
||||||
|
|
||||||
if service.Restart {
|
if service.Restart && service.restartCount < 5 {
|
||||||
|
service.restartCount++
|
||||||
_ = service.StartService()
|
_ = service.StartService()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user