mirror of
				https://github.com/gwoo/goforever.git
				synced 2025-10-31 18:33:20 +08:00 
			
		
		
		
	Update Cli to handle the main process
This commit is contained in:
		
							
								
								
									
										37
									
								
								goforever.go
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								goforever.go
									
									
									
									
									
								
							| @@ -50,11 +50,11 @@ func init() { | |||||||
| func main() { | func main() { | ||||||
| 	if *d == true { | 	if *d == true { | ||||||
| 		daemon.Args = append(daemon.Args, os.Args[2:]...) | 		daemon.Args = append(daemon.Args, os.Args[2:]...) | ||||||
| 		daemon.start(daemon.Name) | 		fmt.Printf(daemon.start(daemon.Name)) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if len(flag.Args()) > 0 { | 	if len(flag.Args()) > 0 { | ||||||
| 		fmt.Printf("%s\n", Cli()) | 		fmt.Printf("%s", Cli()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if len(flag.Args()) == 0 { | 	if len(flag.Args()) == 0 { | ||||||
| @@ -73,22 +73,45 @@ func Cli() string { | |||||||
| 	if sub == "list" { | 	if sub == "list" { | ||||||
| 		o, _, err = req.Get("/") | 		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 != "" { | 	if name != "" { | ||||||
|  | 		path := fmt.Sprintf("/%s", name) | ||||||
| 		switch sub { | 		switch sub { | ||||||
| 		case "show": | 		case "show": | ||||||
| 			o, _, err = req.Get("/" + name) | 			o, _, err = req.Get(path) | ||||||
| 		case "start": | 		case "start": | ||||||
| 			o, _, err = req.Post("/"+name, nil) | 			o, _, err = req.Post(path, nil) | ||||||
| 		case "stop": | 		case "stop": | ||||||
| 			o, _, err = req.Delete("/" + name) | 			o, _, err = req.Delete(path) | ||||||
| 		case "restart": | 		case "restart": | ||||||
| 			o, _, err = req.Put("/"+name, nil) | 			o, _, err = req.Put(path, nil) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fmt.Printf("Process error: %s", err) | 		fmt.Printf("Process error: %s", err) | ||||||
| 	} | 	} | ||||||
| 	return string(o) | 	return fmt.Sprintf("%s\n", o) | ||||||
| } | } | ||||||
|  |  | ||||||
| func RunDaemon() { | func RunDaemon() { | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								http.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								http.go
									
									
									
									
									
								
							| @@ -75,7 +75,7 @@ func PostHandler(w http.ResponseWriter, r *http.Request) { | |||||||
| 		fmt.Fprintf(w, "%s does not exist.", name) | 		fmt.Fprintf(w, "%s does not exist.", name) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	cp, _ := p.find() | 	cp, _, _ := p.find() | ||||||
| 	if cp != nil { | 	if cp != nil { | ||||||
| 		fmt.Fprintf(w, "%s already running.", name) | 		fmt.Fprintf(w, "%s already running.", name) | ||||||
| 		return | 		return | ||||||
| @@ -92,7 +92,7 @@ func PutHandler(w http.ResponseWriter, r *http.Request) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	p.find() | 	p.find() | ||||||
| 	ch := p.restart() | 	ch, _ := p.restart() | ||||||
| 	fmt.Fprintf(w, "%s", <-ch) | 	fmt.Fprintf(w, "%s", <-ch) | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										38
									
								
								process.go
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								process.go
									
									
									
									
									
								
							| @@ -63,26 +63,27 @@ func (p *Process) String() string { | |||||||
| } | } | ||||||
|  |  | ||||||
| //Find a process by name | //Find a process by name | ||||||
| func (p *Process) find() (*os.Process, error) { | func (p *Process) find() (*os.Process, string, error) { | ||||||
| 	if p.Pidfile == "" { | 	if p.Pidfile == "" { | ||||||
| 		return nil, errors.New("Pidfile is empty.") | 		return nil, "", errors.New("Pidfile is empty.") | ||||||
| 	} | 	} | ||||||
| 	if pid := p.Pidfile.read(); pid > 0 { | 	if pid := p.Pidfile.read(); pid > 0 { | ||||||
| 		process, err := os.FindProcess(pid) | 		process, err := os.FindProcess(pid) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, "", err | ||||||
| 		} | 		} | ||||||
| 		p.x = process | 		p.x = process | ||||||
| 		p.Pid = process.Pid | 		p.Pid = process.Pid | ||||||
| 		fmt.Printf("%s is %#v\n", p.Name, process.Pid) |  | ||||||
| 		p.Status = "running" | 		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 | //Start the process | ||||||
| func (p *Process) start(name string) { | func (p *Process) start(name string) string { | ||||||
| 	p.Name = name | 	p.Name = name | ||||||
| 	wd, _ := os.Getwd() | 	wd, _ := os.Getwd() | ||||||
| 	proc := &os.ProcAttr{ | 	proc := &os.ProcAttr{ | ||||||
| @@ -97,22 +98,22 @@ func (p *Process) start(name string) { | |||||||
| 	args := append([]string{p.Name}, p.Args...) | 	args := append([]string{p.Name}, p.Args...) | ||||||
| 	process, err := os.StartProcess(p.Command, args, proc) | 	process, err := os.StartProcess(p.Command, args, proc) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("%s failed. %s", p.Name, err) | 		log.Fatalf("%s failed. %s\n", p.Name, err) | ||||||
| 		return | 		return "" | ||||||
| 	} | 	} | ||||||
| 	err = p.Pidfile.write(process.Pid) | 	err = p.Pidfile.write(process.Pid) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Printf("%s pidfile error: %s", p.Name, err) | 		log.Printf("%s pidfile error: %s\n", p.Name, err) | ||||||
| 		return | 		return "" | ||||||
| 	} | 	} | ||||||
| 	p.x = process | 	p.x = process | ||||||
| 	p.Pid = process.Pid | 	p.Pid = process.Pid | ||||||
| 	fmt.Printf("%s is %#v\n", p.Name, process.Pid) |  | ||||||
| 	p.Status = "started" | 	p.Status = "started" | ||||||
|  | 	return fmt.Sprintf("%s is %#v\n", p.Name, process.Pid) | ||||||
| } | } | ||||||
|  |  | ||||||
| //Stop the process | //Stop the process | ||||||
| func (p *Process) stop() { | func (p *Process) stop() string { | ||||||
| 	if p.x != nil { | 	if p.x != nil { | ||||||
| 		// p.x.Kill() this seems to cause trouble | 		// p.x.Kill() this seems to cause trouble | ||||||
| 		cmd := exec.Command("kill", fmt.Sprintf("%d", p.x.Pid)) | 		cmd := exec.Command("kill", fmt.Sprintf("%d", p.x.Pid)) | ||||||
| @@ -123,8 +124,8 @@ func (p *Process) stop() { | |||||||
| 		p.children.stop("all") | 		p.children.stop("all") | ||||||
| 	} | 	} | ||||||
| 	p.release("stopped") | 	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 | //Release process and remove pidfile | ||||||
| @@ -138,10 +139,11 @@ func (p *Process) release(status string) { | |||||||
| } | } | ||||||
|  |  | ||||||
| //Restart the process | //Restart the process | ||||||
| func (p *Process) restart() chan *Process { | func (p *Process) restart() (chan *Process, string) { | ||||||
| 	p.stop() | 	p.stop() | ||||||
| 	fmt.Fprintf(os.Stderr, "%s restarted.\n", p.Name) | 	message := fmt.Sprintf("%s restarted.\n", p.Name) | ||||||
| 	return RunProcess(p.Name, p) | 	ch := RunProcess(p.Name, p) | ||||||
|  | 	return ch, message | ||||||
| } | } | ||||||
|  |  | ||||||
| //Run callback on the process after given duration. | //Run callback on the process after given duration. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 gwoo
					gwoo