func main() { defer func() { log.Message("Exit.") time.Sleep(time.Second) }() log.Message("Starting...") hostname, err := os.Hostname() if err != nil { log.Error(err) return } n := node.New(*region, hostname) n.ErrHandler = node.ErrHandler n.Bind("Stop", node.Stop) n.Bind("Restart", node.Restart) // n.Bind("Shell", ExecShell) // n.Bind("PHP", ExecPHP) if err := n.Start(*uri, *buri); err != nil { log.Error(err) return } defer n.Close() go func() { n.Wait() signal.Send(os.Getpid(), os.Interrupt) }() // signal handler sh := signal.NewHandler() sh.Bind(os.Interrupt, func() bool { return true }) sh.Loop() }
func main() { // prepare the configuration config, err := InitConfig() if err != nil { log.Error(err) return } // make PID file if config.Pid != "" { p, err := pid.New(config.Pid) if err != nil { log.Error(err) return } defer p.Close() } log.Message("Starting...") if config.Ca != "" || config.Cert != "" || config.Key != "" { log.Messagef("Setting TLS (CA=%s; Cert=%s; Key=%s)...", config.Ca, config.Cert, config.Key) } g, err := gleam.New(config.Etcd, config.Script, config.Cert, config.Key, config.Ca) if err != nil { log.Error(err) return } defer g.Close() g.ErrHandler = func(err error) { log.Error(err) } log.Messagef("Watching(Name = %s)...", config.Name) g.WatchNode(config.Name) for _, r := range config.Region { log.Messagef("Watching(Region = %s)...", r) g.WatchRegion(r) } go g.Serve() // signal handler sh := signal.NewHandler() sh.Bind(os.Interrupt, func() bool { return true }) go func() { g.Wait() if err := signal.Send(os.Getpid(), os.Interrupt); err != nil { panic(err) } }() sh.Loop() log.Message("Exit!") }
func main() { defer func() { log.Message("Exit.") time.Sleep(time.Microsecond * 100) }() log.Message("Starting...") http.HandleFunc("/wechat", echo) go func() { log.Messagef("Bind to [%s]", *addr) if err := http.ListenAndServe(*addr, nil); err != nil { log.Error(err) signal.Send(os.Getpid(), os.Interrupt) } }() // signal handler sh := signal.NewHandler() sh.Bind(os.Interrupt, func() bool { return true }) sh.Loop() }
func main() { signal.Bind(syscall.SIGUSR1, func() uint { fmt.Println("SIGUSR1 handler #1") return signal.Continue }) signal.Bind(syscall.SIGUSR1, func() uint { fmt.Println("SIGUSR1 handler #2") return signal.Break }) // Because the previous handler returns `Break` permanently, // this handler will never be excuted. signal.Bind(syscall.SIGUSR1, func() uint { fmt.Println("SIGUSR1 handler #3") return signal.Continue }) // Bind and Unbind handler := signal.Bind(syscall.SIGUSR1, func() uint { fmt.Println("SIGUSR1 handler #4") return signal.Continue }) handler.Unbind() // Another alternative way is: // signal.Unbind(syscall.SIGUSR1, handler.Id) signal.Bind(syscall.SIGINT, func() uint { return signal.BreakExit }) // Stop automatically after 2 minutes go func() { time.Sleep(time.Second * 120) if err := signal.Send(os.Getpid(), os.Interrupt); err != nil { fmt.Println(err) } }() // Block here s := signal.Wait() fmt.Printf("Exit by signal: %s\n", s) }
func Stop() { if err := signal.Send(os.Getpid(), os.Interrupt); err != nil { _err(err) } }