From 340c492ad5c97deeada3b5da853db4c4de9643aa Mon Sep 17 00:00:00 2001 From: shengxiang Date: Thu, 3 Sep 2015 22:10:46 +0800 Subject: [PATCH] add autostart param --- program.go | 47 ++++++++++++++++++++++++++++++++++++++++------- web.go | 18 +++++------------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/program.go b/program.go index 080e2ce..a93843c 100644 --- a/program.go +++ b/program.go @@ -29,6 +29,13 @@ const ( ST_FATAL = "fatal" ) +type Event int + +const ( + EVENT_START = Event(iota) + EVENT_STOP +) + type Program struct { *kproc.Process `json:"-"` Status string `json:"state"` @@ -52,6 +59,12 @@ func (p *Program) createLog() (*os.File, error) { return os.Create(logFile) } +func (p *Program) InputData(evevt Event) { + if p.Status == ST_PENDING { + go p.Run() + } +} + func (p *Program) Run() error { if err := p.Start(); err != nil { p.Status = ST_FATAL @@ -97,10 +110,11 @@ func (p *Program) Wait() (err error) { } type ProgramInfo struct { - Name string `json:"name"` - Command []string `json:"command"` - Dir string `json:"dir"` - Environ []string `json:"environ"` + Name string `json:"name"` + Command []string `json:"command"` + Dir string `json:"dir"` + Environ []string `json:"environ"` + AutoStart bool `json:"autostart"` } var programTable *ProgramTable @@ -110,6 +124,7 @@ func InitServer() { table: make(map[string]*Program, 10), ch: make(chan string), } + programTable.loadConfig() } type ProgramTable struct { @@ -123,9 +138,6 @@ var ( ) func (pt *ProgramTable) saveConfig() error { - if _, err := os.Stat(GOSUV_PROGRAM_CONFIG); err == nil { - // load config - } table := make(map[string]*ProgramInfo) for name, p := range pt.table { table[name] = p.Info @@ -139,6 +151,27 @@ func (pt *ProgramTable) saveConfig() error { return ioutil.WriteFile(GOSUV_PROGRAM_CONFIG, data, 0644) } +func (pt *ProgramTable) loadConfig() error { + cfgFd, err := os.Open(GOSUV_PROGRAM_CONFIG) + if err != nil { + return err + } + defer cfgFd.Close() + table := make(map[string]*ProgramInfo) + if err = json.NewDecoder(cfgFd).Decode(&table); err != nil { + return err + } + for name, pinfo := range table { + if program, err := buildProgram(pinfo); err == nil { + pt.table[name] = program + if pinfo.AutoStart { + program.InputData(EVENT_START) + } + } + } + return nil +} + func (pt *ProgramTable) AddProgram(p *Program) error { pt.mu.Lock() defer pt.mu.Unlock() diff --git a/web.go b/web.go index c24de7c..45f4c5d 100644 --- a/web.go +++ b/web.go @@ -8,8 +8,8 @@ import ( "os/exec" "time" - "github.com/lunny/tango" "github.com/lunny/log" + "github.com/lunny/tango" ) type JSONResponse struct { @@ -63,15 +63,7 @@ func addHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), 503) return } - go program.Run() - /* - if err = program.Start(); err != nil { - http.Error(w, err.Error(), 503) - return - } - - go program.Wait() - */ + program.InputData(EVENT_START) renderJSON(w, &JSONResponse{ Code: 200, @@ -94,7 +86,6 @@ func ServeAddr(host string, port int) error { InitServer() t := tango.New() - t.Group("/api", func(g *tango.Group) { g.Get("/version", versionHandler) g.Post("/shutdown", shutdownHandler) @@ -102,6 +93,7 @@ func ServeAddr(host string, port int) error { g.Get("/programs", statusHandler) }) - t.Run(fmt.Sprintf("%s:%d", host, port)) - return nil + addr := fmt.Sprintf("%s:%d", host, port) + t.Run(addr) + return fmt.Errorf("Address: %s has been used", addr) }