From 5eaa8f0778834cee066427a85fdaa39e6e061529 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 21 Jan 2021 12:27:21 +0800 Subject: [PATCH] add IsWrapperBinary flag and fix persistence error --- pkg/bbgo/builder.go | 9 +++++-- pkg/bbgo/redis_persistence.go | 14 +++++++---- pkg/bbgo/wrapper.go | 7 ++++++ pkg/cmd/build.go | 1 - pkg/cmd/run.go | 45 ++++++++++++++++++----------------- 5 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 pkg/bbgo/wrapper.go diff --git a/pkg/bbgo/builder.go b/pkg/bbgo/builder.go index 577162dff..dcee9dc39 100644 --- a/pkg/bbgo/builder.go +++ b/pkg/bbgo/builder.go @@ -18,6 +18,7 @@ import ( var wrapperTemplate = template.Must(template.New("main").Parse(`package main // DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES import ( + "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/cmd" {{- range .Imports }} @@ -25,6 +26,10 @@ import ( {{- end }} ) +func init() { + bbgo.SetWrapperBinary() +} + func main() { cmd.Execute() } @@ -111,10 +116,10 @@ func Build(ctx context.Context, userConfig *Config, targetConfig BuildTargetConf buildCmd.Stdout = os.Stdout buildCmd.Stderr = os.Stderr if err := buildCmd.Run(); err != nil { - return binary, err + return output, err } - return binary, nil + return output, nil } func BuildTarget(ctx context.Context, userConfig *Config, target BuildTargetConfig) (string, error) { diff --git a/pkg/bbgo/redis_persistence.go b/pkg/bbgo/redis_persistence.go index dbbf83922..53f80d4fd 100644 --- a/pkg/bbgo/redis_persistence.go +++ b/pkg/bbgo/redis_persistence.go @@ -11,8 +11,11 @@ import ( "strings" "github.com/go-redis/redis/v8" + "github.com/pkg/errors" ) +var ErrPersistenceNotExists = errors.New("persistent data does not exists") + type PersistenceService interface { NewStore(id string, subIDs ...string) Store } @@ -56,8 +59,9 @@ func (store *MemoryStore) Load(val interface{}) error { if data, ok := store.memory.Slots[store.Key]; ok { v.Elem().Set(reflect.ValueOf(data).Elem()) } else { - return os.ErrNotExist + return ErrPersistenceNotExists } + return nil } @@ -105,7 +109,7 @@ func (store JsonStore) Load(val interface{}) error { p := filepath.Join(store.Directory, store.ID) + ".json" if _, err := os.Stat(p); os.IsNotExist(err) { - return os.ErrNotExist + return ErrPersistenceNotExists } data, err := ioutil.ReadFile(p) @@ -114,7 +118,7 @@ func (store JsonStore) Load(val interface{}) error { } if len(data) == 0 { - return os.ErrNotExist + return ErrPersistenceNotExists } return json.Unmarshal(data, val) @@ -175,14 +179,14 @@ func (store *RedisStore) Load(val interface{}) error { data, err := cmd.Result() if err != nil { if err == redis.Nil { - return os.ErrNotExist + return ErrPersistenceNotExists } return err } if len(data) == 0 { - return os.ErrNotExist + return ErrPersistenceNotExists } return json.Unmarshal([]byte(data), val) diff --git a/pkg/bbgo/wrapper.go b/pkg/bbgo/wrapper.go new file mode 100644 index 000000000..aba9f19f0 --- /dev/null +++ b/pkg/bbgo/wrapper.go @@ -0,0 +1,7 @@ +package bbgo + +var IsWrapperBinary = false + +func SetWrapperBinary() { + IsWrapperBinary = true +} diff --git a/pkg/cmd/build.go b/pkg/cmd/build.go index b4f8faa47..6740e4791 100644 --- a/pkg/cmd/build.go +++ b/pkg/cmd/build.go @@ -58,4 +58,3 @@ var BuildCmd = &cobra.Command{ return nil }, } - diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index c84e67d7f..e53686435 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -255,42 +255,43 @@ func run(cmd *cobra.Command, args []string) error { return err } - shouldCompile := len(userConfig.Imports) > 0 - - // if there is no custom imports, we don't have to compile - if noCompile || !shouldCompile { + // for wrapper binary, we can just run the strategies + if bbgo.IsWrapperBinary || len(userConfig.Build.Imports) == 0 || noCompile { userConfig, err = bbgo.Load(configFile, true) if err != nil { return err } - log.Infof("running config without wrapper binary...") + if bbgo.IsWrapperBinary { + log.Infof("running wrapper binary...") + } + if err := runConfig(ctx, userConfig); err != nil { return err } return nil - } + } else { + var runArgs = []string{"run"} + cmd.Flags().Visit(func(flag *flag.Flag) { + runArgs = append(runArgs, "--"+flag.Name, flag.Value.String()) + }) + runArgs = append(runArgs, args...) - var runArgs = []string{"run", "--no-compile"} - 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 { + runCmd, err := buildAndRun(ctx, userConfig, runArgs...) + if err != nil { return err } - if err := runCmd.Wait(); 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 + } + + if err := runCmd.Wait(); err != nil { + return err + } } }