mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 02:26:11 +00:00
Store service state in struct
This commit is contained in:
+1
-12
@@ -152,10 +152,10 @@ func Init() {
|
||||
StopCmd: "",
|
||||
Restart: "",
|
||||
CrashOnSafeExit: true,
|
||||
ServiceRunPath: "",
|
||||
restartCount: 0,
|
||||
stopChannel: make(chan bool),
|
||||
LogOutput: true,
|
||||
state: EnitServiceUnloaded,
|
||||
}
|
||||
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
||||
logger.Printf("Error: could not read service file %s", path.Join(serviceConfigDir, "services", entry.Name()))
|
||||
@@ -188,17 +188,6 @@ func Init() {
|
||||
service.Restart = "false"
|
||||
}
|
||||
|
||||
service.ServiceRunPath = path.Join(runtimeServiceDir, service.Name)
|
||||
err = os.MkdirAll(path.Join(service.ServiceRunPath), 0755)
|
||||
if err != nil {
|
||||
logger.Fatalf("Error: could not initialize ESVM: %s", err)
|
||||
}
|
||||
|
||||
err = service.setCurrentState(EnitServiceUnloaded)
|
||||
if err != nil {
|
||||
logger.Fatalf("Error: could not initialize ESVM: %s", err)
|
||||
}
|
||||
|
||||
Services = append(Services, &service)
|
||||
|
||||
logger.Printf("Service (%s) has been initialized!\n", service.Name)
|
||||
|
||||
+9
-39
@@ -6,7 +6,6 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -45,7 +44,7 @@ type EnitService struct {
|
||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||
Restart string `yaml:"restart,omitempty"`
|
||||
LogOutput bool `yaml:"log_output,omitempty"`
|
||||
ServiceRunPath string
|
||||
state EnitServiceState
|
||||
processID int
|
||||
restartCount int
|
||||
stopChannel chan bool
|
||||
@@ -80,26 +79,6 @@ func (service *EnitService) GetProcess() *os.Process {
|
||||
return process
|
||||
}
|
||||
|
||||
func (service *EnitService) GetCurrentState() EnitServiceState {
|
||||
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "state"))
|
||||
if err != nil {
|
||||
return EnitServiceUnknown
|
||||
}
|
||||
|
||||
state, err := strconv.Atoi(strings.TrimSpace(string(bytes)))
|
||||
if err != nil {
|
||||
return EnitServiceUnknown
|
||||
}
|
||||
return EnitServiceState(state)
|
||||
}
|
||||
|
||||
func (service *EnitService) setCurrentState(state EnitServiceState) error {
|
||||
if err := os.WriteFile(path.Join(service.ServiceRunPath, "state"), []byte(strconv.Itoa(int(state))), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service *EnitService) GetLogFile() (file *os.File, err error) {
|
||||
// Create esvm log directory
|
||||
err = os.MkdirAll("/var/log/esvm", 0755)
|
||||
@@ -138,7 +117,7 @@ func (service *EnitService) StartService() error {
|
||||
if service == nil {
|
||||
return nil
|
||||
}
|
||||
if service.GetCurrentState() == EnitServiceRunning {
|
||||
if service.state == EnitServiceRunning {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -169,16 +148,7 @@ func (service *EnitService) StartService() error {
|
||||
}
|
||||
|
||||
service.processID = cmd.Process.Pid
|
||||
|
||||
err := service.setCurrentState(EnitServiceRunning)
|
||||
if err != nil {
|
||||
// Close log file if not nil
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
service.state = EnitServiceRunning
|
||||
|
||||
go func() {
|
||||
err := cmd.Wait()
|
||||
@@ -195,18 +165,18 @@ func (service *EnitService) StartService() error {
|
||||
if service.Type == "simple" && err == nil {
|
||||
service.restartCount = 0
|
||||
if service.ExitMethod != "stop_command" {
|
||||
_ = service.setCurrentState(EnitServiceCompleted)
|
||||
service.state = EnitServiceCompleted
|
||||
} else {
|
||||
_ = service.setCurrentState(EnitServiceRunning)
|
||||
service.state = EnitServiceRunning
|
||||
}
|
||||
return
|
||||
}
|
||||
if !service.CrashOnSafeExit {
|
||||
logger.Printf("Service (%s) has exited\n", service.Name)
|
||||
_ = service.setCurrentState(EnitServiceStopped)
|
||||
service.state = EnitServiceStopped
|
||||
} else {
|
||||
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
||||
_ = service.setCurrentState(EnitServiceCrashed)
|
||||
service.state = EnitServiceCrashed
|
||||
}
|
||||
|
||||
if service.Restart == "always" {
|
||||
@@ -231,7 +201,7 @@ func (service *EnitService) StartService() error {
|
||||
}
|
||||
|
||||
func (service *EnitService) StopService() error {
|
||||
if service.GetCurrentState() != EnitServiceRunning {
|
||||
if service.state != EnitServiceRunning {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -239,7 +209,7 @@ func (service *EnitService) StopService() error {
|
||||
|
||||
newServiceStatus := EnitServiceCrashed
|
||||
defer func() {
|
||||
service.setCurrentState(newServiceStatus)
|
||||
service.state = newServiceStatus
|
||||
service.processID = 0
|
||||
}()
|
||||
|
||||
|
||||
+2
-2
@@ -222,7 +222,7 @@ func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
|
||||
statusMap := make(map[string]any)
|
||||
statusMap["name"] = service.Name
|
||||
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
||||
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||
statusMap["process_id"] = service.processID
|
||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
||||
|
||||
@@ -244,7 +244,7 @@ func handleListServicesCommand(conn net.Conn, _ map[string]any) {
|
||||
for _, service := range Services {
|
||||
statusMap := make(map[string]any)
|
||||
statusMap["name"] = service.Name
|
||||
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
||||
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||
statusMap["process_id"] = service.processID
|
||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
||||
servicesMap["services"] = append(servicesMap["services"].([]map[string]any), statusMap)
|
||||
|
||||
Reference in New Issue
Block a user