gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/system/service.v2/options.go (about)

     1  package svc
     2  
     3  import (
     4  	"gitee.com/sy_183/go-common/lifecycle"
     5  	"os"
     6  	"syscall"
     7  )
     8  
     9  // An Option configures a Service.
    10  type Option interface {
    11  	apply(service Service)
    12  }
    13  
    14  // optionFunc wraps a func, so it satisfies the Option interface.
    15  type optionFunc func(service Service)
    16  
    17  func (f optionFunc) apply(service Service) {
    18  	f(service)
    19  }
    20  
    21  // ExitCodeGetter Option specifies the callback function for the Service
    22  // to obtain the program exit code
    23  func ExitCodeGetter(exitCodeGetter func(err *Error) int) Option {
    24  	type exitCodeGetterSetter interface {
    25  		setExitCodeGetter(exitCodeGetter func(err *Error) int)
    26  	}
    27  	return optionFunc(func(service Service) {
    28  		if setter, is := service.(exitCodeGetterSetter); is {
    29  			setter.setExitCodeGetter(exitCodeGetter)
    30  		}
    31  	})
    32  }
    33  
    34  func DefaultExitCodeGetter(err *Error) int {
    35  	if err == nil {
    36  		return 0
    37  	}
    38  	return 1
    39  }
    40  
    41  // SignalNotify Option specifies the callback function when capturing the
    42  // signal to be notified, if callback return true, If the callback function
    43  // returns true, the program will start to exit
    44  func SignalNotify(callback func(sig os.Signal) (exit bool), sig ...os.Signal) Option {
    45  	type signalNotifySetter interface {
    46  		setSignalNotify(callback func(sig os.Signal) (exit bool), sig ...os.Signal)
    47  	}
    48  	return optionFunc(func(service Service) {
    49  		if setter, is := service.(signalNotifySetter); is {
    50  			setter.setSignalNotify(callback, sig...)
    51  		}
    52  	})
    53  }
    54  
    55  var DefaultNotifySignals = [...]os.Signal{syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP}
    56  
    57  func DefaultSignalCallback(sig os.Signal) bool {
    58  	switch sig {
    59  	case syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP:
    60  		return true
    61  	}
    62  	return false
    63  }
    64  
    65  func OnStarted(callback func(s Service, l lifecycle.Lifecycle)) Option {
    66  	type onStartedSetter interface {
    67  		setOnStarted(callback func(s Service, l lifecycle.Lifecycle))
    68  	}
    69  	return optionFunc(func(service Service) {
    70  		if setter, is := service.(onStartedSetter); is {
    71  			setter.setOnStarted(callback)
    72  		}
    73  	})
    74  }
    75  
    76  func OnClosed(callback func(s Service, l lifecycle.Lifecycle)) Option {
    77  	type onClosedSetter interface {
    78  		setOnClosed(callback func(s Service, l lifecycle.Lifecycle))
    79  	}
    80  	return optionFunc(func(service Service) {
    81  		if setter, is := service.(onClosedSetter); is {
    82  			setter.setOnClosed(callback)
    83  		}
    84  	})
    85  }