Пример #1
0
//  start a service with ctrl+c signal notify
func Start(s Service) {
	signalChan := make(chan os.Signal)
	sName := reflect.TypeOf(s).String()

	// notify exit signal
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	log.Debug("Start|%s", sName)

	// start
	s.Start()
	<-signalChan
	s.Stop()
	WrapWait() // wait global wait group

	log.Debug("Stop|%s", sName)
}
Пример #2
0
// wrap a function in global wait group
func (w *WaitGroup) Wrap(funName string, fn func()) {
	w.Add(1)
	go func() {
		t := time.Now()
		fn()
		w.Done()
		log.Debug("%s|%.1fms|%d", funName, time.Since(t).Seconds()*1000, runtime.NumGoroutine())
		// exit goroutine
		runtime.Goexit()
	}()
}
Пример #3
0
// call an action
func Call(fn Func, param interface{}) *Result {
	name := funcName(fn)
	log.Debug("Action|Call|%s", name)
	if len(a.before[name]) > 0 {
		for _, b := range a.before[name] {
			b(&param)
		}
	}
	result := fn(param)
	if len(a.after[name]) > 0 {
		for _, af := range a.after[name] {
			af(result, &param)
		}
	}
	if !result.Status {
		log.Warn("Action|Call|%s|%s", name, result.Error)
	}
	return result
}