mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-31 23:16:54 +00:00
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:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/d5/tengo/v2"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/common/crypto"
|
||||
scriptevent "github.com/thrasher-corp/gocryptotrader/database/repository/script"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/loader"
|
||||
@@ -21,7 +22,14 @@ import (
|
||||
)
|
||||
|
||||
// NewVM attempts to create a new Virtual Machine firstly from pool
|
||||
func NewVM() (vm *VM) {
|
||||
func (g *GctScriptManager) NewVM() (vm *VM) {
|
||||
if !g.Started() {
|
||||
log.Error(log.GCTScriptMgr, Error{
|
||||
Action: "NewVM",
|
||||
Cause: ErrScriptingDisabled,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
newUUID, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
log.Error(log.GCTScriptMgr, Error{
|
||||
@@ -31,13 +39,15 @@ func NewVM() (vm *VM) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if GCTScriptConfig.Verbose {
|
||||
if g.config.Verbose {
|
||||
log.Debugln(log.GCTScriptMgr, "New GCTScript VM created")
|
||||
}
|
||||
|
||||
vm = &VM{
|
||||
ID: newUUID,
|
||||
Script: pool.Get().(*tengo.Script),
|
||||
ID: newUUID,
|
||||
Script: pool.Get().(*tengo.Script),
|
||||
config: g.config,
|
||||
unregister: func() error { return g.RemoveVM(newUUID) },
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -53,18 +63,11 @@ func (vm *VM) Load(file string) error {
|
||||
return ErrNoVMLoaded
|
||||
}
|
||||
|
||||
if !GCTScriptConfig.Enabled {
|
||||
return &Error{
|
||||
Action: "Load",
|
||||
Cause: ErrScriptingDisabled,
|
||||
}
|
||||
if filepath.Ext(file) != common.GctExt {
|
||||
file += common.GctExt
|
||||
}
|
||||
|
||||
if filepath.Ext(file) != ".gct" {
|
||||
file += ".gct"
|
||||
}
|
||||
|
||||
if GCTScriptConfig.Verbose {
|
||||
if vm.config.Verbose {
|
||||
log.Debugf(log.GCTScriptMgr, "Loading script: %s ID: %v", vm.ShortName(), vm.ID)
|
||||
}
|
||||
|
||||
@@ -89,8 +92,8 @@ func (vm *VM) Load(file string) error {
|
||||
vm.Script.SetImports(loader.GetModuleMap())
|
||||
vm.Hash = vm.getHash()
|
||||
|
||||
if GCTScriptConfig.AllowImports {
|
||||
if GCTScriptConfig.Verbose {
|
||||
if vm.config.AllowImports {
|
||||
if vm.config.Verbose {
|
||||
log.Debugf(log.GCTScriptMgr, "File imports enabled for vm: %v", vm.ID)
|
||||
}
|
||||
vm.Script.EnableFileImport(true)
|
||||
@@ -108,7 +111,7 @@ func (vm *VM) Compile() (err error) {
|
||||
|
||||
// Run runs byte code
|
||||
func (vm *VM) Run() (err error) {
|
||||
if GCTScriptConfig.Verbose {
|
||||
if vm.config.Verbose {
|
||||
log.Debugf(log.GCTScriptMgr, "Running script: %s ID: %v", vm.ShortName(), vm.ID)
|
||||
}
|
||||
|
||||
@@ -130,10 +133,10 @@ func (vm *VM) RunCtx() (err error) {
|
||||
vm.ctx = context.Background()
|
||||
}
|
||||
|
||||
ct, cancel := context.WithTimeout(vm.ctx, GCTScriptConfig.ScriptTimeout)
|
||||
ct, cancel := context.WithTimeout(vm.ctx, vm.config.ScriptTimeout)
|
||||
defer cancel()
|
||||
|
||||
if GCTScriptConfig.Verbose {
|
||||
if vm.config.Verbose {
|
||||
log.Debugf(log.GCTScriptMgr, "Running script: %s ID: %v", vm.ShortName(), vm.ID)
|
||||
}
|
||||
|
||||
@@ -157,7 +160,7 @@ func (vm *VM) CompileAndRun() {
|
||||
err := vm.Compile()
|
||||
if err != nil {
|
||||
log.Error(log.GCTScriptMgr, err)
|
||||
err = RemoveVM(vm.ID)
|
||||
err = vm.unregister()
|
||||
if err != nil {
|
||||
log.Error(log.GCTScriptMgr, err)
|
||||
}
|
||||
@@ -167,7 +170,7 @@ func (vm *VM) CompileAndRun() {
|
||||
err = vm.RunCtx()
|
||||
if err != nil {
|
||||
log.Error(log.GCTScriptMgr, err)
|
||||
err = RemoveVM(vm.ID)
|
||||
err = vm.unregister()
|
||||
if err != nil {
|
||||
log.Error(log.GCTScriptMgr, err)
|
||||
}
|
||||
@@ -209,13 +212,13 @@ func (vm *VM) Shutdown() error {
|
||||
if vm.S != nil {
|
||||
close(vm.S)
|
||||
}
|
||||
if GCTScriptConfig.Verbose {
|
||||
if vm.config.Verbose {
|
||||
log.Debugf(log.GCTScriptMgr, "Shutting down script: %s ID: %v", vm.ShortName(), vm.ID)
|
||||
}
|
||||
vm.Script = nil
|
||||
pool.Put(vm.Script)
|
||||
vm.event(StatusSuccess, TypeStop)
|
||||
return RemoveVM(vm.ID)
|
||||
return vm.unregister()
|
||||
}
|
||||
|
||||
// Read contents of script back and create script event
|
||||
@@ -226,7 +229,7 @@ func (vm *VM) Read() ([]byte, error) {
|
||||
|
||||
// Read contents of script back
|
||||
func (vm *VM) read() ([]byte, error) {
|
||||
if GCTScriptConfig.Verbose {
|
||||
if vm.config.Verbose {
|
||||
log.Debugf(log.GCTScriptMgr, "Read script: %s ID: %v", vm.ShortName(), vm.ID)
|
||||
}
|
||||
return ioutil.ReadFile(vm.File)
|
||||
|
||||
Reference in New Issue
Block a user