Rename Process to Instance

This commit is contained in:
2025-10-16 19:38:44 +02:00
parent 964c6345ef
commit 80ca0cbd4f
14 changed files with 110 additions and 110 deletions

View File

@@ -14,8 +14,8 @@ import (
"time" "time"
) )
// Process represents a running instance of the llama server // Instance represents a running instance of the llama server
type Process struct { type Instance struct {
Name string `json:"name"` Name string `json:"name"`
options *CreateInstanceOptions `json:"-"` options *CreateInstanceOptions `json:"-"`
globalInstanceSettings *config.InstancesConfig globalInstanceSettings *config.InstancesConfig
@@ -29,10 +29,10 @@ type Process struct {
Created int64 `json:"created,omitempty"` // Unix timestamp when the instance was created Created int64 `json:"created,omitempty"` // Unix timestamp when the instance was created
// Logging file // Logging file
logger *Logger `json:"-"` logger *logger `json:"-"`
// Proxy component // Proxy component
proxy *Proxy `json:"-"` // HTTP proxy and request tracking proxy *proxy `json:"-"` // HTTP proxy and request tracking
// internal // internal
cmd *exec.Cmd `json:"-"` // Command to run the instance cmd *exec.Cmd `json:"-"` // Command to run the instance
@@ -49,14 +49,14 @@ type Process struct {
} }
// NewInstance creates a new instance with the given name, log path, and options // NewInstance creates a new instance with the given name, log path, and options
func NewInstance(name string, globalBackendSettings *config.BackendConfig, globalInstanceSettings *config.InstancesConfig, options *CreateInstanceOptions, onStatusChange func(oldStatus, newStatus InstanceStatus)) *Process { func NewInstance(name string, globalBackendSettings *config.BackendConfig, globalInstanceSettings *config.InstancesConfig, options *CreateInstanceOptions, onStatusChange func(oldStatus, newStatus InstanceStatus)) *Instance {
// Validate and copy options // Validate and copy options
options.ValidateAndApplyDefaults(name, globalInstanceSettings) options.ValidateAndApplyDefaults(name, globalInstanceSettings)
// Create the instance logger // Create the instance logger
logger := NewInstanceLogger(name, globalInstanceSettings.LogsDir) logger := NewLogger(name, globalInstanceSettings.LogsDir)
instance := &Process{ instance := &Instance{
Name: name, Name: name,
options: options, options: options,
globalInstanceSettings: globalInstanceSettings, globalInstanceSettings: globalInstanceSettings,
@@ -73,13 +73,13 @@ func NewInstance(name string, globalBackendSettings *config.BackendConfig, globa
return instance return instance
} }
func (i *Process) GetOptions() *CreateInstanceOptions { func (i *Instance) GetOptions() *CreateInstanceOptions {
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
return i.options return i.options
} }
func (i *Process) GetPort() int { func (i *Instance) GetPort() int {
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
if i.options != nil { if i.options != nil {
@@ -101,7 +101,7 @@ func (i *Process) GetPort() int {
return 0 return 0
} }
func (i *Process) GetHost() string { func (i *Instance) GetHost() string {
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
if i.options != nil { if i.options != nil {
@@ -123,7 +123,7 @@ func (i *Process) GetHost() string {
return "" return ""
} }
func (i *Process) SetOptions(options *CreateInstanceOptions) { func (i *Instance) SetOptions(options *CreateInstanceOptions) {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@@ -145,14 +145,14 @@ func (i *Process) SetOptions(options *CreateInstanceOptions) {
// SetTimeProvider sets a custom time provider for testing // SetTimeProvider sets a custom time provider for testing
// Delegates to the Proxy component // Delegates to the Proxy component
func (i *Process) SetTimeProvider(tp TimeProvider) { func (i *Instance) SetTimeProvider(tp TimeProvider) {
if i.proxy != nil { if i.proxy != nil {
i.proxy.SetTimeProvider(tp) i.proxy.SetTimeProvider(tp)
} }
} }
// GetProxy returns the reverse proxy for this instance, delegating to Proxy component // GetProxy returns the reverse proxy for this instance, delegating to Proxy component
func (i *Process) GetProxy() (*httputil.ReverseProxy, error) { func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) {
if i.proxy == nil { if i.proxy == nil {
return nil, fmt.Errorf("instance %s has no proxy component", i.Name) return nil, fmt.Errorf("instance %s has no proxy component", i.Name)
} }
@@ -160,7 +160,7 @@ func (i *Process) GetProxy() (*httputil.ReverseProxy, error) {
} }
// MarshalJSON implements json.Marshaler for Instance // MarshalJSON implements json.Marshaler for Instance
func (i *Process) MarshalJSON() ([]byte, error) { func (i *Instance) MarshalJSON() ([]byte, error) {
// Use read lock since we're only reading data // Use read lock since we're only reading data
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
@@ -183,7 +183,7 @@ func (i *Process) MarshalJSON() ([]byte, error) {
} }
// Use anonymous struct to avoid recursion // Use anonymous struct to avoid recursion
type Alias Process type Alias Instance
return json.Marshal(&struct { return json.Marshal(&struct {
*Alias *Alias
Options *CreateInstanceOptions `json:"options,omitempty"` Options *CreateInstanceOptions `json:"options,omitempty"`
@@ -196,9 +196,9 @@ func (i *Process) MarshalJSON() ([]byte, error) {
} }
// UnmarshalJSON implements json.Unmarshaler for Instance // UnmarshalJSON implements json.Unmarshaler for Instance
func (i *Process) UnmarshalJSON(data []byte) error { func (i *Instance) UnmarshalJSON(data []byte) error {
// Use anonymous struct to avoid recursion // Use anonymous struct to avoid recursion
type Alias Process type Alias Instance
aux := &struct { aux := &struct {
*Alias *Alias
Options *CreateInstanceOptions `json:"options,omitempty"` Options *CreateInstanceOptions `json:"options,omitempty"`
@@ -218,7 +218,7 @@ func (i *Process) UnmarshalJSON(data []byte) error {
// Initialize fields that are not serialized // Initialize fields that are not serialized
if i.logger == nil && i.globalInstanceSettings != nil { if i.logger == nil && i.globalInstanceSettings != nil {
i.logger = NewInstanceLogger(i.Name, i.globalInstanceSettings.LogsDir) i.logger = NewLogger(i.Name, i.globalInstanceSettings.LogsDir)
} }
if i.proxy == nil { if i.proxy == nil {
i.proxy = NewProxy(i) i.proxy = NewProxy(i)
@@ -227,7 +227,7 @@ func (i *Process) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func (i *Process) IsRemote() bool { func (i *Instance) IsRemote() bool {
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
@@ -238,13 +238,13 @@ func (i *Process) IsRemote() bool {
return len(i.options.Nodes) > 0 return len(i.options.Nodes) > 0
} }
func (i *Process) GetLogs(num_lines int) (string, error) { func (i *Instance) GetLogs(num_lines int) (string, error) {
return i.logger.GetLogs(num_lines) return i.logger.GetLogs(num_lines)
} }
// getBackendHostPort extracts the host and port from instance options // getBackendHostPort extracts the host and port from instance options
// Returns the configured host and port for the backend // Returns the configured host and port for the backend
func (i *Process) getBackendHostPort() (string, int) { func (i *Instance) getBackendHostPort() (string, int) {
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()

View File

@@ -345,7 +345,7 @@ func TestUnmarshalJSON(t *testing.T) {
} }
}` }`
var inst instance.Process var inst instance.Instance
err := json.Unmarshal([]byte(jsonData), &inst) err := json.Unmarshal([]byte(jsonData), &inst)
if err != nil { if err != nil {
t.Fatalf("JSON unmarshal failed: %v", err) t.Fatalf("JSON unmarshal failed: %v", err)

View File

@@ -16,7 +16,7 @@ import (
) )
// Start starts the llama server instance and returns an error if it fails. // Start starts the llama server instance and returns an error if it fails.
func (i *Process) Start() error { func (i *Instance) Start() error {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@@ -90,7 +90,7 @@ func (i *Process) Start() error {
} }
// Stop terminates the subprocess // Stop terminates the subprocess
func (i *Process) Stop() error { func (i *Instance) Stop() error {
i.mu.Lock() i.mu.Lock()
if !i.IsRunning() { if !i.IsRunning() {
@@ -160,14 +160,14 @@ func (i *Process) Stop() error {
// LastRequestTime returns the last request time as a Unix timestamp // LastRequestTime returns the last request time as a Unix timestamp
// Delegates to the Proxy component // Delegates to the Proxy component
func (i *Process) LastRequestTime() int64 { func (i *Instance) LastRequestTime() int64 {
if i.proxy == nil { if i.proxy == nil {
return 0 return 0
} }
return i.proxy.LastRequestTime() return i.proxy.LastRequestTime()
} }
func (i *Process) WaitForHealthy(timeout int) error { func (i *Instance) WaitForHealthy(timeout int) error {
if !i.IsRunning() { if !i.IsRunning() {
return fmt.Errorf("instance %s is not running", i.Name) return fmt.Errorf("instance %s is not running", i.Name)
} }
@@ -226,7 +226,7 @@ func (i *Process) WaitForHealthy(timeout int) error {
} }
} }
func (i *Process) monitorProcess() { func (i *Instance) monitorProcess() {
defer func() { defer func() {
i.mu.Lock() i.mu.Lock()
if i.monitorDone != nil { if i.monitorDone != nil {
@@ -267,7 +267,7 @@ func (i *Process) monitorProcess() {
} }
// handleRestart manages the restart process while holding the lock // handleRestart manages the restart process while holding the lock
func (i *Process) handleRestart() { func (i *Instance) handleRestart() {
// Validate restart conditions and get safe parameters // Validate restart conditions and get safe parameters
shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions() shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions()
if !shouldRestart { if !shouldRestart {
@@ -310,7 +310,7 @@ func (i *Process) handleRestart() {
} }
// validateRestartConditions checks if the instance should be restarted and returns the parameters // validateRestartConditions checks if the instance should be restarted and returns the parameters
func (i *Process) validateRestartConditions() (shouldRestart bool, maxRestarts int, restartDelay int) { func (i *Instance) validateRestartConditions() (shouldRestart bool, maxRestarts int, restartDelay int) {
if i.options == nil { if i.options == nil {
log.Printf("Instance %s not restarting: options are nil", i.Name) log.Printf("Instance %s not restarting: options are nil", i.Name)
return false, 0, 0 return false, 0, 0
@@ -344,7 +344,7 @@ func (i *Process) validateRestartConditions() (shouldRestart bool, maxRestarts i
} }
// buildCommand builds the command to execute using backend-specific logic // buildCommand builds the command to execute using backend-specific logic
func (i *Process) buildCommand() (*exec.Cmd, error) { func (i *Instance) buildCommand() (*exec.Cmd, error) {
// Get backend configuration // Get backend configuration
backendConfig, err := i.getBackendConfig() backendConfig, err := i.getBackendConfig()
if err != nil { if err != nil {
@@ -375,7 +375,7 @@ func (i *Process) buildCommand() (*exec.Cmd, error) {
} }
// getBackendConfig resolves the backend configuration for the current instance // getBackendConfig resolves the backend configuration for the current instance
func (i *Process) getBackendConfig() (*config.BackendSettings, error) { func (i *Instance) getBackendConfig() (*config.BackendSettings, error) {
var backendTypeStr string var backendTypeStr string
switch i.options.BackendType { switch i.options.BackendType {

View File

@@ -10,7 +10,7 @@ import (
"time" "time"
) )
type Logger struct { type logger struct {
name string name string
logDir string logDir string
logFile *os.File logFile *os.File
@@ -18,15 +18,15 @@ type Logger struct {
mu sync.RWMutex mu sync.RWMutex
} }
func NewInstanceLogger(name string, logDir string) *Logger { func NewLogger(name string, logDir string) *logger {
return &Logger{ return &logger{
name: name, name: name,
logDir: logDir, logDir: logDir,
} }
} }
// Create creates and opens the log files for stdout and stderr // Create creates and opens the log files for stdout and stderr
func (i *Logger) Create() error { func (i *logger) Create() error {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@@ -57,7 +57,7 @@ func (i *Logger) Create() error {
} }
// GetLogs retrieves the last n lines of logs from the instance // GetLogs retrieves the last n lines of logs from the instance
func (i *Logger) GetLogs(num_lines int) (string, error) { func (i *logger) GetLogs(num_lines int) (string, error) {
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
@@ -98,7 +98,7 @@ func (i *Logger) GetLogs(num_lines int) (string, error) {
} }
// closeLogFile closes the log files // closeLogFile closes the log files
func (i *Logger) Close() { func (i *logger) Close() {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@@ -111,7 +111,7 @@ func (i *Logger) Close() {
} }
// readOutput reads from the given reader and writes lines to the log file // readOutput reads from the given reader and writes lines to the log file
func (i *Logger) readOutput(reader io.ReadCloser) { func (i *logger) readOutput(reader io.ReadCloser) {
defer reader.Close() defer reader.Close()
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)

View File

@@ -23,9 +23,9 @@ func (realTimeProvider) Now() time.Time {
return time.Now() return time.Now()
} }
// Proxy manages HTTP reverse proxy and request tracking for an instance. // proxy manages HTTP reverse proxy and request tracking for an instance.
type Proxy struct { type proxy struct {
process *Process // Owner reference - Proxy is owned by Process process *Instance // Owner reference - Proxy is owned by Process
mu sync.RWMutex mu sync.RWMutex
proxy *httputil.ReverseProxy proxy *httputil.ReverseProxy
@@ -36,8 +36,8 @@ type Proxy struct {
} }
// NewProxy creates a new Proxy for the given process // NewProxy creates a new Proxy for the given process
func NewProxy(process *Process) *Proxy { func NewProxy(process *Instance) *proxy {
return &Proxy{ return &proxy{
process: process, process: process,
timeProvider: realTimeProvider{}, timeProvider: realTimeProvider{},
} }
@@ -45,7 +45,7 @@ func NewProxy(process *Process) *Proxy {
// GetProxy returns the reverse proxy for this instance, creating it if needed. // GetProxy returns the reverse proxy for this instance, creating it if needed.
// Uses sync.Once to ensure thread-safe one-time initialization. // Uses sync.Once to ensure thread-safe one-time initialization.
func (p *Proxy) GetProxy() (*httputil.ReverseProxy, error) { func (p *proxy) GetProxy() (*httputil.ReverseProxy, error) {
// sync.Once guarantees buildProxy() is called exactly once // sync.Once guarantees buildProxy() is called exactly once
// Other callers block until first initialization completes // Other callers block until first initialization completes
p.proxyOnce.Do(func() { p.proxyOnce.Do(func() {
@@ -56,7 +56,7 @@ func (p *Proxy) GetProxy() (*httputil.ReverseProxy, error) {
} }
// buildProxy creates the reverse proxy based on instance options // buildProxy creates the reverse proxy based on instance options
func (p *Proxy) buildProxy() (*httputil.ReverseProxy, error) { func (p *proxy) buildProxy() (*httputil.ReverseProxy, error) {
options := p.process.GetOptions() options := p.process.GetOptions()
if options == nil { if options == nil {
return nil, fmt.Errorf("instance %s has no options set", p.process.Name) return nil, fmt.Errorf("instance %s has no options set", p.process.Name)
@@ -109,7 +109,7 @@ func (p *Proxy) buildProxy() (*httputil.ReverseProxy, error) {
// clearProxy resets the proxy, allowing it to be recreated when options change. // clearProxy resets the proxy, allowing it to be recreated when options change.
// This resets the sync.Once so the next GetProxy call will rebuild the proxy. // This resets the sync.Once so the next GetProxy call will rebuild the proxy.
func (p *Proxy) clearProxy() { func (p *proxy) clearProxy() {
p.mu.Lock() p.mu.Lock()
defer p.mu.Unlock() defer p.mu.Unlock()
@@ -119,18 +119,18 @@ func (p *Proxy) clearProxy() {
} }
// UpdateLastRequestTime updates the last request access time for the instance // UpdateLastRequestTime updates the last request access time for the instance
func (p *Proxy) UpdateLastRequestTime() { func (p *proxy) UpdateLastRequestTime() {
lastRequestTime := p.timeProvider.Now().Unix() lastRequestTime := p.timeProvider.Now().Unix()
p.lastRequestTime.Store(lastRequestTime) p.lastRequestTime.Store(lastRequestTime)
} }
// LastRequestTime returns the last request time as a Unix timestamp // LastRequestTime returns the last request time as a Unix timestamp
func (p *Proxy) LastRequestTime() int64 { func (p *proxy) LastRequestTime() int64 {
return p.lastRequestTime.Load() return p.lastRequestTime.Load()
} }
// ShouldTimeout checks if the instance should timeout based on idle time // ShouldTimeout checks if the instance should timeout based on idle time
func (p *Proxy) ShouldTimeout() bool { func (p *proxy) ShouldTimeout() bool {
if !p.process.IsRunning() { if !p.process.IsRunning() {
return false return false
} }
@@ -151,6 +151,6 @@ func (p *Proxy) ShouldTimeout() bool {
} }
// SetTimeProvider sets a custom time provider for testing // SetTimeProvider sets a custom time provider for testing
func (p *Proxy) SetTimeProvider(tp TimeProvider) { func (p *proxy) SetTimeProvider(tp TimeProvider) {
p.timeProvider = tp p.timeProvider = tp
} }

View File

@@ -26,7 +26,7 @@ var statusToName = map[InstanceStatus]string{
Failed: "failed", Failed: "failed",
} }
func (p *Process) SetStatus(status InstanceStatus) { func (p *Instance) SetStatus(status InstanceStatus) {
oldStatus := p.Status oldStatus := p.Status
p.Status = status p.Status = status
@@ -35,12 +35,12 @@ func (p *Process) SetStatus(status InstanceStatus) {
} }
} }
func (p *Process) GetStatus() InstanceStatus { func (p *Instance) GetStatus() InstanceStatus {
return p.Status return p.Status
} }
// IsRunning returns true if the status is Running // IsRunning returns true if the status is Running
func (p *Process) IsRunning() bool { func (p *Instance) IsRunning() bool {
return p.Status == Running return p.Status == Running
} }

View File

@@ -2,7 +2,7 @@ package instance
// UpdateLastRequestTime updates the last request access time for the instance via proxy // UpdateLastRequestTime updates the last request access time for the instance via proxy
// Delegates to the Proxy component // Delegates to the Proxy component
func (i *Process) UpdateLastRequestTime() { func (i *Instance) UpdateLastRequestTime() {
if i.proxy != nil { if i.proxy != nil {
i.proxy.UpdateLastRequestTime() i.proxy.UpdateLastRequestTime()
} }
@@ -10,7 +10,7 @@ func (i *Process) UpdateLastRequestTime() {
// ShouldTimeout checks if the instance should timeout based on idle time // ShouldTimeout checks if the instance should timeout based on idle time
// Delegates to the Proxy component // Delegates to the Proxy component
func (i *Process) ShouldTimeout() bool { func (i *Instance) ShouldTimeout() bool {
if i.proxy == nil { if i.proxy == nil {
return false return false
} }

View File

@@ -16,35 +16,35 @@ import (
// InstanceManager defines the interface for managing instances of the llama server. // InstanceManager defines the interface for managing instances of the llama server.
type InstanceManager interface { type InstanceManager interface {
ListInstances() ([]*instance.Process, error) ListInstances() ([]*instance.Instance, error)
CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
GetInstance(name string) (*instance.Process, error) GetInstance(name string) (*instance.Instance, error)
UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
DeleteInstance(name string) error DeleteInstance(name string) error
StartInstance(name string) (*instance.Process, error) StartInstance(name string) (*instance.Instance, error)
IsMaxRunningInstancesReached() bool IsMaxRunningInstancesReached() bool
StopInstance(name string) (*instance.Process, error) StopInstance(name string) (*instance.Instance, error)
EvictLRUInstance() error EvictLRUInstance() error
RestartInstance(name string) (*instance.Process, error) RestartInstance(name string) (*instance.Instance, error)
GetInstanceLogs(name string, numLines int) (string, error) GetInstanceLogs(name string, numLines int) (string, error)
Shutdown() Shutdown()
} }
type RemoteManager interface { type RemoteManager interface {
ListRemoteInstances(node *config.NodeConfig) ([]*instance.Process, error) ListRemoteInstances(node *config.NodeConfig) ([]*instance.Instance, error)
CreateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Process, error) CreateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
GetRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error) GetRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
UpdateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Process, error) UpdateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
DeleteRemoteInstance(node *config.NodeConfig, name string) error DeleteRemoteInstance(node *config.NodeConfig, name string) error
StartRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error) StartRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
StopRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error) StopRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
RestartRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error) RestartRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
GetRemoteInstanceLogs(node *config.NodeConfig, name string, numLines int) (string, error) GetRemoteInstanceLogs(node *config.NodeConfig, name string, numLines int) (string, error)
} }
type instanceManager struct { type instanceManager struct {
mu sync.RWMutex mu sync.RWMutex
instances map[string]*instance.Process instances map[string]*instance.Instance
runningInstances map[string]struct{} runningInstances map[string]struct{}
ports map[int]bool ports map[int]bool
instancesConfig config.InstancesConfig instancesConfig config.InstancesConfig
@@ -57,9 +57,9 @@ type instanceManager struct {
isShutdown bool isShutdown bool
// Remote instance management // Remote instance management
httpClient *http.Client httpClient *http.Client
instanceNodeMap map[string]*config.NodeConfig // Maps instance name to its node config instanceNodeMap map[string]*config.NodeConfig // Maps instance name to its node config
nodeConfigMap map[string]*config.NodeConfig // Maps node name to node config for quick lookup nodeConfigMap map[string]*config.NodeConfig // Maps node name to node config for quick lookup
} }
// NewInstanceManager creates a new instance of InstanceManager. // NewInstanceManager creates a new instance of InstanceManager.
@@ -76,7 +76,7 @@ func NewInstanceManager(backendsConfig config.BackendConfig, instancesConfig con
} }
im := &instanceManager{ im := &instanceManager{
instances: make(map[string]*instance.Process), instances: make(map[string]*instance.Instance),
runningInstances: make(map[string]struct{}), runningInstances: make(map[string]struct{}),
ports: make(map[int]bool), ports: make(map[int]bool),
instancesConfig: instancesConfig, instancesConfig: instancesConfig,
@@ -130,7 +130,7 @@ func (im *instanceManager) getNextAvailablePort() (int, error) {
} }
// persistInstance saves an instance to its JSON file // persistInstance saves an instance to its JSON file
func (im *instanceManager) persistInstance(instance *instance.Process) error { func (im *instanceManager) persistInstance(instance *instance.Instance) error {
if im.instancesConfig.InstancesDir == "" { if im.instancesConfig.InstancesDir == "" {
return nil // Persistence disabled return nil // Persistence disabled
} }
@@ -172,7 +172,7 @@ func (im *instanceManager) Shutdown() {
close(im.shutdownChan) close(im.shutdownChan)
// Create a list of running instances to stop // Create a list of running instances to stop
var runningInstances []*instance.Process var runningInstances []*instance.Instance
var runningNames []string var runningNames []string
for name, inst := range im.instances { for name, inst := range im.instances {
if inst.IsRunning() { if inst.IsRunning() {
@@ -197,7 +197,7 @@ func (im *instanceManager) Shutdown() {
wg.Add(len(runningInstances)) wg.Add(len(runningInstances))
for i, inst := range runningInstances { for i, inst := range runningInstances {
go func(name string, inst *instance.Process) { go func(name string, inst *instance.Instance) {
defer wg.Done() defer wg.Done()
fmt.Printf("Stopping instance %s...\n", name) fmt.Printf("Stopping instance %s...\n", name)
// Attempt to stop the instance gracefully // Attempt to stop the instance gracefully
@@ -261,7 +261,7 @@ func (im *instanceManager) loadInstance(name, path string) error {
return fmt.Errorf("failed to read instance file: %w", err) return fmt.Errorf("failed to read instance file: %w", err)
} }
var persistedInstance instance.Process var persistedInstance instance.Instance
if err := json.Unmarshal(data, &persistedInstance); err != nil { if err := json.Unmarshal(data, &persistedInstance); err != nil {
return fmt.Errorf("failed to unmarshal instance: %w", err) return fmt.Errorf("failed to unmarshal instance: %w", err)
} }
@@ -318,8 +318,8 @@ func (im *instanceManager) loadInstance(name, path string) error {
// For instances with auto-restart disabled, it sets their status to Stopped // For instances with auto-restart disabled, it sets their status to Stopped
func (im *instanceManager) autoStartInstances() { func (im *instanceManager) autoStartInstances() {
im.mu.RLock() im.mu.RLock()
var instancesToStart []*instance.Process var instancesToStart []*instance.Instance
var instancesToStop []*instance.Process var instancesToStop []*instance.Instance
for _, inst := range im.instances { for _, inst := range im.instances {
if inst.IsRunning() && // Was running when persisted if inst.IsRunning() && // Was running when persisted
inst.GetOptions() != nil && inst.GetOptions() != nil &&
@@ -374,7 +374,7 @@ func (im *instanceManager) onStatusChange(name string, oldStatus, newStatus inst
// getNodeForInstance returns the node configuration for a remote instance // getNodeForInstance returns the node configuration for a remote instance
// Returns nil if the instance is not remote or the node is not found // Returns nil if the instance is not remote or the node is not found
func (im *instanceManager) getNodeForInstance(inst *instance.Process) *config.NodeConfig { func (im *instanceManager) getNodeForInstance(inst *instance.Instance) *config.NodeConfig {
if !inst.IsRemote() { if !inst.IsRemote() {
return nil return nil
} }

View File

@@ -14,7 +14,7 @@ type MaxRunningInstancesError error
// updateLocalInstanceFromRemote updates the local stub instance with data from the remote instance // updateLocalInstanceFromRemote updates the local stub instance with data from the remote instance
// while preserving the Nodes field to maintain remote instance tracking // while preserving the Nodes field to maintain remote instance tracking
func (im *instanceManager) updateLocalInstanceFromRemote(localInst *instance.Process, remoteInst *instance.Process) { func (im *instanceManager) updateLocalInstanceFromRemote(localInst *instance.Instance, remoteInst *instance.Instance) {
if localInst == nil || remoteInst == nil { if localInst == nil || remoteInst == nil {
return return
} }
@@ -45,9 +45,9 @@ func (im *instanceManager) updateLocalInstanceFromRemote(localInst *instance.Pro
// ListInstances returns a list of all instances managed by the instance manager. // ListInstances returns a list of all instances managed by the instance manager.
// For remote instances, this fetches the live state from remote nodes and updates local stubs. // For remote instances, this fetches the live state from remote nodes and updates local stubs.
func (im *instanceManager) ListInstances() ([]*instance.Process, error) { func (im *instanceManager) ListInstances() ([]*instance.Instance, error) {
im.mu.RLock() im.mu.RLock()
localInstances := make([]*instance.Process, 0, len(im.instances)) localInstances := make([]*instance.Instance, 0, len(im.instances))
for _, inst := range im.instances { for _, inst := range im.instances {
localInstances = append(localInstances, inst) localInstances = append(localInstances, inst)
} }
@@ -75,7 +75,7 @@ func (im *instanceManager) ListInstances() ([]*instance.Process, error) {
// CreateInstance creates a new instance with the given options and returns it. // CreateInstance creates a new instance with the given options and returns it.
// The instance is initially in a "stopped" state. // The instance is initially in a "stopped" state.
func (im *instanceManager) CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) { func (im *instanceManager) CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Instance, error) {
if options == nil { if options == nil {
return nil, fmt.Errorf("instance options cannot be nil") return nil, fmt.Errorf("instance options cannot be nil")
} }
@@ -164,7 +164,7 @@ func (im *instanceManager) CreateInstance(name string, options *instance.CreateI
// GetInstance retrieves an instance by its name. // GetInstance retrieves an instance by its name.
// For remote instances, this fetches the live state from the remote node and updates the local stub. // For remote instances, this fetches the live state from the remote node and updates the local stub.
func (im *instanceManager) GetInstance(name string) (*instance.Process, error) { func (im *instanceManager) GetInstance(name string) (*instance.Instance, error) {
im.mu.RLock() im.mu.RLock()
inst, exists := im.instances[name] inst, exists := im.instances[name]
im.mu.RUnlock() im.mu.RUnlock()
@@ -194,7 +194,7 @@ func (im *instanceManager) GetInstance(name string) (*instance.Process, error) {
// UpdateInstance updates the options of an existing instance and returns it. // UpdateInstance updates the options of an existing instance and returns it.
// If the instance is running, it will be restarted to apply the new options. // If the instance is running, it will be restarted to apply the new options.
func (im *instanceManager) UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) { func (im *instanceManager) UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Instance, error) {
im.mu.RLock() im.mu.RLock()
inst, exists := im.instances[name] inst, exists := im.instances[name]
im.mu.RUnlock() im.mu.RUnlock()
@@ -326,7 +326,7 @@ func (im *instanceManager) DeleteInstance(name string) error {
// StartInstance starts a stopped instance and returns it. // StartInstance starts a stopped instance and returns it.
// If the instance is already running, it returns an error. // If the instance is already running, it returns an error.
func (im *instanceManager) StartInstance(name string) (*instance.Process, error) { func (im *instanceManager) StartInstance(name string) (*instance.Instance, error) {
im.mu.RLock() im.mu.RLock()
inst, exists := im.instances[name] inst, exists := im.instances[name]
im.mu.RUnlock() im.mu.RUnlock()
@@ -395,7 +395,7 @@ func (im *instanceManager) IsMaxRunningInstancesReached() bool {
} }
// StopInstance stops a running instance and returns it. // StopInstance stops a running instance and returns it.
func (im *instanceManager) StopInstance(name string) (*instance.Process, error) { func (im *instanceManager) StopInstance(name string) (*instance.Instance, error) {
im.mu.RLock() im.mu.RLock()
inst, exists := im.instances[name] inst, exists := im.instances[name]
im.mu.RUnlock() im.mu.RUnlock()
@@ -438,7 +438,7 @@ func (im *instanceManager) StopInstance(name string) (*instance.Process, error)
} }
// RestartInstance stops and then starts an instance, returning the updated instance. // RestartInstance stops and then starts an instance, returning the updated instance.
func (im *instanceManager) RestartInstance(name string) (*instance.Process, error) { func (im *instanceManager) RestartInstance(name string) (*instance.Instance, error) {
im.mu.RLock() im.mu.RLock()
inst, exists := im.instances[name] inst, exists := im.instances[name]
im.mu.RUnlock() im.mu.RUnlock()

View File

@@ -87,13 +87,13 @@ func parseRemoteResponse(resp *http.Response, result any) error {
} }
// ListRemoteInstances lists all instances on the remote node // ListRemoteInstances lists all instances on the remote node
func (im *instanceManager) ListRemoteInstances(nodeConfig *config.NodeConfig) ([]*instance.Process, error) { func (im *instanceManager) ListRemoteInstances(nodeConfig *config.NodeConfig) ([]*instance.Instance, error) {
resp, err := im.makeRemoteRequest(nodeConfig, "GET", "/api/v1/instances/", nil) resp, err := im.makeRemoteRequest(nodeConfig, "GET", "/api/v1/instances/", nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var instances []*instance.Process var instances []*instance.Instance
if err := parseRemoteResponse(resp, &instances); err != nil { if err := parseRemoteResponse(resp, &instances); err != nil {
return nil, err return nil, err
} }
@@ -102,7 +102,7 @@ func (im *instanceManager) ListRemoteInstances(nodeConfig *config.NodeConfig) ([
} }
// CreateRemoteInstance creates a new instance on the remote node // CreateRemoteInstance creates a new instance on the remote node
func (im *instanceManager) CreateRemoteInstance(nodeConfig *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Process, error) { func (im *instanceManager) CreateRemoteInstance(nodeConfig *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Instance, error) {
path := fmt.Sprintf("/api/v1/instances/%s/", name) path := fmt.Sprintf("/api/v1/instances/%s/", name)
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, options) resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, options)
@@ -110,7 +110,7 @@ func (im *instanceManager) CreateRemoteInstance(nodeConfig *config.NodeConfig, n
return nil, err return nil, err
} }
var inst instance.Process var inst instance.Instance
if err := parseRemoteResponse(resp, &inst); err != nil { if err := parseRemoteResponse(resp, &inst); err != nil {
return nil, err return nil, err
} }
@@ -119,14 +119,14 @@ func (im *instanceManager) CreateRemoteInstance(nodeConfig *config.NodeConfig, n
} }
// GetRemoteInstance retrieves an instance by name from the remote node // GetRemoteInstance retrieves an instance by name from the remote node
func (im *instanceManager) GetRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Process, error) { func (im *instanceManager) GetRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Instance, error) {
path := fmt.Sprintf("/api/v1/instances/%s/", name) path := fmt.Sprintf("/api/v1/instances/%s/", name)
resp, err := im.makeRemoteRequest(nodeConfig, "GET", path, nil) resp, err := im.makeRemoteRequest(nodeConfig, "GET", path, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var inst instance.Process var inst instance.Instance
if err := parseRemoteResponse(resp, &inst); err != nil { if err := parseRemoteResponse(resp, &inst); err != nil {
return nil, err return nil, err
} }
@@ -135,7 +135,7 @@ func (im *instanceManager) GetRemoteInstance(nodeConfig *config.NodeConfig, name
} }
// UpdateRemoteInstance updates an existing instance on the remote node // UpdateRemoteInstance updates an existing instance on the remote node
func (im *instanceManager) UpdateRemoteInstance(nodeConfig *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Process, error) { func (im *instanceManager) UpdateRemoteInstance(nodeConfig *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Instance, error) {
path := fmt.Sprintf("/api/v1/instances/%s/", name) path := fmt.Sprintf("/api/v1/instances/%s/", name)
resp, err := im.makeRemoteRequest(nodeConfig, "PUT", path, options) resp, err := im.makeRemoteRequest(nodeConfig, "PUT", path, options)
@@ -143,7 +143,7 @@ func (im *instanceManager) UpdateRemoteInstance(nodeConfig *config.NodeConfig, n
return nil, err return nil, err
} }
var inst instance.Process var inst instance.Instance
if err := parseRemoteResponse(resp, &inst); err != nil { if err := parseRemoteResponse(resp, &inst); err != nil {
return nil, err return nil, err
} }
@@ -163,14 +163,14 @@ func (im *instanceManager) DeleteRemoteInstance(nodeConfig *config.NodeConfig, n
} }
// StartRemoteInstance starts an instance on the remote node // StartRemoteInstance starts an instance on the remote node
func (im *instanceManager) StartRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Process, error) { func (im *instanceManager) StartRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Instance, error) {
path := fmt.Sprintf("/api/v1/instances/%s/start", name) path := fmt.Sprintf("/api/v1/instances/%s/start", name)
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil) resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var inst instance.Process var inst instance.Instance
if err := parseRemoteResponse(resp, &inst); err != nil { if err := parseRemoteResponse(resp, &inst); err != nil {
return nil, err return nil, err
} }
@@ -179,14 +179,14 @@ func (im *instanceManager) StartRemoteInstance(nodeConfig *config.NodeConfig, na
} }
// StopRemoteInstance stops an instance on the remote node // StopRemoteInstance stops an instance on the remote node
func (im *instanceManager) StopRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Process, error) { func (im *instanceManager) StopRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Instance, error) {
path := fmt.Sprintf("/api/v1/instances/%s/stop", name) path := fmt.Sprintf("/api/v1/instances/%s/stop", name)
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil) resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var inst instance.Process var inst instance.Instance
if err := parseRemoteResponse(resp, &inst); err != nil { if err := parseRemoteResponse(resp, &inst); err != nil {
return nil, err return nil, err
} }
@@ -195,14 +195,14 @@ func (im *instanceManager) StopRemoteInstance(nodeConfig *config.NodeConfig, nam
} }
// RestartRemoteInstance restarts an instance on the remote node // RestartRemoteInstance restarts an instance on the remote node
func (im *instanceManager) RestartRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Process, error) { func (im *instanceManager) RestartRemoteInstance(nodeConfig *config.NodeConfig, name string) (*instance.Instance, error) {
path := fmt.Sprintf("/api/v1/instances/%s/restart", name) path := fmt.Sprintf("/api/v1/instances/%s/restart", name)
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil) resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var inst instance.Process var inst instance.Instance
if err := parseRemoteResponse(resp, &inst); err != nil { if err := parseRemoteResponse(resp, &inst); err != nil {
return nil, err return nil, err
} }

View File

@@ -37,7 +37,7 @@ func (im *instanceManager) checkAllTimeouts() {
// EvictLRUInstance finds and stops the least recently used running instance. // EvictLRUInstance finds and stops the least recently used running instance.
func (im *instanceManager) EvictLRUInstance() error { func (im *instanceManager) EvictLRUInstance() error {
im.mu.RLock() im.mu.RLock()
var lruInstance *instance.Process var lruInstance *instance.Instance
for name := range im.runningInstances { for name := range im.runningInstances {
inst := im.instances[name] inst := im.instances[name]

View File

@@ -196,7 +196,7 @@ func TestEvictLRUInstance_Success(t *testing.T) {
func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) { func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) {
// Helper function to create instances with different timeout configurations // Helper function to create instances with different timeout configurations
createInstanceWithTimeout := func(manager manager.InstanceManager, name, model string, timeout *int) *instance.Process { createInstanceWithTimeout := func(manager manager.InstanceManager, name, model string, timeout *int) *instance.Instance {
options := &instance.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
BackendType: backends.BackendTypeLlamaCpp, BackendType: backends.BackendTypeLlamaCpp,
LlamaServerOptions: &llamacpp.LlamaServerOptions{ LlamaServerOptions: &llamacpp.LlamaServerOptions{
@@ -236,7 +236,7 @@ func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) {
inst3 := createInstanceWithTimeout(manager, "no-timeout-3", "/path/to/model3.gguf", nil) inst3 := createInstanceWithTimeout(manager, "no-timeout-3", "/path/to/model3.gguf", nil)
// Set instances to running // Set instances to running
instances := []*instance.Process{inst1, inst2, inst3} instances := []*instance.Instance{inst1, inst2, inst3}
for _, inst := range instances { for _, inst := range instances {
inst.SetStatus(instance.Running) inst.SetStatus(instance.Running)
} }
@@ -276,7 +276,7 @@ func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) {
instNoTimeout2 := createInstanceWithTimeout(manager, "no-timeout-2", "/path/to/model-no-timeout2.gguf", nil) instNoTimeout2 := createInstanceWithTimeout(manager, "no-timeout-2", "/path/to/model-no-timeout2.gguf", nil)
// Set all instances to running // Set all instances to running
instances := []*instance.Process{instWithTimeout, instNoTimeout1, instNoTimeout2} instances := []*instance.Instance{instWithTimeout, instNoTimeout1, instNoTimeout2}
for _, inst := range instances { for _, inst := range instances {
inst.SetStatus(instance.Running) inst.SetStatus(instance.Running)
inst.UpdateLastRequestTime() inst.UpdateLastRequestTime()

View File

@@ -391,7 +391,7 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
} }
// RemoteInstanceProxy proxies requests to a remote instance // RemoteInstanceProxy proxies requests to a remote instance
func (h *Handler) RemoteInstanceProxy(w http.ResponseWriter, r *http.Request, name string, inst *instance.Process) { func (h *Handler) RemoteInstanceProxy(w http.ResponseWriter, r *http.Request, name string, inst *instance.Instance) {
// Get the node name from instance options // Get the node name from instance options
options := inst.GetOptions() options := inst.GetOptions()
if options == nil || len(options.Nodes) == 0 { if options == nil || len(options.Nodes) == 0 {

View File

@@ -152,7 +152,7 @@ func (h *Handler) OpenAIProxy() http.HandlerFunc {
} }
// RemoteOpenAIProxy proxies OpenAI-compatible requests to a remote instance // RemoteOpenAIProxy proxies OpenAI-compatible requests to a remote instance
func (h *Handler) RemoteOpenAIProxy(w http.ResponseWriter, r *http.Request, modelName string, inst *instance.Process) { func (h *Handler) RemoteOpenAIProxy(w http.ResponseWriter, r *http.Request, modelName string, inst *instance.Instance) {
// Get the node name from instance options // Get the node name from instance options
options := inst.GetOptions() options := inst.GetOptions()
if options == nil || len(options.Nodes) == 0 { if options == nil || len(options.Nodes) == 0 {