mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 09:04:27 +00:00
Refactor instance management: replace CanStartInstance with IsMaxRunningInstancesReached method
This commit is contained in:
@@ -21,7 +21,7 @@ type InstanceManager interface {
|
|||||||
UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
|
UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
|
||||||
DeleteInstance(name string) error
|
DeleteInstance(name string) error
|
||||||
StartInstance(name string) (*instance.Process, error)
|
StartInstance(name string) (*instance.Process, error)
|
||||||
CanStartInstance(inst *instance.Process) (bool, error)
|
IsMaxRunningInstancesReached() bool
|
||||||
StopInstance(name string) (*instance.Process, error)
|
StopInstance(name string) (*instance.Process, error)
|
||||||
RestartInstance(name string) (*instance.Process, error)
|
RestartInstance(name string) (*instance.Process, error)
|
||||||
GetInstanceLogs(name string) (string, error)
|
GetInstanceLogs(name string) (string, error)
|
||||||
|
|||||||
@@ -201,30 +201,15 @@ func (im *instanceManager) StartInstance(name string) (*instance.Process, error)
|
|||||||
return instance, nil
|
return instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanStartInstance checks if an instance can be started.
|
func (im *instanceManager) IsMaxRunningInstancesReached() bool {
|
||||||
// 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()
|
im.mu.RLock()
|
||||||
defer im.mu.RUnlock()
|
defer im.mu.RUnlock()
|
||||||
|
|
||||||
if inst == nil {
|
if im.instancesConfig.MaxRunningInstances != -1 && len(im.runningInstances) >= im.instancesConfig.MaxRunningInstances {
|
||||||
return false, fmt.Errorf("instance is nil")
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if inst.IsRunning() {
|
return false
|
||||||
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.
|
// StopInstance stops a running instance and returns it.
|
||||||
|
|||||||
Reference in New Issue
Block a user