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
// 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) {

View File

@ -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)

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
},
}

View File

@ -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
}
}
}