// 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) }
// 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() }() }
// 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(¶m) } } result := fn(param) if len(a.after[name]) > 0 { for _, af := range a.after[name] { af(result, ¶m) } } if !result.Status { log.Warn("Action|Call|%s|%s", name, result.Error) } return result }