mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 00:54:23 +00:00
Rename Process to Instance
This commit is contained in:
@@ -14,8 +14,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Process represents a running instance of the llama server
|
||||
type Process struct {
|
||||
// Instance represents a running instance of the llama server
|
||||
type Instance struct {
|
||||
Name string `json:"name"`
|
||||
options *CreateInstanceOptions `json:"-"`
|
||||
globalInstanceSettings *config.InstancesConfig
|
||||
@@ -29,10 +29,10 @@ type Process struct {
|
||||
Created int64 `json:"created,omitempty"` // Unix timestamp when the instance was created
|
||||
|
||||
// Logging file
|
||||
logger *Logger `json:"-"`
|
||||
logger *logger `json:"-"`
|
||||
|
||||
// Proxy component
|
||||
proxy *Proxy `json:"-"` // HTTP proxy and request tracking
|
||||
proxy *proxy `json:"-"` // HTTP proxy and request tracking
|
||||
|
||||
// internal
|
||||
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
|
||||
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
|
||||
options.ValidateAndApplyDefaults(name, globalInstanceSettings)
|
||||
|
||||
// Create the instance logger
|
||||
logger := NewInstanceLogger(name, globalInstanceSettings.LogsDir)
|
||||
logger := NewLogger(name, globalInstanceSettings.LogsDir)
|
||||
|
||||
instance := &Process{
|
||||
instance := &Instance{
|
||||
Name: name,
|
||||
options: options,
|
||||
globalInstanceSettings: globalInstanceSettings,
|
||||
@@ -73,13 +73,13 @@ func NewInstance(name string, globalBackendSettings *config.BackendConfig, globa
|
||||
return instance
|
||||
}
|
||||
|
||||
func (i *Process) GetOptions() *CreateInstanceOptions {
|
||||
func (i *Instance) GetOptions() *CreateInstanceOptions {
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
return i.options
|
||||
}
|
||||
|
||||
func (i *Process) GetPort() int {
|
||||
func (i *Instance) GetPort() int {
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
if i.options != nil {
|
||||
@@ -101,7 +101,7 @@ func (i *Process) GetPort() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (i *Process) GetHost() string {
|
||||
func (i *Instance) GetHost() string {
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
if i.options != nil {
|
||||
@@ -123,7 +123,7 @@ func (i *Process) GetHost() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (i *Process) SetOptions(options *CreateInstanceOptions) {
|
||||
func (i *Instance) SetOptions(options *CreateInstanceOptions) {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
|
||||
@@ -145,14 +145,14 @@ func (i *Process) SetOptions(options *CreateInstanceOptions) {
|
||||
|
||||
// SetTimeProvider sets a custom time provider for testing
|
||||
// Delegates to the Proxy component
|
||||
func (i *Process) SetTimeProvider(tp TimeProvider) {
|
||||
func (i *Instance) SetTimeProvider(tp TimeProvider) {
|
||||
if i.proxy != nil {
|
||||
i.proxy.SetTimeProvider(tp)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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
|
||||
func (i *Process) MarshalJSON() ([]byte, error) {
|
||||
func (i *Instance) MarshalJSON() ([]byte, error) {
|
||||
// Use read lock since we're only reading data
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
@@ -183,7 +183,7 @@ func (i *Process) MarshalJSON() ([]byte, error) {
|
||||
}
|
||||
|
||||
// Use anonymous struct to avoid recursion
|
||||
type Alias Process
|
||||
type Alias Instance
|
||||
return json.Marshal(&struct {
|
||||
*Alias
|
||||
Options *CreateInstanceOptions `json:"options,omitempty"`
|
||||
@@ -196,9 +196,9 @@ func (i *Process) MarshalJSON() ([]byte, error) {
|
||||
}
|
||||
|
||||
// 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
|
||||
type Alias Process
|
||||
type Alias Instance
|
||||
aux := &struct {
|
||||
*Alias
|
||||
Options *CreateInstanceOptions `json:"options,omitempty"`
|
||||
@@ -218,7 +218,7 @@ func (i *Process) UnmarshalJSON(data []byte) error {
|
||||
|
||||
// Initialize fields that are not serialized
|
||||
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 {
|
||||
i.proxy = NewProxy(i)
|
||||
@@ -227,7 +227,7 @@ func (i *Process) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Process) IsRemote() bool {
|
||||
func (i *Instance) IsRemote() bool {
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
|
||||
@@ -238,13 +238,13 @@ func (i *Process) IsRemote() bool {
|
||||
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)
|
||||
}
|
||||
|
||||
// getBackendHostPort extracts the host and port from instance options
|
||||
// Returns the configured host and port for the backend
|
||||
func (i *Process) getBackendHostPort() (string, int) {
|
||||
func (i *Instance) getBackendHostPort() (string, int) {
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
|
||||
|
||||
@@ -345,7 +345,7 @@ func TestUnmarshalJSON(t *testing.T) {
|
||||
}
|
||||
}`
|
||||
|
||||
var inst instance.Process
|
||||
var inst instance.Instance
|
||||
err := json.Unmarshal([]byte(jsonData), &inst)
|
||||
if err != nil {
|
||||
t.Fatalf("JSON unmarshal failed: %v", err)
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
// 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()
|
||||
defer i.mu.Unlock()
|
||||
|
||||
@@ -90,7 +90,7 @@ func (i *Process) Start() error {
|
||||
}
|
||||
|
||||
// Stop terminates the subprocess
|
||||
func (i *Process) Stop() error {
|
||||
func (i *Instance) Stop() error {
|
||||
i.mu.Lock()
|
||||
|
||||
if !i.IsRunning() {
|
||||
@@ -160,14 +160,14 @@ func (i *Process) Stop() error {
|
||||
|
||||
// LastRequestTime returns the last request time as a Unix timestamp
|
||||
// Delegates to the Proxy component
|
||||
func (i *Process) LastRequestTime() int64 {
|
||||
func (i *Instance) LastRequestTime() int64 {
|
||||
if i.proxy == nil {
|
||||
return 0
|
||||
}
|
||||
return i.proxy.LastRequestTime()
|
||||
}
|
||||
|
||||
func (i *Process) WaitForHealthy(timeout int) error {
|
||||
func (i *Instance) WaitForHealthy(timeout int) error {
|
||||
if !i.IsRunning() {
|
||||
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() {
|
||||
i.mu.Lock()
|
||||
if i.monitorDone != nil {
|
||||
@@ -267,7 +267,7 @@ func (i *Process) monitorProcess() {
|
||||
}
|
||||
|
||||
// handleRestart manages the restart process while holding the lock
|
||||
func (i *Process) handleRestart() {
|
||||
func (i *Instance) handleRestart() {
|
||||
// Validate restart conditions and get safe parameters
|
||||
shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions()
|
||||
if !shouldRestart {
|
||||
@@ -310,7 +310,7 @@ func (i *Process) handleRestart() {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
log.Printf("Instance %s not restarting: options are nil", i.Name)
|
||||
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
|
||||
func (i *Process) buildCommand() (*exec.Cmd, error) {
|
||||
func (i *Instance) buildCommand() (*exec.Cmd, error) {
|
||||
// Get backend configuration
|
||||
backendConfig, err := i.getBackendConfig()
|
||||
if err != nil {
|
||||
@@ -375,7 +375,7 @@ func (i *Process) buildCommand() (*exec.Cmd, error) {
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
switch i.options.BackendType {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
type logger struct {
|
||||
name string
|
||||
logDir string
|
||||
logFile *os.File
|
||||
@@ -18,15 +18,15 @@ type Logger struct {
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewInstanceLogger(name string, logDir string) *Logger {
|
||||
return &Logger{
|
||||
func NewLogger(name string, logDir string) *logger {
|
||||
return &logger{
|
||||
name: name,
|
||||
logDir: logDir,
|
||||
}
|
||||
}
|
||||
|
||||
// Create creates and opens the log files for stdout and stderr
|
||||
func (i *Logger) Create() error {
|
||||
func (i *logger) Create() error {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
|
||||
@@ -57,7 +57,7 @@ func (i *Logger) Create() error {
|
||||
}
|
||||
|
||||
// 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()
|
||||
defer i.mu.RUnlock()
|
||||
|
||||
@@ -98,7 +98,7 @@ func (i *Logger) GetLogs(num_lines int) (string, error) {
|
||||
}
|
||||
|
||||
// closeLogFile closes the log files
|
||||
func (i *Logger) Close() {
|
||||
func (i *logger) Close() {
|
||||
i.mu.Lock()
|
||||
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
|
||||
func (i *Logger) readOutput(reader io.ReadCloser) {
|
||||
func (i *logger) readOutput(reader io.ReadCloser) {
|
||||
defer reader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
|
||||
@@ -23,9 +23,9 @@ func (realTimeProvider) Now() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
// Proxy manages HTTP reverse proxy and request tracking for an instance.
|
||||
type Proxy struct {
|
||||
process *Process // Owner reference - Proxy is owned by Process
|
||||
// proxy manages HTTP reverse proxy and request tracking for an instance.
|
||||
type proxy struct {
|
||||
process *Instance // Owner reference - Proxy is owned by Process
|
||||
|
||||
mu sync.RWMutex
|
||||
proxy *httputil.ReverseProxy
|
||||
@@ -36,8 +36,8 @@ type Proxy struct {
|
||||
}
|
||||
|
||||
// NewProxy creates a new Proxy for the given process
|
||||
func NewProxy(process *Process) *Proxy {
|
||||
return &Proxy{
|
||||
func NewProxy(process *Instance) *proxy {
|
||||
return &proxy{
|
||||
process: process,
|
||||
timeProvider: realTimeProvider{},
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func NewProxy(process *Process) *Proxy {
|
||||
|
||||
// GetProxy returns the reverse proxy for this instance, creating it if needed.
|
||||
// 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
|
||||
// Other callers block until first initialization completes
|
||||
p.proxyOnce.Do(func() {
|
||||
@@ -56,7 +56,7 @@ func (p *Proxy) GetProxy() (*httputil.ReverseProxy, error) {
|
||||
}
|
||||
|
||||
// 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()
|
||||
if options == nil {
|
||||
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.
|
||||
// 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()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
@@ -119,18 +119,18 @@ func (p *Proxy) clearProxy() {
|
||||
}
|
||||
|
||||
// UpdateLastRequestTime updates the last request access time for the instance
|
||||
func (p *Proxy) UpdateLastRequestTime() {
|
||||
func (p *proxy) UpdateLastRequestTime() {
|
||||
lastRequestTime := p.timeProvider.Now().Unix()
|
||||
p.lastRequestTime.Store(lastRequestTime)
|
||||
}
|
||||
|
||||
// LastRequestTime returns the last request time as a Unix timestamp
|
||||
func (p *Proxy) LastRequestTime() int64 {
|
||||
func (p *proxy) LastRequestTime() int64 {
|
||||
return p.lastRequestTime.Load()
|
||||
}
|
||||
|
||||
// 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() {
|
||||
return false
|
||||
}
|
||||
@@ -151,6 +151,6 @@ func (p *Proxy) ShouldTimeout() bool {
|
||||
}
|
||||
|
||||
// SetTimeProvider sets a custom time provider for testing
|
||||
func (p *Proxy) SetTimeProvider(tp TimeProvider) {
|
||||
func (p *proxy) SetTimeProvider(tp TimeProvider) {
|
||||
p.timeProvider = tp
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ var statusToName = map[InstanceStatus]string{
|
||||
Failed: "failed",
|
||||
}
|
||||
|
||||
func (p *Process) SetStatus(status InstanceStatus) {
|
||||
func (p *Instance) SetStatus(status InstanceStatus) {
|
||||
oldStatus := p.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
|
||||
}
|
||||
|
||||
// IsRunning returns true if the status is Running
|
||||
func (p *Process) IsRunning() bool {
|
||||
func (p *Instance) IsRunning() bool {
|
||||
return p.Status == Running
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package instance
|
||||
|
||||
// UpdateLastRequestTime updates the last request access time for the instance via proxy
|
||||
// Delegates to the Proxy component
|
||||
func (i *Process) UpdateLastRequestTime() {
|
||||
func (i *Instance) UpdateLastRequestTime() {
|
||||
if i.proxy != nil {
|
||||
i.proxy.UpdateLastRequestTime()
|
||||
}
|
||||
@@ -10,7 +10,7 @@ func (i *Process) UpdateLastRequestTime() {
|
||||
|
||||
// ShouldTimeout checks if the instance should timeout based on idle time
|
||||
// Delegates to the Proxy component
|
||||
func (i *Process) ShouldTimeout() bool {
|
||||
func (i *Instance) ShouldTimeout() bool {
|
||||
if i.proxy == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -16,35 +16,35 @@ import (
|
||||
|
||||
// InstanceManager defines the interface for managing instances of the llama server.
|
||||
type InstanceManager interface {
|
||||
ListInstances() ([]*instance.Process, error)
|
||||
CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
|
||||
GetInstance(name string) (*instance.Process, error)
|
||||
UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
|
||||
ListInstances() ([]*instance.Instance, error)
|
||||
CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
|
||||
GetInstance(name string) (*instance.Instance, error)
|
||||
UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
|
||||
DeleteInstance(name string) error
|
||||
StartInstance(name string) (*instance.Process, error)
|
||||
StartInstance(name string) (*instance.Instance, error)
|
||||
IsMaxRunningInstancesReached() bool
|
||||
StopInstance(name string) (*instance.Process, error)
|
||||
StopInstance(name string) (*instance.Instance, error)
|
||||
EvictLRUInstance() error
|
||||
RestartInstance(name string) (*instance.Process, error)
|
||||
RestartInstance(name string) (*instance.Instance, error)
|
||||
GetInstanceLogs(name string, numLines int) (string, error)
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
type RemoteManager interface {
|
||||
ListRemoteInstances(node *config.NodeConfig) ([]*instance.Process, error)
|
||||
CreateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
|
||||
GetRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error)
|
||||
UpdateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
|
||||
ListRemoteInstances(node *config.NodeConfig) ([]*instance.Instance, error)
|
||||
CreateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
|
||||
GetRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
|
||||
UpdateRemoteInstance(node *config.NodeConfig, name string, options *instance.CreateInstanceOptions) (*instance.Instance, error)
|
||||
DeleteRemoteInstance(node *config.NodeConfig, name string) error
|
||||
StartRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error)
|
||||
StopRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error)
|
||||
RestartRemoteInstance(node *config.NodeConfig, name string) (*instance.Process, error)
|
||||
StartRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
|
||||
StopRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
|
||||
RestartRemoteInstance(node *config.NodeConfig, name string) (*instance.Instance, error)
|
||||
GetRemoteInstanceLogs(node *config.NodeConfig, name string, numLines int) (string, error)
|
||||
}
|
||||
|
||||
type instanceManager struct {
|
||||
mu sync.RWMutex
|
||||
instances map[string]*instance.Process
|
||||
instances map[string]*instance.Instance
|
||||
runningInstances map[string]struct{}
|
||||
ports map[int]bool
|
||||
instancesConfig config.InstancesConfig
|
||||
@@ -76,7 +76,7 @@ func NewInstanceManager(backendsConfig config.BackendConfig, instancesConfig con
|
||||
}
|
||||
|
||||
im := &instanceManager{
|
||||
instances: make(map[string]*instance.Process),
|
||||
instances: make(map[string]*instance.Instance),
|
||||
runningInstances: make(map[string]struct{}),
|
||||
ports: make(map[int]bool),
|
||||
instancesConfig: instancesConfig,
|
||||
@@ -130,7 +130,7 @@ func (im *instanceManager) getNextAvailablePort() (int, error) {
|
||||
}
|
||||
|
||||
// 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 == "" {
|
||||
return nil // Persistence disabled
|
||||
}
|
||||
@@ -172,7 +172,7 @@ func (im *instanceManager) Shutdown() {
|
||||
close(im.shutdownChan)
|
||||
|
||||
// Create a list of running instances to stop
|
||||
var runningInstances []*instance.Process
|
||||
var runningInstances []*instance.Instance
|
||||
var runningNames []string
|
||||
for name, inst := range im.instances {
|
||||
if inst.IsRunning() {
|
||||
@@ -197,7 +197,7 @@ func (im *instanceManager) Shutdown() {
|
||||
wg.Add(len(runningInstances))
|
||||
|
||||
for i, inst := range runningInstances {
|
||||
go func(name string, inst *instance.Process) {
|
||||
go func(name string, inst *instance.Instance) {
|
||||
defer wg.Done()
|
||||
fmt.Printf("Stopping instance %s...\n", name)
|
||||
// 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)
|
||||
}
|
||||
|
||||
var persistedInstance instance.Process
|
||||
var persistedInstance instance.Instance
|
||||
if err := json.Unmarshal(data, &persistedInstance); err != nil {
|
||||
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
|
||||
func (im *instanceManager) autoStartInstances() {
|
||||
im.mu.RLock()
|
||||
var instancesToStart []*instance.Process
|
||||
var instancesToStop []*instance.Process
|
||||
var instancesToStart []*instance.Instance
|
||||
var instancesToStop []*instance.Instance
|
||||
for _, inst := range im.instances {
|
||||
if inst.IsRunning() && // Was running when persisted
|
||||
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
|
||||
// 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() {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type MaxRunningInstancesError error
|
||||
|
||||
// updateLocalInstanceFromRemote updates the local stub instance with data from the remote instance
|
||||
// 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 {
|
||||
return
|
||||
}
|
||||
@@ -45,9 +45,9 @@ func (im *instanceManager) updateLocalInstanceFromRemote(localInst *instance.Pro
|
||||
|
||||
// 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.
|
||||
func (im *instanceManager) ListInstances() ([]*instance.Process, error) {
|
||||
func (im *instanceManager) ListInstances() ([]*instance.Instance, error) {
|
||||
im.mu.RLock()
|
||||
localInstances := make([]*instance.Process, 0, len(im.instances))
|
||||
localInstances := make([]*instance.Instance, 0, len(im.instances))
|
||||
for _, inst := range im.instances {
|
||||
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.
|
||||
// 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 {
|
||||
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.
|
||||
// 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()
|
||||
inst, exists := im.instances[name]
|
||||
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.
|
||||
// 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()
|
||||
inst, exists := im.instances[name]
|
||||
im.mu.RUnlock()
|
||||
@@ -326,7 +326,7 @@ func (im *instanceManager) DeleteInstance(name string) error {
|
||||
|
||||
// StartInstance starts a stopped instance and returns it.
|
||||
// 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()
|
||||
inst, exists := im.instances[name]
|
||||
im.mu.RUnlock()
|
||||
@@ -395,7 +395,7 @@ func (im *instanceManager) IsMaxRunningInstancesReached() bool {
|
||||
}
|
||||
|
||||
// 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()
|
||||
inst, exists := im.instances[name]
|
||||
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.
|
||||
func (im *instanceManager) RestartInstance(name string) (*instance.Process, error) {
|
||||
func (im *instanceManager) RestartInstance(name string) (*instance.Instance, error) {
|
||||
im.mu.RLock()
|
||||
inst, exists := im.instances[name]
|
||||
im.mu.RUnlock()
|
||||
|
||||
@@ -87,13 +87,13 @@ func parseRemoteResponse(resp *http.Response, result any) error {
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var instances []*instance.Process
|
||||
var instances []*instance.Instance
|
||||
if err := parseRemoteResponse(resp, &instances); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -102,7 +102,7 @@ func (im *instanceManager) ListRemoteInstances(nodeConfig *config.NodeConfig) ([
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, options)
|
||||
@@ -110,7 +110,7 @@ func (im *instanceManager) CreateRemoteInstance(nodeConfig *config.NodeConfig, n
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var inst instance.Process
|
||||
var inst instance.Instance
|
||||
if err := parseRemoteResponse(resp, &inst); err != nil {
|
||||
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
|
||||
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)
|
||||
resp, err := im.makeRemoteRequest(nodeConfig, "GET", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var inst instance.Process
|
||||
var inst instance.Instance
|
||||
if err := parseRemoteResponse(resp, &inst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -135,7 +135,7 @@ func (im *instanceManager) GetRemoteInstance(nodeConfig *config.NodeConfig, name
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
resp, err := im.makeRemoteRequest(nodeConfig, "PUT", path, options)
|
||||
@@ -143,7 +143,7 @@ func (im *instanceManager) UpdateRemoteInstance(nodeConfig *config.NodeConfig, n
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var inst instance.Process
|
||||
var inst instance.Instance
|
||||
if err := parseRemoteResponse(resp, &inst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -163,14 +163,14 @@ func (im *instanceManager) DeleteRemoteInstance(nodeConfig *config.NodeConfig, n
|
||||
}
|
||||
|
||||
// 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)
|
||||
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var inst instance.Process
|
||||
var inst instance.Instance
|
||||
if err := parseRemoteResponse(resp, &inst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -179,14 +179,14 @@ func (im *instanceManager) StartRemoteInstance(nodeConfig *config.NodeConfig, na
|
||||
}
|
||||
|
||||
// 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)
|
||||
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var inst instance.Process
|
||||
var inst instance.Instance
|
||||
if err := parseRemoteResponse(resp, &inst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -195,14 +195,14 @@ func (im *instanceManager) StopRemoteInstance(nodeConfig *config.NodeConfig, nam
|
||||
}
|
||||
|
||||
// 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)
|
||||
resp, err := im.makeRemoteRequest(nodeConfig, "POST", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var inst instance.Process
|
||||
var inst instance.Instance
|
||||
if err := parseRemoteResponse(resp, &inst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func (im *instanceManager) checkAllTimeouts() {
|
||||
// EvictLRUInstance finds and stops the least recently used running instance.
|
||||
func (im *instanceManager) EvictLRUInstance() error {
|
||||
im.mu.RLock()
|
||||
var lruInstance *instance.Process
|
||||
var lruInstance *instance.Instance
|
||||
|
||||
for name := range im.runningInstances {
|
||||
inst := im.instances[name]
|
||||
|
||||
@@ -196,7 +196,7 @@ func TestEvictLRUInstance_Success(t *testing.T) {
|
||||
|
||||
func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) {
|
||||
// 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{
|
||||
BackendType: backends.BackendTypeLlamaCpp,
|
||||
LlamaServerOptions: &llamacpp.LlamaServerOptions{
|
||||
@@ -236,7 +236,7 @@ func TestEvictLRUInstance_NoEligibleInstances(t *testing.T) {
|
||||
inst3 := createInstanceWithTimeout(manager, "no-timeout-3", "/path/to/model3.gguf", nil)
|
||||
|
||||
// Set instances to running
|
||||
instances := []*instance.Process{inst1, inst2, inst3}
|
||||
instances := []*instance.Instance{inst1, inst2, inst3}
|
||||
for _, inst := range instances {
|
||||
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)
|
||||
|
||||
// Set all instances to running
|
||||
instances := []*instance.Process{instWithTimeout, instNoTimeout1, instNoTimeout2}
|
||||
instances := []*instance.Instance{instWithTimeout, instNoTimeout1, instNoTimeout2}
|
||||
for _, inst := range instances {
|
||||
inst.SetStatus(instance.Running)
|
||||
inst.UpdateLastRequestTime()
|
||||
|
||||
@@ -391,7 +391,7 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
|
||||
}
|
||||
|
||||
// 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
|
||||
options := inst.GetOptions()
|
||||
if options == nil || len(options.Nodes) == 0 {
|
||||
|
||||
@@ -152,7 +152,7 @@ func (h *Handler) OpenAIProxy() http.HandlerFunc {
|
||||
}
|
||||
|
||||
// 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
|
||||
options := inst.GetOptions()
|
||||
if options == nil || len(options.Nodes) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user