From 615c2ac54eefc16236b67ac7805f1be13aa9d283 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 27 Aug 2025 18:42:34 +0200 Subject: [PATCH] Add MaxRunningInstances to InstancesConfig and implement IsRunning method --- pkg/config/config.go | 9 +++++++++ pkg/instance/lifecycle.go | 6 ++++++ pkg/manager/manager.go | 16 +++++++++------- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 4486fa5..89080ed 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -55,6 +55,9 @@ type InstancesConfig struct { // Maximum number of instances that can be created MaxInstances int `yaml:"max_instances"` + // Maximum number of instances that can be running at the same time + MaxRunningInstances int `yaml:"max_running_instances,omitempty"` + // Path to llama-server executable LlamaExecutable string `yaml:"llama_executable"` @@ -113,6 +116,7 @@ func LoadConfig(configPath string) (AppConfig, error) { LogsDir: filepath.Join(getDefaultDataDirectory(), "logs"), AutoCreateDirs: true, MaxInstances: -1, // -1 means unlimited + MaxRunningInstances: -1, // -1 means unlimited LlamaExecutable: "llama-server", DefaultAutoRestart: true, DefaultMaxRestarts: 3, @@ -211,6 +215,11 @@ func loadEnvVars(cfg *AppConfig) { cfg.Instances.MaxInstances = m } } + if maxRunning := os.Getenv("LLAMACTL_MAX_RUNNING_INSTANCES"); maxRunning != "" { + if m, err := strconv.Atoi(maxRunning); err == nil { + cfg.Instances.MaxRunningInstances = m + } + } if llamaExec := os.Getenv("LLAMACTL_LLAMA_EXECUTABLE"); llamaExec != "" { cfg.Instances.LlamaExecutable = llamaExec } diff --git a/pkg/instance/lifecycle.go b/pkg/instance/lifecycle.go index 84d5ea3..eb2fc83 100644 --- a/pkg/instance/lifecycle.go +++ b/pkg/instance/lifecycle.go @@ -11,6 +11,12 @@ import ( "time" ) +func (i *Process) IsRunning() bool { + i.mu.RLock() + defer i.mu.RUnlock() + return i.Running +} + // Start starts the llama server instance and returns an error if it fails. func (i *Process) Start() error { i.mu.Lock() diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index bac7567..31c0b01 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -28,10 +28,11 @@ type InstanceManager interface { } type instanceManager struct { - mu sync.RWMutex - instances map[string]*instance.Process - ports map[int]bool - instancesConfig config.InstancesConfig + mu sync.RWMutex + instances map[string]*instance.Process + runningInstances map[*instance.Process]struct{} + ports map[int]bool + instancesConfig config.InstancesConfig // Timeout checker timeoutChecker *time.Ticker @@ -46,9 +47,10 @@ func NewInstanceManager(instancesConfig config.InstancesConfig) InstanceManager instancesConfig.TimeoutCheckInterval = 5 // Default to 5 minutes if not set } im := &instanceManager{ - instances: make(map[string]*instance.Process), - ports: make(map[int]bool), - instancesConfig: instancesConfig, + instances: make(map[string]*instance.Process), + runningInstances: make(map[*instance.Process]struct{}), + ports: make(map[int]bool), + instancesConfig: instancesConfig, timeoutChecker: time.NewTicker(time.Duration(instancesConfig.TimeoutCheckInterval) * time.Minute), shutdownChan: make(chan struct{}),