From 68253be3e8c6872402100ed311fb4825a4f47bf2 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 30 Aug 2025 22:47:15 +0200 Subject: [PATCH] Add CanStartInstance method to check instance start conditions --- pkg/manager/manager.go | 1 + pkg/manager/operations.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 5d60f3a..0a749a8 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -21,6 +21,7 @@ type InstanceManager interface { UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) DeleteInstance(name string) error StartInstance(name string) (*instance.Process, error) + CanStartInstance(inst *instance.Process) (bool, error) StopInstance(name string) (*instance.Process, error) RestartInstance(name string) (*instance.Process, error) GetInstanceLogs(name string) (string, error) diff --git a/pkg/manager/operations.go b/pkg/manager/operations.go index 21f9727..5f8b910 100644 --- a/pkg/manager/operations.go +++ b/pkg/manager/operations.go @@ -201,6 +201,32 @@ func (im *instanceManager) StartInstance(name string) (*instance.Process, error) return instance, nil } +// CanStartInstance checks if an instance can be started. +// An instance can be started if: +// 1. It is not already running, AND +// 2. Either: +// a) There is capacity (MaxRunningInstances is unlimited or not reached), OR +// b) The instance has on-demand start enabled AND LRU eviction is enabled +// (allowing older instances to be evicted to make room) +func (im *instanceManager) CanStartInstance(inst *instance.Process) (bool, error) { + im.mu.RLock() + defer im.mu.RUnlock() + + if inst == nil { + return false, fmt.Errorf("instance is nil") + } + + if inst.IsRunning() { + return false, fmt.Errorf("instance with name %s is already running", inst.Name) + } + + allowMaxRunning := im.instancesConfig.MaxRunningInstances == -1 || len(im.runningInstances) < im.instancesConfig.MaxRunningInstances + allowOnDemand := inst.GetOptions() != nil && inst.GetOptions().OnDemandStart != nil && *inst.GetOptions().OnDemandStart + allowLRUEviction := im.instancesConfig.EnableLRUEviction + + return allowMaxRunning || (allowOnDemand && allowLRUEviction), nil +} + // StopInstance stops a running instance and returns it. func (im *instanceManager) StopInstance(name string) (*instance.Process, error) { im.mu.RLock()