add first supervisor process
This commit is contained in:
10
dist/supervisorTemplate.json
vendored
Normal file
10
dist/supervisorTemplate.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"processes":[{
|
||||||
|
"name":"Database DBM",
|
||||||
|
"executePath":"dist/test-windows-amd64.exe",
|
||||||
|
"workingDirectory":".",
|
||||||
|
"startDelay":2,
|
||||||
|
"priority":0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
57
models/supervisor.go
Normal file
57
models/supervisor.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Supervisor struct {
|
||||||
|
Processes []*SupervisorProcess `json:"processes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SupervisorProcess struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ExecutePath string `json:"executePath"`
|
||||||
|
WorkingDirectory string `json:"workingDirectory,omitempty"`
|
||||||
|
StartDelay int `json:"startDelay,omitempty"`
|
||||||
|
Priority int `json:"priority,omitempty"`
|
||||||
|
Arguments []string `json:"arguments,omitempty"`
|
||||||
|
process *exec.Cmd `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadTemplate(path string) (supervisor Supervisor, err error) {
|
||||||
|
content, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(content, &supervisor)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Supervisor) StartProcesses() error {
|
||||||
|
sort.Slice(s.Processes, func(i, j int) bool {
|
||||||
|
return s.Processes[i].Priority < s.Processes[j].Priority
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, p := range s.Processes {
|
||||||
|
go func() {
|
||||||
|
p.process = exec.Command(p.ExecutePath, p.Arguments...)
|
||||||
|
//cmd.Env = append(cmd.Env, "MY_VAR=some_value")
|
||||||
|
p.process.Dir = p.WorkingDirectory
|
||||||
|
|
||||||
|
output, err := p.process.Output()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Output:", string(output))
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
fmt.Println(3)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user