mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 02:26:11 +00:00
Store service process ID in struct
This commit is contained in:
+9
-1
@@ -255,6 +255,7 @@ func main() {
|
|||||||
serviceState := returnedJsonData["state"].(string)
|
serviceState := returnedJsonData["state"].(string)
|
||||||
serviceEnabled := returnedJsonData["is_enabled"].(bool)
|
serviceEnabled := returnedJsonData["is_enabled"].(bool)
|
||||||
serviceStage := int(returnedJsonData["stage"].(float64))
|
serviceStage := int(returnedJsonData["stage"].(float64))
|
||||||
|
processID := int(returnedJsonData["process_id"].(float64))
|
||||||
|
|
||||||
fmt.Printf("Name: %s\n", flag.Arg(2))
|
fmt.Printf("Name: %s\n", flag.Arg(2))
|
||||||
fmt.Printf("State: %s\n", serviceState)
|
fmt.Printf("State: %s\n", serviceState)
|
||||||
@@ -263,6 +264,9 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||||
}
|
}
|
||||||
|
if serviceState == "running" {
|
||||||
|
fmt.Printf("Process ID: %d\n", processID)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
} else if flag.Arg(1) == "list" {
|
} else if flag.Arg(1) == "list" {
|
||||||
@@ -317,7 +321,8 @@ func main() {
|
|||||||
serviceName := serviceMap.(map[string]any)["name"].(string)
|
serviceName := serviceMap.(map[string]any)["name"].(string)
|
||||||
serviceState := serviceMap.(map[string]any)["state"].(string)
|
serviceState := serviceMap.(map[string]any)["state"].(string)
|
||||||
serviceEnabled := serviceMap.(map[string]any)["is_enabled"].(bool)
|
serviceEnabled := serviceMap.(map[string]any)["is_enabled"].(bool)
|
||||||
serviceStage := serviceMap.(map[string]any)["stage"].(int)
|
serviceStage := int(serviceMap.(map[string]any)["stage"].(float64))
|
||||||
|
processID := int(serviceMap.(map[string]any)["process_id"].(float64))
|
||||||
|
|
||||||
fmt.Printf("Name: %s\n", serviceName)
|
fmt.Printf("Name: %s\n", serviceName)
|
||||||
fmt.Printf("State: %s\n", serviceState)
|
fmt.Printf("State: %s\n", serviceState)
|
||||||
@@ -326,6 +331,9 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||||
}
|
}
|
||||||
|
if serviceState == "running" {
|
||||||
|
fmt.Printf("Process ID: %d\n", processID)
|
||||||
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -199,7 +199,7 @@ func Init() {
|
|||||||
logger.Fatalf("Error: could not initialize ESVM: %s", err)
|
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)
|
||||||
}
|
}
|
||||||
@@ -261,7 +261,7 @@ func Destroy() {
|
|||||||
func GetServiceByName(name string) *EnitService {
|
func GetServiceByName(name string) *EnitService {
|
||||||
for _, service := range Services {
|
for _, service := range Services {
|
||||||
if service.Name == name {
|
if service.Name == name {
|
||||||
return &service
|
return service
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+16
-40
@@ -46,11 +46,12 @@ type EnitService struct {
|
|||||||
Restart string `yaml:"restart,omitempty"`
|
Restart string `yaml:"restart,omitempty"`
|
||||||
LogOutput bool `yaml:"log_output,omitempty"`
|
LogOutput bool `yaml:"log_output,omitempty"`
|
||||||
ServiceRunPath string
|
ServiceRunPath string
|
||||||
|
processID int
|
||||||
restartCount int
|
restartCount int
|
||||||
stopChannel chan bool
|
stopChannel chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var Services = make([]EnitService, 0)
|
var Services = make([]*EnitService, 0)
|
||||||
var EnabledServices = make(map[int][]string)
|
var EnabledServices = make(map[int][]string)
|
||||||
var startedServicesOrder = make([]string, 0)
|
var startedServicesOrder = make([]string, 0)
|
||||||
|
|
||||||
@@ -74,31 +75,11 @@ func (service *EnitService) GetUnmetDependencies() (missingDependencies []string
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (service *EnitService) GetProcess() *os.Process {
|
func (service *EnitService) GetProcess() *os.Process {
|
||||||
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "process"))
|
process, _ := os.FindProcess(service.processID)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return process
|
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 {
|
func (service *EnitService) GetCurrentState() EnitServiceState {
|
||||||
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "state"))
|
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "state"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -187,17 +168,9 @@ func (service *EnitService) StartService() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err := service.setProcessID(cmd.Process.Pid)
|
service.processID = cmd.Process.Pid
|
||||||
if err != nil {
|
|
||||||
// Close log file if not nil
|
|
||||||
if logFile != nil {
|
|
||||||
logFile.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
err := service.setCurrentState(EnitServiceRunning)
|
||||||
}
|
|
||||||
|
|
||||||
err = service.setCurrentState(EnitServiceRunning)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Close log file if not nil
|
// Close log file if not nil
|
||||||
if logFile != nil {
|
if logFile != nil {
|
||||||
@@ -243,6 +216,8 @@ func (service *EnitService) StartService() error {
|
|||||||
_ = service.StartService()
|
_ = service.StartService()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
service.processID = 0
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Add to started services order slice
|
// Add to started services order slice
|
||||||
@@ -263,12 +238,13 @@ func (service *EnitService) StopService() error {
|
|||||||
logger.Printf("Stopping service (%s)...", service.Name)
|
logger.Printf("Stopping service (%s)...", service.Name)
|
||||||
|
|
||||||
newServiceStatus := EnitServiceCrashed
|
newServiceStatus := EnitServiceCrashed
|
||||||
defer service.setCurrentState(newServiceStatus)
|
defer func() {
|
||||||
defer service.setProcessID(0)
|
service.setCurrentState(newServiceStatus)
|
||||||
|
service.processID = 0
|
||||||
|
}()
|
||||||
|
|
||||||
if service.ExitMethod == "kill" {
|
if service.ExitMethod == "kill" {
|
||||||
process := service.GetProcess()
|
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
|
||||||
if err := process.Signal(syscall.Signal(0)); err != nil {
|
|
||||||
newServiceStatus = EnitServiceStopped
|
newServiceStatus = EnitServiceStopped
|
||||||
logger.Printf("Service (%s) has stopped (Process already dead)", service.Name)
|
logger.Printf("Service (%s) has stopped (Process already dead)", service.Name)
|
||||||
return nil
|
return nil
|
||||||
@@ -277,8 +253,8 @@ func (service *EnitService) StopService() error {
|
|||||||
go func() { service.stopChannel <- true }()
|
go func() { service.stopChannel <- true }()
|
||||||
|
|
||||||
// Send SIGTERM signal to process
|
// Send SIGTERM signal to process
|
||||||
if err := process.Signal(syscall.SIGTERM); err != nil {
|
if err := service.GetProcess().Signal(syscall.SIGTERM); err != nil {
|
||||||
process.Signal(syscall.SIGKILL)
|
service.GetProcess().Signal(syscall.SIGKILL)
|
||||||
return fmt.Errorf("could not stop process gracefully")
|
return fmt.Errorf("could not stop process gracefully")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +262,7 @@ func (service *EnitService) StopService() error {
|
|||||||
exited := make(chan bool)
|
exited := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
if err := process.Signal(syscall.Signal(0)); err != nil {
|
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -296,7 +272,7 @@ func (service *EnitService) StopService() error {
|
|||||||
select {
|
select {
|
||||||
case <-exited:
|
case <-exited:
|
||||||
case <-time.After(5 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
process.Signal(syscall.SIGKILL)
|
service.GetProcess().Signal(syscall.SIGKILL)
|
||||||
return fmt.Errorf("could not stop process gracefully")
|
return fmt.Errorf("could not stop process gracefully")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -223,6 +223,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.GetCurrentState()]
|
||||||
|
statusMap["process_id"] = service.processID
|
||||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
||||||
|
|
||||||
// Encode map to json string
|
// Encode map to json string
|
||||||
@@ -244,6 +245,7 @@ func handleListServicesCommand(conn net.Conn, _ 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.GetCurrentState()]
|
||||||
|
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