mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 09:04:27 +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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user