Files
gocryptotrader/gctscript/vm/manager.go
Adrian Gallagher d64d56f77c build/ci: Update Go to v1.24, golangci-lint to v1.64.6 and fix issues (#1804)
* build/ci: Update Go to v1.24, golangci-lint to v1.64.5 and fix issues

* Address shazbert's nitters

* linter/config: Fix new linter issue and use versionSize const

* Address gk's nitters and fix additional linter issue after rebase

* Address glorious nits

* staticcheck: Fix additional linter issues after upgrading to Go 1.24.1 and golangci-lint v1.64.6

Also addresses nits

* Improve testing, assertify usage and use common.ErrParsingWSField

* TestCreateNewStrategy: Replace must > should wording
2025-03-10 16:33:55 +11:00

92 lines
2.2 KiB
Go

package vm
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/log"
)
const (
caseName = "GCTScript"
// Name is an exported subsystem name
Name = "gctscript"
)
// ErrNilSubsystem returned when script manager has not been set up
var ErrNilSubsystem = errors.New("gct script has not been set up")
// GctScriptManager loads and runs GCT Tengo scripts
type GctScriptManager struct {
config *Config
started int32
shutdown chan struct{}
// Optional values to override stored config ('nil' if not overridden)
MaxVirtualMachines *uint64
}
// NewManager creates a new instance of script manager
func NewManager(config *Config) (*GctScriptManager, error) {
if config == nil {
return nil, errors.New("config must be provided for script manager")
}
return &GctScriptManager{config: config}, nil
}
// IsRunning returns if gctscript manager subsystem is started
func (g *GctScriptManager) IsRunning() bool {
return g != nil && atomic.LoadInt32(&g.started) == 1
}
// Start starts gctscript subsystem and creates shutdown channel
func (g *GctScriptManager) Start(wg *sync.WaitGroup) (err error) {
if wg == nil {
return fmt.Errorf("%T %w", wg, common.ErrNilPointer)
}
if !atomic.CompareAndSwapInt32(&g.started, 0, 1) {
return fmt.Errorf("%s %s", caseName, ErrScriptFailedValidation)
}
g.shutdown = make(chan struct{})
wg.Add(1)
go g.run(wg)
return nil
}
// Stop stops gctscript subsystem along with all running Virtual Machines
func (g *GctScriptManager) Stop() error {
if g == nil {
return fmt.Errorf("%s %w", caseName, ErrNilSubsystem)
}
if atomic.LoadInt32(&g.started) == 0 {
return fmt.Errorf("%s not running", caseName)
}
defer atomic.CompareAndSwapInt32(&g.started, 1, 0)
if err := g.ShutdownAll(); err != nil {
return err
}
close(g.shutdown)
return nil
}
func (g *GctScriptManager) run(wg *sync.WaitGroup) {
log.Debugf(log.Global, "%s starting", caseName)
SetDefaultScriptOutput()
g.autoLoad()
defer wg.Done()
<-g.shutdown
}
// GetMaxVirtualMachines returns the max number of VMs to create
func (g *GctScriptManager) GetMaxVirtualMachines() uint64 {
if g.MaxVirtualMachines != nil {
return *g.MaxVirtualMachines
}
return g.config.MaxVirtualMachines
}