improve wrapper binary invocation

This commit is contained in:
c9s 2020-11-15 13:23:26 +08:00
parent 045b9f5d00
commit 94aaaf21b0
4 changed files with 35 additions and 9 deletions

View File

@ -129,6 +129,21 @@ func loadStash(config []byte) (Stash, error) {
return stash, nil
}
func Preload(configFile string) (*Config, error) {
var config Config
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(content, &config); err != nil {
return nil, err
}
return &config, nil
}
func Load(configFile string) (*Config, error) {
var config Config

View File

@ -25,6 +25,9 @@ func RegisterStrategy(key string, s interface{}) {
case CrossExchangeStrategy:
LoadedCrossExchangeStrategies[key] = d
default:
panic(fmt.Errorf("%T does not implement SingleExchangeStrategy or CrossExchangeStrategy", d))
}
}
@ -127,7 +130,6 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
balances.Print()
session.Account.UpdateBalances(balances)
session.Account.BindStream(session.Stream)
@ -135,11 +137,10 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
session.Stream.OnKLineClosed(func(kline types.KLine) {
log.Infof("kline closed: %+v", kline)
if _, ok := session.startPrices[kline.Symbol] ; !ok {
if _, ok := session.startPrices[kline.Symbol]; !ok {
session.startPrices[kline.Symbol] = kline.Open
}
session.lastPrices[kline.Symbol] = kline.Close
session.marketDataStores[kline.Symbol].AddKLine(kline)
})

View File

@ -16,7 +16,7 @@ func init() {
BuildCmd.Flags().StringP("output", "o", "", "binary output")
BuildCmd.Flags().String("os", runtime.GOOS, "GOOS")
BuildCmd.Flags().String("arch", runtime.GOARCH, "GOARCH")
BuildCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file")
BuildCmd.Flags().String("config", "", "config file")
RootCmd.AddCommand(BuildCmd)
}

View File

@ -31,7 +31,7 @@ func init() {
RunCmd.Flags().String("os", runtime.GOOS, "GOOS")
RunCmd.Flags().String("arch", runtime.GOARCH, "GOARCH")
RunCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file")
RunCmd.Flags().String("config", "", "config file")
RunCmd.Flags().String("since", "", "pnl since time")
RootCmd.AddCommand(RunCmd)
}
@ -196,13 +196,19 @@ var RunCmd = &cobra.Command{
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
userConfig, err := bbgo.Load(configFile)
userConfig, err := bbgo.Preload(configFile)
if err != nil {
return err
}
// if there is no custom imports, we don't have to compile
if noCompile || len(userConfig.Imports) == 0 {
if noCompile {
userConfig, err = bbgo.Load(configFile)
if err != nil {
return err
}
log.Infof("running config without wrapper binary...")
if err := runConfig(ctx, userConfig); err != nil {
return err
}
@ -210,9 +216,14 @@ var RunCmd = &cobra.Command{
return nil
}
shouldCompile := len(userConfig.Imports) > 0
if shouldCompile {
log.Infof("found imports %v, compiling wrapper binary...", userConfig.Imports)
}
var runArgs = []string{"run", "--no-compile"}
cmd.Flags().Visit(func(flag *flag.Flag) {
runArgs = append(runArgs, flag.Name, flag.Value.String())
runArgs = append(runArgs, "--" + flag.Name, flag.Value.String())
})
runArgs = append(runArgs, args...)
@ -296,7 +307,6 @@ func buildAndRun(ctx context.Context, userConfig *bbgo.Config, goOS, goArch stri
executePath := filepath.Join(cwd, binary)
log.Infof("running wrapper binary, args: %v", args)
runCmd := exec.CommandContext(ctx, executePath, args...)
runCmd.Stdout = os.Stdout
runCmd.Stderr = os.Stderr