Esempio n. 1
0
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()
}
Esempio n. 2
0
File: main.go Progetto: alemic/gleam
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!")
}
Esempio n. 3
0
func initLog() {
	level := log.LogNone
	levels := strings.SplitN(*loglevel, "|", -1)
	for _, v := range levels {
		switch v {
		case "none":
			level = level | log.LogNone
			break
		case "error":
			level = level | log.LogError
		case "warning":
			level = level | log.LogWarning
		case "message":
			level = level | log.LogMessage
		case "debug":
			level = level | log.LogDebug
		case "all":
			level = log.LogAll
		default:
		}
	}
	if err := log.Init(*logfile, level); err != nil {
		log.Error(err)
	}
}
Esempio n. 4
0
func init() {
	if !flag.Parsed() {
		flag.Parse()
	}
	node.ErrHandler = func(err error) {
		log.Error(err)
	}
}
Esempio n. 5
0
func main() {
	conn, err := doozer.DialUri(*uri, *buri)
	if err != nil {
		log.Error(err)
		return
	}
	defer conn.Close()
	rev, err := conn.Rev()
	if err != nil {
		log.Error(err)
		return
	}
	if (*region)[0] != '/' {
		*region = "/" + *region
	}
	var path string
	if *pid == 0 {
		path = fmt.Sprintf("%s/wire", *region)
	} else {
		path = fmt.Sprintf("%s/node/%d", *region, *pid)
	}
	params := make([]interface{}, flag.NArg())
	for i := 0; i < flag.NArg(); i++ {
		params[i] = interface{}(flag.Arg(i))
	}
	f := &node.ZFunc{Name: *fn, Params: params}
	body, err := json.Marshal(f)
	if err != nil {
		log.Error(err)
		return
	}
	rev, err = conn.Set(path, rev, body)
	if err != nil {
		log.Error(err)
		return
	}
	log.Messagef("Rev: %d", rev)
	time.Sleep(time.Second)
}
Esempio n. 6
0
func Handle(w http.ResponseWriter, r *http.Request, h HandlerFunc) {
	defer r.Body.Close()
	if r.Method == "POST" || r.Method == "post" {
		body, err := ioutil.ReadAll(r.Body)
		if err != nil {
			log.Error(err)
			w.WriteHeader(500)
			return
		}
		var wreq *Request
		if wreq, err = DecodeRequest(body); err != nil {
			log.Error(err)
			w.WriteHeader(500)
			return
		}
		wresp, err := h(wreq)
		if err != nil {
			log.Error(err)
			w.WriteHeader(500)
			return
		}
		data, err := wresp.Encode()
		if _, err := w.Write(data); err != nil {
			log.Error(err)
			w.WriteHeader(500)
		}
		return
	} else {
		if Signature(Token, r.FormValue("timestamp"),
			r.FormValue("nonce")) == r.FormValue("signature") {
			w.Write([]byte(r.FormValue("echostr")))
		} else {
			w.WriteHeader(403)
		}
	}
}
Esempio n. 7
0
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()
}
Esempio n. 8
0
func main() {
	log.Message("Starting ... ")
	defer func() {
		time.Sleep(time.Second)
		log.Message("Shutdown complate!")
	}()

	// init profiling file
	if *proffile != "" {
		log.Debugf("Open a profiling file: %s", *proffile)
		if err := prof.Start(*proffile); err != nil {
			log.Error(err)
		} else {
			defer prof.Stop()
		}
	}

	// init heap dumping file
	if *dumpfile != "" {
		log.Debugf("Open a heap dumping file: %s", *dumpfile)
		if err := prof.NewDump(*dumpfile); err != nil {
			log.Error(err)
		} else {
			defer prof.CloseDump()
			go func() {
				for prof.Dumping {
					time.Sleep(time.Duration(*dumptime) * time.Second)
					prof.Dump()
				}
			}()
		}
	}

	// init pid file
	log.Debugf("Open a pid file: %s", *pidfile)
	if pidFile, err := pid.New(*pidfile); err != nil {
		log.Error(err)
	} else {
		defer pidFile.Close()
	}

	w := worker.New(*joblimit)
	if err := w.AddServer(*gearmand); err != nil {
		log.Error(err)
		return
	}
	if err := w.AddFunc("exec", execShell, uint32(*timeout)); err != nil {
		log.Error(err)
		return
	}
	if err := w.AddFunc("execphp", execPHP, uint32(*timeout)); err != nil {
		log.Error(err)
		return
	}
	defer w.Close()
	go w.Work()

	// signal handler
	sh := signal.NewHandler()
	sh.Bind(os.Interrupt, func() bool { return true })
	sh.Loop()
}