bbgo_origin/pkg/cmd/run.go

329 lines
8.0 KiB
Go
Raw Permalink Normal View History

package cmd
import (
"context"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
2021-05-18 07:38:22 +00:00
"runtime/pprof"
"syscall"
2020-11-12 06:50:08 +00:00
"time"
"github.com/pkg/errors"
2020-10-24 07:43:55 +00:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/c9s/bbgo/pkg/bbgo"
2020-10-21 07:58:58 +00:00
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
2021-02-02 18:26:41 +00:00
"github.com/c9s/bbgo/pkg/server"
)
func init() {
RunCmd.Flags().Bool("no-compile", false, "do not compile wrapper binary")
RunCmd.Flags().String("totp-key-url", "", "time-based one-time password key URL, if defined, it will be used for restoring the otp key")
RunCmd.Flags().String("totp-issuer", "", "")
RunCmd.Flags().String("totp-account-name", "", "")
RunCmd.Flags().Bool("enable-webserver", false, "enable webserver")
RunCmd.Flags().Bool("enable-web-server", false, "legacy option, this is renamed to --enable-webserver")
2021-05-18 07:38:22 +00:00
RunCmd.Flags().String("cpu-profile", "", "cpu profile")
RunCmd.Flags().String("webserver-bind", ":8080", "webserver binding")
2021-02-01 09:20:01 +00:00
RunCmd.Flags().Bool("setup", false, "use setup mode")
2020-10-23 06:28:07 +00:00
RootCmd.AddCommand(RunCmd)
}
2020-12-31 09:07:12 +00:00
var RunCmd = &cobra.Command{
Use: "run",
Short: "run strategies from config file",
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
RunE: run,
}
2021-02-03 10:22:16 +00:00
func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer bool) error {
ctx, cancelTrading := context.WithCancel(baseCtx)
defer cancelTrading()
environ := bbgo.NewEnvironment()
trader := bbgo.NewTrader(environ)
if enableApiServer {
go func() {
2021-02-03 10:09:33 +00:00
s := &server.Server{
2021-02-05 05:01:07 +00:00
Config: userConfig,
Environ: environ,
Trader: trader,
2021-02-04 05:48:21 +00:00
OpenInBrowser: true,
Setup: &server.Setup{
Context: ctx,
Cancel: cancelTrading,
Token: "",
},
2021-02-03 10:09:33 +00:00
}
if err := s.Run(ctx); err != nil {
log.WithError(err).Errorf("server error")
}
}()
}
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
cancelTrading()
2021-05-22 09:44:20 +00:00
// graceful period = 15 second
shutdownCtx, cancelShutdown := context.WithDeadline(ctx, time.Now().Add(15*time.Second))
log.Infof("shutting down...")
trader.Graceful.Shutdown(shutdownCtx)
cancelShutdown()
return nil
}
func BootstrapBacktestEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if err := environ.ConfigureDatabase(ctx); err != nil {
return err
}
environ.Notifiability = bbgo.Notifiability{
SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil),
SessionChannelRouter: bbgo.NewPatternChannelRouter(nil),
ObjectChannelRouter: bbgo.NewObjectChannelRouter(),
}
return nil
}
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
2021-02-21 08:52:47 +00:00
if err := environ.ConfigureDatabase(ctx); err != nil {
return err
}
2021-02-21 08:52:47 +00:00
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
2021-02-21 09:48:03 +00:00
return errors.Wrap(err, "exchange session configure error")
}
if userConfig.Persistence != nil {
if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil {
2021-02-21 09:48:03 +00:00
return errors.Wrap(err, "persistence configure error")
}
}
2021-03-16 10:39:18 +00:00
if err := environ.ConfigureNotificationSystem(userConfig); err != nil {
return errors.Wrap(err, "notification configure error")
2020-10-27 01:38:29 +00:00
}
2021-02-04 10:22:47 +00:00
return nil
}
2020-10-30 21:21:17 +00:00
func runConfig(basectx context.Context, userConfig *bbgo.Config, enableWebServer bool, webServerBind string) error {
2021-02-04 10:22:47 +00:00
ctx, cancelTrading := context.WithCancel(basectx)
defer cancelTrading()
environ := bbgo.NewEnvironment()
2021-02-05 05:01:07 +00:00
if err := BootstrapEnvironment(ctx, environ, userConfig); err != nil {
2021-02-04 10:22:47 +00:00
return err
}
2021-05-18 07:38:22 +00:00
if err := environ.Init(ctx); err != nil {
2021-05-07 16:44:43 +00:00
return err
}
2021-03-16 10:39:18 +00:00
if err := environ.Sync(ctx); err != nil {
return err
}
2021-02-04 10:22:47 +00:00
trader := bbgo.NewTrader(environ)
if err := trader.Configure(userConfig); err != nil {
2021-02-04 10:22:47 +00:00
return err
}
2020-11-12 06:50:08 +00:00
if err := trader.Run(ctx); err != nil {
return err
}
if enableWebServer {
2021-01-24 11:07:56 +00:00
go func() {
2021-02-03 10:09:33 +00:00
s := &server.Server{
Config: userConfig,
2021-02-03 10:09:33 +00:00
Environ: environ,
Trader: trader,
2021-02-03 10:09:33 +00:00
}
if err := s.Run(ctx, webServerBind); err != nil {
2021-01-24 11:07:56 +00:00
log.WithError(err).Errorf("server error")
}
}()
}
2020-11-12 06:50:08 +00:00
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
cancelTrading()
2020-11-12 06:50:08 +00:00
log.Infof("shutting down...")
shutdownCtx, cancelShutdown := context.WithDeadline(ctx, time.Now().Add(30*time.Second))
2020-11-12 06:50:08 +00:00
trader.Graceful.Shutdown(shutdownCtx)
cancelShutdown()
for _, session := range environ.Sessions() {
if err := session.MarketDataStream.Close(); err != nil {
log.WithError(err).Errorf("[%s] market data stream close error", session.Name)
}
if err := session.UserDataStream.Close(); err != nil {
log.WithError(err).Errorf("[%s] user data stream close error", session.Name)
}
}
2021-05-09 18:17:19 +00:00
2020-11-12 06:50:08 +00:00
return nil
2020-10-23 06:49:54 +00:00
}
2020-12-31 09:07:12 +00:00
func run(cmd *cobra.Command, args []string) error {
setup, err := cmd.Flags().GetBool("setup")
2020-12-31 09:07:12 +00:00
if err != nil {
return err
}
enableWebServer, err := cmd.Flags().GetBool("enable-webserver")
if err != nil {
return err
}
webServerBind, err := cmd.Flags().GetString("webserver-bind")
2020-12-31 09:07:12 +00:00
if err != nil {
return err
}
enableWebServerLegacy, err := cmd.Flags().GetBool("enable-web-server")
if err != nil {
return err
}
if enableWebServerLegacy {
log.Warn("command option --enable-web-server is renamed to --enable-webserver")
enableWebServer = true
}
noCompile, err := cmd.Flags().GetBool("no-compile")
2020-12-31 09:07:12 +00:00
if err != nil {
return err
}
configFile, err := cmd.Flags().GetString("config")
2021-01-24 11:07:56 +00:00
if err != nil {
return err
}
2021-05-18 07:38:22 +00:00
cpuProfile, err := cmd.Flags().GetString("cpu-profile")
if err != nil {
return err
}
2021-02-05 05:01:07 +00:00
var userConfig = &bbgo.Config{}
if !setup {
2021-02-05 05:01:07 +00:00
// if it's not setup, then the config file option is required.
if len(configFile) == 0 {
return errors.New("--config option is required")
}
2021-02-05 05:01:07 +00:00
if _, err := os.Stat(configFile); err != nil {
return err
}
userConfig, err = bbgo.Load(configFile, false)
if err != nil {
return err
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// for wrapper binary, we can just run the strategies
if bbgo.IsWrapperBinary || (userConfig.Build != nil && len(userConfig.Build.Imports) == 0) || noCompile {
if bbgo.IsWrapperBinary {
log.Infof("running wrapper binary...")
}
if setup {
return runSetup(ctx, userConfig, true)
}
userConfig, err = bbgo.Load(configFile, true)
if err != nil {
return err
}
2021-05-18 07:38:22 +00:00
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
return runConfig(ctx, userConfig, enableWebServer, webServerBind)
2021-02-01 09:31:54 +00:00
}
2020-12-09 08:13:20 +00:00
2021-02-01 09:31:54 +00:00
return runWrapperBinary(ctx, userConfig, cmd, args)
}
2021-02-01 09:31:54 +00:00
func runWrapperBinary(ctx context.Context, userConfig *bbgo.Config, cmd *cobra.Command, args []string) error {
var runArgs = []string{"run"}
cmd.Flags().Visit(func(flag *flag.Flag) {
runArgs = append(runArgs, "--"+flag.Name, flag.Value.String())
})
runArgs = append(runArgs, args...)
runCmd, err := buildAndRun(ctx, userConfig, runArgs...)
if err != nil {
return err
}
if sig := cmdutil.WaitForSignal(ctx, syscall.SIGTERM, syscall.SIGINT); sig != nil {
log.Infof("sending signal to the child process...")
if err := runCmd.Process.Signal(sig); err != nil {
return err
}
2021-02-01 09:31:54 +00:00
if err := runCmd.Wait(); err != nil {
return err
2020-11-23 08:36:03 +00:00
}
2020-12-31 09:07:12 +00:00
}
2020-11-23 08:36:03 +00:00
2020-12-31 09:07:12 +00:00
return nil
}
2021-01-21 04:06:03 +00:00
// buildAndRun builds the package natively and run the binary with the given args
func buildAndRun(ctx context.Context, userConfig *bbgo.Config, args ...string) (*exec.Cmd, error) {
packageDir, err := ioutil.TempDir("build", "bbgow")
if err != nil {
2021-01-21 04:06:03 +00:00
return nil, err
2020-10-26 05:27:07 +00:00
}
2021-01-21 04:06:03 +00:00
defer os.RemoveAll(packageDir)
2020-10-26 05:27:07 +00:00
2021-01-21 04:06:03 +00:00
targetConfig := bbgo.GetNativeBuildTargetConfig()
binary, err := bbgo.Build(ctx, userConfig, targetConfig)
2020-10-26 05:27:07 +00:00
if err != nil {
2020-11-23 08:36:03 +00:00
return nil, err
2020-10-26 05:27:07 +00:00
}
cwd, err := os.Getwd()
if err != nil {
2020-11-23 08:36:03 +00:00
return nil, err
}
executePath := filepath.Join(cwd, binary)
2020-11-23 08:36:03 +00:00
runCmd := exec.Command(executePath, args...)
runCmd.Stdout = os.Stdout
runCmd.Stderr = os.Stderr
2020-11-23 08:36:03 +00:00
return runCmd, runCmd.Start()
}