Engine/GCTScript: Refactor script manager (#580)

* refactor script manager

* remove singleton GCTScriptConfig
* create constant for ".gct" extension
* move GctScriptManager into vm package
* reduce script manager global dependencies
* use manager struct to store runtime override values
* enable/disable scripting subsystem now doesn't store the setting in
config (aligned with other subsystems)
* setting max VMs via start option doesn't change config

* instantiate scriptmanager as part of creating a new Engine

    * script manager config is now set during instantiation
    * run script manager when enabled in conf or explicitly enabled
    * use the Started() method to check if script manager is running

* in tests set script manager as running

* script manager adjustments

* create manager before attempting overrides
* check for nil config when creating script manager

* fix script manager waitgroup counter increased too late

* move autoload() function to autoload.go
* add tests to script manager
This commit is contained in:
Rauno Ots
2020-11-05 05:44:53 +01:00
committed by GitHub
parent ee55ae5d0f
commit 1b65d97b65
19 changed files with 520 additions and 253 deletions

View File

@@ -5,21 +5,22 @@ import (
"os"
"path/filepath"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/log"
)
// Autoload remove entry from autoload slice
func Autoload(name string, remove bool) error {
if filepath.Ext(name) != ".gct" {
name += ".gct"
func (g *GctScriptManager) Autoload(name string, remove bool) error {
if filepath.Ext(name) != common.GctExt {
name += common.GctExt
}
if remove {
for x := range GCTScriptConfig.AutoLoad {
if GCTScriptConfig.AutoLoad[x] != name {
for x := range g.config.AutoLoad {
if g.config.AutoLoad[x] != name {
continue
}
GCTScriptConfig.AutoLoad = append(GCTScriptConfig.AutoLoad[:x], GCTScriptConfig.AutoLoad[x+1:]...)
if GCTScriptConfig.Verbose {
g.config.AutoLoad = append(g.config.AutoLoad[:x], g.config.AutoLoad[x+1:]...)
if g.config.Verbose {
log.Debugf(log.GCTScriptMgr, "Removing script: %s from autoload", name)
}
return nil
@@ -35,9 +36,35 @@ func Autoload(name string, remove bool) error {
}
return err
}
GCTScriptConfig.AutoLoad = append(GCTScriptConfig.AutoLoad, name)
if GCTScriptConfig.Verbose {
g.config.AutoLoad = append(g.config.AutoLoad, name)
if g.config.Verbose {
log.Debugf(log.GCTScriptMgr, "Adding script: %s to autoload", name)
}
return nil
}
func (g *GctScriptManager) autoLoad() {
for x := range g.config.AutoLoad {
temp := g.New()
if temp == nil {
log.Errorf(log.GCTScriptMgr, "Unable to create Virtual Machine, autoload failed for: %v",
g.config.AutoLoad[x])
continue
}
var name = g.config.AutoLoad[x]
if filepath.Ext(name) != common.GctExt {
name += common.GctExt
}
scriptPath := filepath.Join(ScriptPath, name)
err := temp.Load(scriptPath)
if err != nil {
log.Errorf(log.GCTScriptMgr, "%v failed to load: %v", filepath.Base(scriptPath), err)
err = temp.unregister()
if err != nil {
log.Errorf(log.GCTScriptMgr, "%v failed to unregister: %v", filepath.Base(scriptPath), err)
}
continue
}
go temp.CompileAndRun()
}
}