Update Cli to handle the main process

This commit is contained in:
gwoo
2014-04-13 13:27:01 -07:00
parent 7fb476c7a1
commit c578df6c79
3 changed files with 52 additions and 27 deletions

View File

@@ -50,11 +50,11 @@ func init() {
func main() {
if *d == true {
daemon.Args = append(daemon.Args, os.Args[2:]...)
daemon.start(daemon.Name)
fmt.Printf(daemon.start(daemon.Name))
return
}
if len(flag.Args()) > 0 {
fmt.Printf("%s\n", Cli())
fmt.Printf("%s", Cli())
return
}
if len(flag.Args()) == 0 {
@@ -73,22 +73,45 @@ func Cli() string {
if sub == "list" {
o, _, err = req.Get("/")
}
if name == "" {
if sub == "start" {
daemon.Args = append(daemon.Args, os.Args[2:]...)
return daemon.start(daemon.Name)
}
_, _, err = daemon.find()
if err != nil {
return fmt.Sprintf("Error: %s.\n", err)
}
if sub == "show" {
return fmt.Sprintf("%s.\n", daemon.String())
}
if sub == "stop" {
message := daemon.stop()
return message
}
if sub == "restart" {
ch, message := daemon.restart()
fmt.Print(message)
return fmt.Sprintf("%s\n", <-ch)
}
}
if name != "" {
path := fmt.Sprintf("/%s", name)
switch sub {
case "show":
o, _, err = req.Get("/" + name)
o, _, err = req.Get(path)
case "start":
o, _, err = req.Post("/"+name, nil)
o, _, err = req.Post(path, nil)
case "stop":
o, _, err = req.Delete("/" + name)
o, _, err = req.Delete(path)
case "restart":
o, _, err = req.Put("/"+name, nil)
o, _, err = req.Put(path, nil)
}
}
if err != nil {
fmt.Printf("Process error: %s", err)
}
return string(o)
return fmt.Sprintf("%s\n", o)
}
func RunDaemon() {

View File

@@ -75,7 +75,7 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s does not exist.", name)
return
}
cp, _ := p.find()
cp, _, _ := p.find()
if cp != nil {
fmt.Fprintf(w, "%s already running.", name)
return
@@ -92,7 +92,7 @@ func PutHandler(w http.ResponseWriter, r *http.Request) {
return
}
p.find()
ch := p.restart()
ch, _ := p.restart()
fmt.Fprintf(w, "%s", <-ch)
}

View File

@@ -63,26 +63,27 @@ func (p *Process) String() string {
}
//Find a process by name
func (p *Process) find() (*os.Process, error) {
func (p *Process) find() (*os.Process, string, error) {
if p.Pidfile == "" {
return nil, errors.New("Pidfile is empty.")
return nil, "", errors.New("Pidfile is empty.")
}
if pid := p.Pidfile.read(); pid > 0 {
process, err := os.FindProcess(pid)
if err != nil {
return nil, err
return nil, "", err
}
p.x = process
p.Pid = process.Pid
fmt.Printf("%s is %#v\n", p.Name, process.Pid)
p.Status = "running"
return process, nil
message := fmt.Sprintf("%s is %#v\n", p.Name, process.Pid)
return process, message, nil
}
return nil, errors.New(fmt.Sprintf("Could not find process %s.", p.Name))
message := fmt.Sprintf("%s not running.\n", p.Name)
return nil, message, errors.New(fmt.Sprintf("Could not find process %s.", p.Name))
}
//Start the process
func (p *Process) start(name string) {
func (p *Process) start(name string) string {
p.Name = name
wd, _ := os.Getwd()
proc := &os.ProcAttr{
@@ -97,22 +98,22 @@ func (p *Process) start(name string) {
args := append([]string{p.Name}, p.Args...)
process, err := os.StartProcess(p.Command, args, proc)
if err != nil {
log.Fatalf("%s failed. %s", p.Name, err)
return
log.Fatalf("%s failed. %s\n", p.Name, err)
return ""
}
err = p.Pidfile.write(process.Pid)
if err != nil {
log.Printf("%s pidfile error: %s", p.Name, err)
return
log.Printf("%s pidfile error: %s\n", p.Name, err)
return ""
}
p.x = process
p.Pid = process.Pid
fmt.Printf("%s is %#v\n", p.Name, process.Pid)
p.Status = "started"
return fmt.Sprintf("%s is %#v\n", p.Name, process.Pid)
}
//Stop the process
func (p *Process) stop() {
func (p *Process) stop() string {
if p.x != nil {
// p.x.Kill() this seems to cause trouble
cmd := exec.Command("kill", fmt.Sprintf("%d", p.x.Pid))
@@ -123,8 +124,8 @@ func (p *Process) stop() {
p.children.stop("all")
}
p.release("stopped")
fmt.Printf("%s stopped.\n", p.Name)
message := fmt.Sprintf("%s stopped.\n", p.Name)
return message
}
//Release process and remove pidfile
@@ -138,10 +139,11 @@ func (p *Process) release(status string) {
}
//Restart the process
func (p *Process) restart() chan *Process {
func (p *Process) restart() (chan *Process, string) {
p.stop()
fmt.Fprintf(os.Stderr, "%s restarted.\n", p.Name)
return RunProcess(p.Name, p)
message := fmt.Sprintf("%s restarted.\n", p.Name)
ch := RunProcess(p.Name, p)
return ch, message
}
//Run callback on the process after given duration.