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: "",
|
StopCmd: "",
|
||||||
Restart: "",
|
Restart: "",
|
||||||
CrashOnSafeExit: true,
|
CrashOnSafeExit: true,
|
||||||
ServiceRunPath: "",
|
|
||||||
restartCount: 0,
|
restartCount: 0,
|
||||||
stopChannel: make(chan bool),
|
stopChannel: make(chan bool),
|
||||||
LogOutput: true,
|
LogOutput: true,
|
||||||
|
state: EnitServiceUnloaded,
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
||||||
logger.Printf("Error: could not read service file %s", path.Join(serviceConfigDir, "services", entry.Name()))
|
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.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)
|
Services = append(Services, &service)
|
||||||
|
|
||||||
logger.Printf("Service (%s) has been initialized!\n", service.Name)
|
logger.Printf("Service (%s) has been initialized!\n", service.Name)
|
||||||
|
|||||||
+9
-39
@@ -6,7 +6,6 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@@ -45,7 +44,7 @@ type EnitService struct {
|
|||||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||||
Restart string `yaml:"restart,omitempty"`
|
Restart string `yaml:"restart,omitempty"`
|
||||||
LogOutput bool `yaml:"log_output,omitempty"`
|
LogOutput bool `yaml:"log_output,omitempty"`
|
||||||
ServiceRunPath string
|
state EnitServiceState
|
||||||
processID int
|
processID int
|
||||||
restartCount int
|
restartCount int
|
||||||
stopChannel chan bool
|
stopChannel chan bool
|
||||||
@@ -80,26 +79,6 @@ func (service *EnitService) GetProcess() *os.Process {
|
|||||||
return 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) {
|
func (service *EnitService) GetLogFile() (file *os.File, err error) {
|
||||||
// Create esvm log directory
|
// Create esvm log directory
|
||||||
err = os.MkdirAll("/var/log/esvm", 0755)
|
err = os.MkdirAll("/var/log/esvm", 0755)
|
||||||
@@ -138,7 +117,7 @@ func (service *EnitService) StartService() error {
|
|||||||
if service == nil {
|
if service == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if service.GetCurrentState() == EnitServiceRunning {
|
if service.state == EnitServiceRunning {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,16 +148,7 @@ func (service *EnitService) StartService() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
service.processID = cmd.Process.Pid
|
service.processID = cmd.Process.Pid
|
||||||
|
service.state = EnitServiceRunning
|
||||||
err := service.setCurrentState(EnitServiceRunning)
|
|
||||||
if err != nil {
|
|
||||||
// Close log file if not nil
|
|
||||||
if logFile != nil {
|
|
||||||
logFile.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := cmd.Wait()
|
err := cmd.Wait()
|
||||||
@@ -195,18 +165,18 @@ func (service *EnitService) StartService() error {
|
|||||||
if service.Type == "simple" && err == nil {
|
if service.Type == "simple" && err == nil {
|
||||||
service.restartCount = 0
|
service.restartCount = 0
|
||||||
if service.ExitMethod != "stop_command" {
|
if service.ExitMethod != "stop_command" {
|
||||||
_ = service.setCurrentState(EnitServiceCompleted)
|
service.state = EnitServiceCompleted
|
||||||
} else {
|
} else {
|
||||||
_ = service.setCurrentState(EnitServiceRunning)
|
service.state = EnitServiceRunning
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !service.CrashOnSafeExit {
|
if !service.CrashOnSafeExit {
|
||||||
logger.Printf("Service (%s) has exited\n", service.Name)
|
logger.Printf("Service (%s) has exited\n", service.Name)
|
||||||
_ = service.setCurrentState(EnitServiceStopped)
|
service.state = EnitServiceStopped
|
||||||
} else {
|
} else {
|
||||||
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
||||||
_ = service.setCurrentState(EnitServiceCrashed)
|
service.state = EnitServiceCrashed
|
||||||
}
|
}
|
||||||
|
|
||||||
if service.Restart == "always" {
|
if service.Restart == "always" {
|
||||||
@@ -231,7 +201,7 @@ func (service *EnitService) StartService() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (service *EnitService) StopService() error {
|
func (service *EnitService) StopService() error {
|
||||||
if service.GetCurrentState() != EnitServiceRunning {
|
if service.state != EnitServiceRunning {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,7 +209,7 @@ func (service *EnitService) StopService() error {
|
|||||||
|
|
||||||
newServiceStatus := EnitServiceCrashed
|
newServiceStatus := EnitServiceCrashed
|
||||||
defer func() {
|
defer func() {
|
||||||
service.setCurrentState(newServiceStatus)
|
service.state = newServiceStatus
|
||||||
service.processID = 0
|
service.processID = 0
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -222,7 +222,7 @@ func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
|||||||
|
|
||||||
statusMap := make(map[string]any)
|
statusMap := make(map[string]any)
|
||||||
statusMap["name"] = service.Name
|
statusMap["name"] = service.Name
|
||||||
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||||
statusMap["process_id"] = service.processID
|
statusMap["process_id"] = service.processID
|
||||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
||||||
|
|
||||||
@@ -244,7 +244,7 @@ func handleListServicesCommand(conn net.Conn, _ map[string]any) {
|
|||||||
for _, service := range Services {
|
for _, service := range Services {
|
||||||
statusMap := make(map[string]any)
|
statusMap := make(map[string]any)
|
||||||
statusMap["name"] = service.Name
|
statusMap["name"] = service.Name
|
||||||
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||||
statusMap["process_id"] = service.processID
|
statusMap["process_id"] = service.processID
|
||||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
||||||
servicesMap["services"] = append(servicesMap["services"].([]map[string]any), statusMap)
|
servicesMap["services"] = append(servicesMap["services"].([]map[string]any), statusMap)
|
||||||
|
|||||||
Reference in New Issue
Block a user