add IsWrapperBinary flag and fix persistence error

This commit is contained in:
c9s 2021-01-21 12:27:21 +08:00
parent 503df57e72
commit 5eaa8f0778
5 changed files with 46 additions and 30 deletions

View File

@ -18,6 +18,7 @@ import (
var wrapperTemplate = template.Must(template.New("main").Parse(`package main var wrapperTemplate = template.Must(template.New("main").Parse(`package main
// DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES // DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES
import ( import (
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/cmd" "github.com/c9s/bbgo/pkg/cmd"
{{- range .Imports }} {{- range .Imports }}
@ -25,6 +26,10 @@ import (
{{- end }} {{- end }}
) )
func init() {
bbgo.SetWrapperBinary()
}
func main() { func main() {
cmd.Execute() cmd.Execute()
} }
@ -111,10 +116,10 @@ func Build(ctx context.Context, userConfig *Config, targetConfig BuildTargetConf
buildCmd.Stdout = os.Stdout buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil { 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) { func BuildTarget(ctx context.Context, userConfig *Config, target BuildTargetConfig) (string, error) {

View File

@ -11,8 +11,11 @@ import (
"strings" "strings"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"github.com/pkg/errors"
) )
var ErrPersistenceNotExists = errors.New("persistent data does not exists")
type PersistenceService interface { type PersistenceService interface {
NewStore(id string, subIDs ...string) Store 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 { if data, ok := store.memory.Slots[store.Key]; ok {
v.Elem().Set(reflect.ValueOf(data).Elem()) v.Elem().Set(reflect.ValueOf(data).Elem())
} else { } else {
return os.ErrNotExist return ErrPersistenceNotExists
} }
return nil return nil
} }
@ -105,7 +109,7 @@ func (store JsonStore) Load(val interface{}) error {
p := filepath.Join(store.Directory, store.ID) + ".json" p := filepath.Join(store.Directory, store.ID) + ".json"
if _, err := os.Stat(p); os.IsNotExist(err) { if _, err := os.Stat(p); os.IsNotExist(err) {
return os.ErrNotExist return ErrPersistenceNotExists
} }
data, err := ioutil.ReadFile(p) data, err := ioutil.ReadFile(p)
@ -114,7 +118,7 @@ func (store JsonStore) Load(val interface{}) error {
} }
if len(data) == 0 { if len(data) == 0 {
return os.ErrNotExist return ErrPersistenceNotExists
} }
return json.Unmarshal(data, val) return json.Unmarshal(data, val)
@ -175,14 +179,14 @@ func (store *RedisStore) Load(val interface{}) error {
data, err := cmd.Result() data, err := cmd.Result()
if err != nil { if err != nil {
if err == redis.Nil { if err == redis.Nil {
return os.ErrNotExist return ErrPersistenceNotExists
} }
return err return err
} }
if len(data) == 0 { if len(data) == 0 {
return os.ErrNotExist return ErrPersistenceNotExists
} }
return json.Unmarshal([]byte(data), val) return json.Unmarshal([]byte(data), val)

7
pkg/bbgo/wrapper.go Normal file
View File

@ -0,0 +1,7 @@
package bbgo
var IsWrapperBinary = false
func SetWrapperBinary() {
IsWrapperBinary = true
}

View File

@ -58,4 +58,3 @@ var BuildCmd = &cobra.Command{
return nil return nil
}, },
} }

View File

@ -255,24 +255,24 @@ func run(cmd *cobra.Command, args []string) error {
return err return err
} }
shouldCompile := len(userConfig.Imports) > 0 // for wrapper binary, we can just run the strategies
if bbgo.IsWrapperBinary || len(userConfig.Build.Imports) == 0 || noCompile {
// if there is no custom imports, we don't have to compile
if noCompile || !shouldCompile {
userConfig, err = bbgo.Load(configFile, true) userConfig, err = bbgo.Load(configFile, true)
if err != nil { if err != nil {
return err return err
} }
log.Infof("running config without wrapper binary...") if bbgo.IsWrapperBinary {
log.Infof("running wrapper binary...")
}
if err := runConfig(ctx, userConfig); err != nil { if err := runConfig(ctx, userConfig); err != nil {
return err return err
} }
return nil return nil
} } else {
var runArgs = []string{"run"}
var runArgs = []string{"run", "--no-compile"}
cmd.Flags().Visit(func(flag *flag.Flag) { cmd.Flags().Visit(func(flag *flag.Flag) {
runArgs = append(runArgs, "--"+flag.Name, flag.Value.String()) runArgs = append(runArgs, "--"+flag.Name, flag.Value.String())
}) })
@ -293,6 +293,7 @@ func run(cmd *cobra.Command, args []string) error {
return err return err
} }
} }
}
return nil return nil
} }