Store service process ID in struct

This commit is contained in:
2025-09-24 18:58:46 +03:00
parent 8ed8efa9b6
commit 09d8b70009
4 changed files with 29 additions and 43 deletions
+2 -2
View File
@@ -199,7 +199,7 @@ func Init() {
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)
}
@@ -261,7 +261,7 @@ func Destroy() {
func GetServiceByName(name string) *EnitService {
for _, service := range Services {
if service.Name == name {
return &service
return service
}
}
return nil
+16 -40
View File
@@ -46,11 +46,12 @@ type EnitService struct {
Restart string `yaml:"restart,omitempty"`
LogOutput bool `yaml:"log_output,omitempty"`
ServiceRunPath string
processID int
restartCount int
stopChannel chan bool
}
var Services = make([]EnitService, 0)
var Services = make([]*EnitService, 0)
var EnabledServices = make(map[int][]string)
var startedServicesOrder = make([]string, 0)
@@ -74,31 +75,11 @@ func (service *EnitService) GetUnmetDependencies() (missingDependencies []string
}
func (service *EnitService) GetProcess() *os.Process {
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "process"))
if err != nil {
return nil
}
pid, err := strconv.Atoi(strings.TrimSpace(string(bytes)))
if err != nil {
return nil
}
process, err := os.FindProcess(pid)
if err != nil {
return nil
}
process, _ := os.FindProcess(service.processID)
return process
}
func (service *EnitService) setProcessID(pid int) error {
if err := os.WriteFile(path.Join(service.ServiceRunPath, "process"), []byte(strconv.Itoa(pid)), 0644); err != nil {
return err
}
return nil
}
func (service *EnitService) GetCurrentState() EnitServiceState {
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "state"))
if err != nil {
@@ -187,17 +168,9 @@ func (service *EnitService) StartService() error {
return err
}
err := service.setProcessID(cmd.Process.Pid)
if err != nil {
// Close log file if not nil
if logFile != nil {
logFile.Close()
}
service.processID = cmd.Process.Pid
return err
}
err = service.setCurrentState(EnitServiceRunning)
err := service.setCurrentState(EnitServiceRunning)
if err != nil {
// Close log file if not nil
if logFile != nil {
@@ -243,6 +216,8 @@ func (service *EnitService) StartService() error {
_ = service.StartService()
}
}
service.processID = 0
}()
// Add to started services order slice
@@ -263,12 +238,13 @@ func (service *EnitService) StopService() error {
logger.Printf("Stopping service (%s)...", service.Name)
newServiceStatus := EnitServiceCrashed
defer service.setCurrentState(newServiceStatus)
defer service.setProcessID(0)
defer func() {
service.setCurrentState(newServiceStatus)
service.processID = 0
}()
if service.ExitMethod == "kill" {
process := service.GetProcess()
if err := process.Signal(syscall.Signal(0)); err != nil {
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
newServiceStatus = EnitServiceStopped
logger.Printf("Service (%s) has stopped (Process already dead)", service.Name)
return nil
@@ -277,8 +253,8 @@ func (service *EnitService) StopService() error {
go func() { service.stopChannel <- true }()
// Send SIGTERM signal to process
if err := process.Signal(syscall.SIGTERM); err != nil {
process.Signal(syscall.SIGKILL)
if err := service.GetProcess().Signal(syscall.SIGTERM); err != nil {
service.GetProcess().Signal(syscall.SIGKILL)
return fmt.Errorf("could not stop process gracefully")
}
@@ -286,7 +262,7 @@ func (service *EnitService) StopService() error {
exited := make(chan bool)
go func() {
for {
if err := process.Signal(syscall.Signal(0)); err != nil {
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
break
}
}
@@ -296,7 +272,7 @@ func (service *EnitService) StopService() error {
select {
case <-exited:
case <-time.After(5 * time.Second):
process.Signal(syscall.SIGKILL)
service.GetProcess().Signal(syscall.SIGKILL)
return fmt.Errorf("could not stop process gracefully")
}
} else {
+2
View File
@@ -223,6 +223,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["process_id"] = service.processID
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
// Encode map to json string
@@ -244,6 +245,7 @@ func handleListServicesCommand(conn net.Conn, _ map[string]any) {
statusMap := make(map[string]any)
statusMap["name"] = service.Name
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
statusMap["process_id"] = service.processID
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
servicesMap["services"] = append(servicesMap["services"].([]map[string]any), statusMap)
}