get program list from gosuv

master
codeskyblue 8 years ago
parent ca308edede
commit ec0f64c789

@ -34,6 +34,8 @@ type FSM struct {
mu sync.Mutex
state FSMState
handlers map[FSMState]map[FSMEvent]FSMHandler
StateChange func(oldState, newState FSMState)
}
func (f *FSM) AddHandler(state FSMState, event FSMEvent, hdlr FSMHandler) *FSM {
@ -53,6 +55,9 @@ func (f *FSM) State() FSMState {
}
func (f *FSM) SetState(newState FSMState) {
if f.StateChange != nil {
f.StateChange(f.state, newState)
}
f.state = newState
}
@ -90,13 +95,13 @@ var (
)
type Program struct {
Name string `yaml:"name"`
Command string `yaml:"command"`
Environ []string `yaml:"environ"`
Dir string `yaml:"directory"`
AutoStart bool `yaml:"autostart"` // change to *bool, which support unexpected
StartRetries int `yaml:"startretries"`
StartSeconds int `yaml:"startsecs"`
Name string `yaml:"name" json:"name"`
Command string `yaml:"command" json:"command"`
Environ []string `yaml:"environ" json:"environ"`
Dir string `yaml:"directory" json:"directory"`
AutoStart bool `yaml:"autostart" json:"autostart"` // change to *bool, which support unexpected
StartRetries int `yaml:"startretries" json:"startretries"`
StartSeconds int `yaml:"startsecs" json:"startsecs"`
// LogDir string `yaml:"logdir"`
}
@ -114,11 +119,12 @@ func (p *Program) Check() error {
}
type Process struct {
*FSM
Program
*FSM `json:"-"`
Program `json:"program"`
cmd *kexec.KCommand
stopC chan int
retryLeft int
Status string `json:"status"`
}
func (p *Process) buildCommand() *kexec.KCommand {
@ -163,6 +169,9 @@ func NewProcess(pg Program) *Process {
stopC: make(chan int),
retryLeft: pg.StartRetries,
}
pr.StateChange = func(_, newStatus FSMState) {
pr.Status = string(newStatus)
}
startFunc := func() {
pr.retryLeft = pr.StartRetries

@ -54,6 +54,21 @@
</tr>
</thead>
<tbody>
<tr v-for="p in programs">
<td v-text="p.program.name"></td>
<td v-text="p.status"></td>
<td>
<button class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-play"></span> Start
</button>
<button class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-stop"></span> Stop
</button>
<button class="btn btn-default btn-xs" disabled="true">
<span class="glyphicon glyphicon-minus"></span> Tailf
</button>
</td>
</tr>
<tr class="success">
<td>gohttpserver</td>
<td>Running(2h)</td>

@ -15,12 +15,15 @@ function getQueryString(name) {
var vm = new Vue({
el: "#app",
data: {
program: {
name: "",
command: "",
dir: "",
autoStart: true,
},
programs: [{
program: {
name: "gggg",
command: "",
dir: "",
autoStart: true,
},
status: "running",
}],
},
methods: {
addNewProgram: function() {
@ -69,8 +72,19 @@ Vue.filter('formatBytes', function(value) {
else return (bytes / 1073741824).toFixed(1) + " GB";
})
var refreshPrograms = function() {
$.ajax({
url: "/api/programs",
success: function(data) {
console.log(data)
vm.programs = data;
}
});
}
$(function() {
refreshPrograms();
$("#formNewProgram").submit(function(e) {
var url = "/api/programs",
data = $(this).serialize();

@ -31,7 +31,7 @@ func (s *Supervisor) programPath() string {
func (s *Supervisor) addOrUpdateProgram(pg Program) error {
origPg, ok := s.pgMap[pg.Name]
if ok {
log.Println("Orig:", origPg, "Curr:", pg)
// log.Println("Orig:", origPg, "Curr:", pg)
if !reflect.DeepEqual(origPg, &pg) {
log.Println("Update:", pg.Name)
origProc := s.procMap[pg.Name]
@ -127,6 +127,22 @@ func (s *Supervisor) hIndex(w http.ResponseWriter, r *http.Request) {
t.ExecuteTemplate(w, "index.html", nil)
}
func (s *Supervisor) hGetProgram(w http.ResponseWriter, r *http.Request) {
procs := make([]*Process, 0, len(s.pgs))
for _, pg := range s.pgs {
procs = append(procs, s.procMap[pg.Name])
}
log.Println(procs[0])
data, err := json.Marshal(procs)
log.Println(string(data))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(data)
}
func (s *Supervisor) hAddProgram(w http.ResponseWriter, r *http.Request) {
pg := Program{
Name: r.FormValue("name"),
@ -176,6 +192,7 @@ func init() {
}
r := mux.NewRouter()
r.HandleFunc("/", suv.hIndex)
r.HandleFunc("/api/programs", suv.hGetProgram).Methods("GET")
r.HandleFunc("/api/programs", suv.hAddProgram).Methods("POST")
fs := http.FileServer(http.Dir("res"))

Loading…
Cancel
Save