Ejemplo n.º 1
0
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)
}
Ejemplo n.º 2
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()
	signal.Bind(os.Interrupt, func() uint { return signal.BreakExit })
	signal.Wait()
}