Example #1
0
func main() {
	log.Println("Starting ...")
	defer log.Println("Shutdown complete!")
	w := worker.New(worker.Unlimited)
	defer w.Close()
	w.ErrHandler = func(e error) {
		log.Println(e)
		if e == worker.ErrConnection {
			proc, err := os.FindProcess(os.Getpid())
			if err != nil {
				log.Println(err)
			}
			if err := proc.Signal(os.Interrupt); err != nil {
				log.Println(err)
			}
		}
	}
	w.JobHandler = func(job *worker.Job) error {
		log.Printf("H=%s, UID=%s, Data=%s, DataType=%d\n", job.Handle,
			job.UniqueId, job.Data, job.DataType)
		return nil
	}
	w.AddServer("127.0.0.1:4730")
	w.AddFunc("ToUpper", ToUpper, worker.Immediately)
	w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
	w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
	w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately)
	w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately)
	go w.Work()
	sh := signal.NewHandler()
	sh.Bind(os.Interrupt, func() bool { return true })
	sh.Loop()
}
Example #2
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()
}
Example #3
0
File: main.go Project: 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!")
}
Example #4
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()
}
Example #5
0
func main() {
	log.Println("Starting ...")
	defer log.Println("Shutdown complete!")
	w := worker.New(worker.Unlimited)
	defer w.Close()
	w.ErrorHandler = func(e error) {
		log.Println(e)
		if opErr, ok := e.(*net.OpError); ok {
			if !opErr.Temporary() {
				proc, err := os.FindProcess(os.Getpid())
				if err != nil {
					log.Println(err)
				}
				if err := proc.Signal(os.Interrupt); err != nil {
					log.Println(err)
				}
			}
		}
	}
	w.JobHandler = func(job worker.Job) error {
		log.Printf("Data=%s\n", job.Data())
		return nil
	}
	w.AddServer("tcp4", "127.0.0.1:4730")
	w.AddFunc("Foobar", Foobar, worker.Unlimited)
	w.AddFunc("ToUpper", ToUpper, worker.Unlimited)
	w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
	w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
	w.AddFunc("SysInfo", worker.SysInfo, worker.Unlimited)
	w.AddFunc("MemInfo", worker.MemInfo, worker.Unlimited)
	if err := w.Ready(); err != nil {
		log.Fatal(err)
		return
	}
	go w.Work()
	sh := signal.NewHandler()
	sh.Bind(os.Interrupt, func() bool { return true })
	sh.Loop()
}
Example #6
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()
}