gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/lifecycle/runner.go (about) 1 package lifecycle 2 3 type Runner interface { 4 DoStart(Lifecycle) error 5 6 DoRun(Lifecycle) error 7 8 DoClose(Lifecycle) error 9 } 10 11 type ( 12 StartFunc = func(Lifecycle) error 13 RunFunc = func(Lifecycle) error 14 CloseFunc = func(Lifecycle) error 15 ) 16 17 type runnerFunc struct { 18 startFn StartFunc 19 runFn RunFunc 20 closeFn CloseFunc 21 } 22 23 func FuncRunner(startFn, runFn, closeFn func(Lifecycle) error) Runner { 24 if startFn == nil { 25 startFn = func(Lifecycle) error { return nil } 26 } 27 if runFn == nil { 28 runFn = func(Lifecycle) error { return nil } 29 } 30 if closeFn == nil { 31 closeFn = func(Lifecycle) error { return nil } 32 } 33 return runnerFunc{startFn: startFn, runFn: runFn, closeFn: closeFn} 34 } 35 36 func (f runnerFunc) DoStart(lifecycle Lifecycle) error { 37 return f.startFn(lifecycle) 38 } 39 40 func (f runnerFunc) DoRun(lifecycle Lifecycle) error { 41 return f.runFn(lifecycle) 42 } 43 44 func (f runnerFunc) DoClose(lifecycle Lifecycle) error { 45 return f.closeFn(lifecycle) 46 }