gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/system/service/options.go (about) 1 package svc 2 3 import ( 4 "gitee.com/sy_183/go-common/lifecycle" 5 "gitee.com/sy_183/go-common/log" 6 "os" 7 "syscall" 8 ) 9 10 // An Option configures a Service. 11 type Option interface { 12 apply(service Service) 13 } 14 15 // optionFunc wraps a func, so it satisfies the Option interface. 16 type optionFunc func(service Service) 17 18 func (f optionFunc) apply(service Service) { 19 f(service) 20 } 21 22 // ErrorCallback Option specifies the callback function for the Service 23 // when an error occurs 24 func ErrorCallback(callback func(err *Error)) Option { 25 type errorCallbackSetter interface { 26 setErrorCallback(callback func(err *Error)) 27 } 28 return optionFunc(func(service Service) { 29 if setter, is := service.(errorCallbackSetter); is { 30 setter.setErrorCallback(callback) 31 } 32 }) 33 } 34 35 func DefaultErrorCallback(err *Error) {} 36 37 func LogErrorCallback(log *log.Logger) func(err *Error) { 38 return func(err *Error) { 39 switch err.Type { 40 case StartError: 41 log.ErrorWith("service start error", err.Err) 42 case ExitError: 43 log.ErrorWith("service exit error", err.Err) 44 case StopError: 45 log.ErrorWith("service stop error", err.Err) 46 case ServiceError: 47 log.ErrorWith("service internal error", err.Err) 48 } 49 } 50 } 51 52 // ExitCodeGetter Option specifies the callback function for the Service 53 // to obtain the program exit code 54 func ExitCodeGetter(exitCodeGetter func(err *Error) int) Option { 55 type exitCodeGetterSetter interface { 56 setExitCodeGetter(exitCodeGetter func(err *Error) int) 57 } 58 return optionFunc(func(service Service) { 59 if setter, is := service.(exitCodeGetterSetter); is { 60 setter.setExitCodeGetter(exitCodeGetter) 61 } 62 }) 63 } 64 65 func DefaultExitCodeGetter(err *Error) int { 66 if err == nil { 67 return 0 68 } 69 return 1 70 } 71 72 // SignalNotify Option specifies the callback function when capturing the 73 // signal to be notified, if callback return true, If the callback function 74 // returns true, the program will start to exit 75 func SignalNotify(callback func(sig os.Signal) (exit bool), sig ...os.Signal) Option { 76 type signalNotifySetter interface { 77 setSignalNotify(callback func(sig os.Signal) (exit bool), sig ...os.Signal) 78 } 79 return optionFunc(func(service Service) { 80 if setter, is := service.(signalNotifySetter); is { 81 setter.setSignalNotify(callback, sig...) 82 } 83 }) 84 } 85 86 var DefaultNotifySignals = [...]os.Signal{syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP} 87 88 func DefaultSignalCallback(sig os.Signal) bool { 89 switch sig { 90 case syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP: 91 return true 92 } 93 return false 94 } 95 96 func OnStarted(callback func(s Service, l lifecycle.Lifecycle)) Option { 97 type onStartedSetter interface { 98 setOnStarted(callback func(s Service, l lifecycle.Lifecycle)) 99 } 100 return optionFunc(func(service Service) { 101 if setter, is := service.(onStartedSetter); is { 102 setter.setOnStarted(callback) 103 } 104 }) 105 } 106 107 func OnClosed(callback func(s Service, l lifecycle.Lifecycle)) Option { 108 type onClosedSetter interface { 109 setOnClosed(callback func(s Service, l lifecycle.Lifecycle)) 110 } 111 return optionFunc(func(service Service) { 112 if setter, is := service.(onClosedSetter); is { 113 setter.setOnClosed(callback) 114 } 115 }) 116 }