mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add BeforeRestart hook
This commit is contained in:
parent
ba9c8693af
commit
0803d6bae0
|
@ -78,6 +78,11 @@ func main() {
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Cancel: cancel,
|
Cancel: cancel,
|
||||||
Token: "",
|
Token: "",
|
||||||
|
BeforeRestart: func() {
|
||||||
|
if err := ui.Close() ; err != nil {
|
||||||
|
log.WithError(err).Errorf("ui close error")
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
userConfig = &bbgo.Config{
|
userConfig = &bbgo.Config{
|
||||||
Notifications: nil,
|
Notifications: nil,
|
||||||
|
|
|
@ -95,9 +95,8 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
|
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
|
||||||
if dsn, ok := os.LookupEnv("MYSQL_URL") ; ok {
|
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
|
||||||
if err := environ.ConfigureDatabase(ctx, dsn); err != nil {
|
if err := environ.ConfigureDatabase(ctx, dsn); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -267,12 +266,12 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableApiServer
|
||||||
defer cancelTrading()
|
defer cancelTrading()
|
||||||
|
|
||||||
environ := bbgo.NewEnvironment()
|
environ := bbgo.NewEnvironment()
|
||||||
if err := BootstrapEnvironment(ctx, environ, userConfig) ; err != nil {
|
if err := BootstrapEnvironment(ctx, environ, userConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
trader := bbgo.NewTrader(environ)
|
trader := bbgo.NewTrader(environ)
|
||||||
if err := ConfigureTrader(trader, userConfig) ; err != nil {
|
if err := ConfigureTrader(trader, userConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,7 +319,7 @@ func run(cmd *cobra.Command, args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(dotenvFile) ; err == nil {
|
if _, err := os.Stat(dotenvFile); err == nil {
|
||||||
if err := godotenv.Load(dotenvFile); err != nil {
|
if err := godotenv.Load(dotenvFile); err != nil {
|
||||||
return errors.Wrap(err, "error loading dotenv file")
|
return errors.Wrap(err, "error loading dotenv file")
|
||||||
}
|
}
|
||||||
|
@ -347,22 +346,21 @@ func run(cmd *cobra.Command, args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var userConfig *bbgo.Config
|
var userConfig = &bbgo.Config{}
|
||||||
|
|
||||||
if setup {
|
if setup {
|
||||||
log.Infof("running in setup mode, skip reading config file")
|
log.Infof("running in setup mode, skip reading config file")
|
||||||
enableApiServer = true
|
enableApiServer = true
|
||||||
userConfig = &bbgo.Config{
|
|
||||||
Notifications: nil,
|
|
||||||
Persistence: nil,
|
|
||||||
Sessions: nil,
|
|
||||||
ExchangeStrategies: nil,
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
// if it's not setup, then the config file option is required.
|
||||||
if len(configFile) == 0 {
|
if len(configFile) == 0 {
|
||||||
return errors.New("--config option is required")
|
return errors.New("--config option is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(configFile); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
userConfig, err = bbgo.Load(configFile, false)
|
userConfig, err = bbgo.Load(configFile, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -32,6 +32,8 @@ type Setup struct {
|
||||||
|
|
||||||
// Token is used for setup api authentication
|
// Token is used for setup api authentication
|
||||||
Token string
|
Token string
|
||||||
|
|
||||||
|
BeforeRestart func()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
|
|
@ -119,6 +119,10 @@ func (s *Server) setupRestart(c *gin.Context) {
|
||||||
|
|
||||||
logrus.Info("server shutdown completed")
|
logrus.Info("server shutdown completed")
|
||||||
|
|
||||||
|
if s.Setup.BeforeRestart != nil {
|
||||||
|
s.Setup.BeforeRestart()
|
||||||
|
}
|
||||||
|
|
||||||
bin := os.Args[0]
|
bin := os.Args[0]
|
||||||
args := os.Args[0:]
|
args := os.Args[0:]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user