mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 00:54:23 +00:00
Introduce MaxRunningInstancesError type and handle it in StartInstance handler
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MaxRunningInstancesError error
|
||||||
|
|
||||||
// ListInstances returns a list of all instances managed by the instance manager.
|
// ListInstances returns a list of all instances managed by the instance manager.
|
||||||
func (im *instanceManager) ListInstances() ([]*instance.Process, error) {
|
func (im *instanceManager) ListInstances() ([]*instance.Process, error) {
|
||||||
im.mu.RLock()
|
im.mu.RLock()
|
||||||
@@ -42,7 +44,7 @@ func (im *instanceManager) CreateInstance(name string, options *instance.CreateI
|
|||||||
|
|
||||||
// Check max instances limit after acquiring the lock
|
// Check max instances limit after acquiring the lock
|
||||||
if len(im.instances) >= im.instancesConfig.MaxInstances && im.instancesConfig.MaxInstances != -1 {
|
if len(im.instances) >= im.instancesConfig.MaxInstances && im.instancesConfig.MaxInstances != -1 {
|
||||||
return nil, fmt.Errorf("maximum number of instances (%d) reached", im.instancesConfig.MaxInstances)
|
return nil, MaxRunningInstancesError(fmt.Errorf("maximum number of instances (%d) reached", im.instancesConfig.MaxInstances))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if instance with this name already exists
|
// Check if instance with this name already exists
|
||||||
@@ -182,7 +184,7 @@ func (im *instanceManager) StartInstance(name string) (*instance.Process, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(im.runningInstances) >= im.instancesConfig.MaxRunningInstances && im.instancesConfig.MaxRunningInstances != -1 {
|
if len(im.runningInstances) >= im.instancesConfig.MaxRunningInstances && im.instancesConfig.MaxRunningInstances != -1 {
|
||||||
return nil, fmt.Errorf("maximum number of running instances (%d) reached", im.instancesConfig.MaxRunningInstances)
|
return nil, MaxRunningInstancesError(fmt.Errorf("maximum number of running instances (%d) reached", im.instancesConfig.MaxRunningInstances))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := instance.Start(); err != nil {
|
if err := instance.Start(); err != nil {
|
||||||
|
|||||||
@@ -272,6 +272,12 @@ func (h *Handler) StartInstance() http.HandlerFunc {
|
|||||||
|
|
||||||
inst, err := h.InstanceManager.StartInstance(name)
|
inst, err := h.InstanceManager.StartInstance(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Check if error is due to maximum running instances limit
|
||||||
|
if _, ok := err.(manager.MaxRunningInstancesError); ok {
|
||||||
|
http.Error(w, err.Error(), http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
http.Error(w, "Failed to start instance: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to start instance: "+err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user