From 2abe9c282e72de1422796007510b734e7742fcb0 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 4 Aug 2025 19:30:50 +0200 Subject: [PATCH] Rename config and instance struct to avoid awkward naming --- pkg/config/config.go | 12 +++++----- pkg/config/config_test.go | 10 ++++----- pkg/instance/instance.go | 18 +++++++-------- pkg/instance/instance_test.go | 6 ++--- pkg/instance/lifecycle.go | 10 ++++----- pkg/instance/logging.go | 2 +- pkg/manager/manager.go | 42 +++++++++++++++++------------------ pkg/manager/manager_test.go | 2 +- pkg/server/handlers.go | 4 ++-- 9 files changed, 53 insertions(+), 53 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 21eebfc..dac781c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -10,8 +10,8 @@ import ( "gopkg.in/yaml.v3" ) -// Config represents the configuration for llamactl -type Config struct { +// AppConfig represents the configuration for llamactl +type AppConfig struct { Server ServerConfig `yaml:"server"` Instances InstancesConfig `yaml:"instances"` Auth AuthConfig `yaml:"auth"` @@ -85,9 +85,9 @@ type AuthConfig struct { // 1. Hardcoded defaults // 2. Config file // 3. Environment variables -func LoadConfig(configPath string) (Config, error) { +func LoadConfig(configPath string) (AppConfig, error) { // 1. Start with defaults - cfg := Config{ + cfg := AppConfig{ Server: ServerConfig{ Host: "0.0.0.0", Port: 8080, @@ -126,7 +126,7 @@ func LoadConfig(configPath string) (Config, error) { } // loadConfigFile attempts to load config from file with fallback locations -func loadConfigFile(cfg *Config, configPath string) error { +func loadConfigFile(cfg *AppConfig, configPath string) error { var configLocations []string // If specific config path provided, use only that @@ -150,7 +150,7 @@ func loadConfigFile(cfg *Config, configPath string) error { } // loadEnvVars overrides config with environment variables -func loadEnvVars(cfg *Config) { +func loadEnvVars(cfg *AppConfig) { // Server config if host := os.Getenv("LLAMACTL_HOST"); host != "" { cfg.Server.Host = host diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 41f0413..2596dac 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -271,31 +271,31 @@ func TestLoadConfig_EnvironmentVariableTypes(t *testing.T) { testCases := []struct { envVar string envValue string - checkFn func(*config.Config) bool + checkFn func(*config.AppConfig) bool desc string }{ { envVar: "LLAMACTL_PORT", envValue: "invalid-port", - checkFn: func(c *config.Config) bool { return c.Server.Port == 8080 }, // Should keep default + checkFn: func(c *config.AppConfig) bool { return c.Server.Port == 8080 }, // Should keep default desc: "invalid port number should keep default", }, { envVar: "LLAMACTL_MAX_INSTANCES", envValue: "not-a-number", - checkFn: func(c *config.Config) bool { return c.Instances.MaxInstances == -1 }, // Should keep default + checkFn: func(c *config.AppConfig) bool { return c.Instances.MaxInstances == -1 }, // Should keep default desc: "invalid max instances should keep default", }, { envVar: "LLAMACTL_DEFAULT_AUTO_RESTART", envValue: "invalid-bool", - checkFn: func(c *config.Config) bool { return c.Instances.DefaultAutoRestart == true }, // Should keep default + checkFn: func(c *config.AppConfig) bool { return c.Instances.DefaultAutoRestart == true }, // Should keep default desc: "invalid boolean should keep default", }, { envVar: "LLAMACTL_INSTANCE_PORT_RANGE", envValue: "invalid-range", - checkFn: func(c *config.Config) bool { return c.Instances.PortRange == [2]int{8000, 9000} }, // Should keep default + checkFn: func(c *config.AppConfig) bool { return c.Instances.PortRange == [2]int{8000, 9000} }, // Should keep default desc: "invalid port range should keep default", }, } diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 02969d9..dd4a45f 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -55,8 +55,8 @@ func (c *CreateInstanceOptions) UnmarshalJSON(data []byte) error { return nil } -// Instance represents a running instance of the llama server -type Instance struct { +// Process represents a running instance of the llama server +type Process struct { Name string `json:"name"` options *CreateInstanceOptions `json:"-"` globalSettings *config.InstancesConfig @@ -145,7 +145,7 @@ func applyDefaultOptions(options *CreateInstanceOptions, globalSettings *config. } // NewInstance creates a new instance with the given name, log path, and options -func NewInstance(name string, globalSettings *config.InstancesConfig, options *CreateInstanceOptions) *Instance { +func NewInstance(name string, globalSettings *config.InstancesConfig, options *CreateInstanceOptions) *Process { // Validate and copy options optionsCopy := validateAndCopyOptions(name, options) // Apply defaults @@ -153,7 +153,7 @@ func NewInstance(name string, globalSettings *config.InstancesConfig, options *C // Create the instance logger logger := NewInstanceLogger(name, globalSettings.LogsDir) - return &Instance{ + return &Process{ Name: name, options: optionsCopy, globalSettings: globalSettings, @@ -165,13 +165,13 @@ func NewInstance(name string, globalSettings *config.InstancesConfig, options *C } } -func (i *Instance) GetOptions() *CreateInstanceOptions { +func (i *Process) GetOptions() *CreateInstanceOptions { i.mu.RLock() defer i.mu.RUnlock() return i.options } -func (i *Instance) SetOptions(options *CreateInstanceOptions) { +func (i *Process) SetOptions(options *CreateInstanceOptions) { i.mu.Lock() defer i.mu.Unlock() @@ -190,7 +190,7 @@ func (i *Instance) SetOptions(options *CreateInstanceOptions) { } // GetProxy returns the reverse proxy for this instance, creating it if needed -func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) { +func (i *Process) GetProxy() (*httputil.ReverseProxy, error) { i.mu.Lock() defer i.mu.Unlock() @@ -227,7 +227,7 @@ func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) { } // MarshalJSON implements json.Marshaler for Instance -func (i *Instance) MarshalJSON() ([]byte, error) { +func (i *Process) MarshalJSON() ([]byte, error) { // Use read lock since we're only reading data i.mu.RLock() defer i.mu.RUnlock() @@ -249,7 +249,7 @@ func (i *Instance) MarshalJSON() ([]byte, error) { } // UnmarshalJSON implements json.Unmarshaler for Instance -func (i *Instance) UnmarshalJSON(data []byte) error { +func (i *Process) UnmarshalJSON(data []byte) error { // Create a temporary struct for unmarshalling temp := struct { Name string `json:"name"` diff --git a/pkg/instance/instance_test.go b/pkg/instance/instance_test.go index dadcf89..d89bad3 100644 --- a/pkg/instance/instance_test.go +++ b/pkg/instance/instance_test.go @@ -286,7 +286,7 @@ func TestUnmarshalJSON(t *testing.T) { } }` - var inst instance.Instance + var inst instance.Process err := json.Unmarshal([]byte(jsonData), &inst) if err != nil { t.Fatalf("JSON unmarshal failed: %v", err) @@ -326,7 +326,7 @@ func TestUnmarshalJSON_PartialOptions(t *testing.T) { } }` - var inst instance.Instance + var inst instance.Process err := json.Unmarshal([]byte(jsonData), &inst) if err != nil { t.Fatalf("JSON unmarshal failed: %v", err) @@ -350,7 +350,7 @@ func TestUnmarshalJSON_NoOptions(t *testing.T) { "running": false }` - var inst instance.Instance + var inst instance.Process err := json.Unmarshal([]byte(jsonData), &inst) if err != nil { t.Fatalf("JSON unmarshal failed: %v", err) diff --git a/pkg/instance/lifecycle.go b/pkg/instance/lifecycle.go index 365a682..1442b6e 100644 --- a/pkg/instance/lifecycle.go +++ b/pkg/instance/lifecycle.go @@ -11,7 +11,7 @@ import ( ) // Start starts the llama server instance and returns an error if it fails. -func (i *Instance) Start() error { +func (i *Process) Start() error { i.mu.Lock() defer i.mu.Unlock() @@ -75,7 +75,7 @@ func (i *Instance) Start() error { } // Stop terminates the subprocess -func (i *Instance) Stop() error { +func (i *Process) Stop() error { i.mu.Lock() if !i.Running { @@ -140,7 +140,7 @@ func (i *Instance) Stop() error { return nil } -func (i *Instance) monitorProcess() { +func (i *Process) monitorProcess() { defer func() { i.mu.Lock() if i.monitorDone != nil { @@ -181,7 +181,7 @@ func (i *Instance) monitorProcess() { } // handleRestart manages the restart process while holding the lock -func (i *Instance) handleRestart() { +func (i *Process) handleRestart() { // Validate restart conditions and get safe parameters shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions() if !shouldRestart { @@ -223,7 +223,7 @@ func (i *Instance) handleRestart() { } // validateRestartConditions checks if the instance should be restarted and returns the parameters -func (i *Instance) validateRestartConditions() (shouldRestart bool, maxRestarts int, restartDelay int) { +func (i *Process) 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 diff --git a/pkg/instance/logging.go b/pkg/instance/logging.go index 4360e7b..5432556 100644 --- a/pkg/instance/logging.go +++ b/pkg/instance/logging.go @@ -52,7 +52,7 @@ func (i *InstanceLogger) Create() error { } // GetLogs retrieves the last n lines of logs from the instance -func (i *Instance) GetLogs(num_lines int) (string, error) { +func (i *Process) GetLogs(num_lines int) (string, error) { i.mu.RLock() logFileName := i.logger.logFilePath i.mu.RUnlock() diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 48469b7..bf7645f 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -15,21 +15,21 @@ import ( // InstanceManager defines the interface for managing instances of the llama server. type InstanceManager interface { - 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) + 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) DeleteInstance(name string) error - StartInstance(name string) (*instance.Instance, error) - StopInstance(name string) (*instance.Instance, error) - RestartInstance(name string) (*instance.Instance, error) + StartInstance(name string) (*instance.Process, error) + StopInstance(name string) (*instance.Process, error) + RestartInstance(name string) (*instance.Process, error) GetInstanceLogs(name string) (string, error) Shutdown() } type instanceManager struct { mu sync.RWMutex - instances map[string]*instance.Instance + instances map[string]*instance.Process ports map[int]bool instancesConfig config.InstancesConfig } @@ -37,7 +37,7 @@ type instanceManager struct { // NewInstanceManager creates a new instance of InstanceManager. func NewInstanceManager(instancesConfig config.InstancesConfig) InstanceManager { im := &instanceManager{ - instances: make(map[string]*instance.Instance), + instances: make(map[string]*instance.Process), ports: make(map[int]bool), instancesConfig: instancesConfig, } @@ -50,11 +50,11 @@ func NewInstanceManager(instancesConfig config.InstancesConfig) InstanceManager } // ListInstances returns a list of all instances managed by the instance manager. -func (im *instanceManager) ListInstances() ([]*instance.Instance, error) { +func (im *instanceManager) ListInstances() ([]*instance.Process, error) { im.mu.RLock() defer im.mu.RUnlock() - instances := make([]*instance.Instance, 0, len(im.instances)) + instances := make([]*instance.Process, 0, len(im.instances)) for _, inst := range im.instances { instances = append(instances, inst) } @@ -63,7 +63,7 @@ func (im *instanceManager) ListInstances() ([]*instance.Instance, 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.Instance, error) { +func (im *instanceManager) CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) { if options == nil { return nil, fmt.Errorf("instance options cannot be nil") } @@ -117,7 +117,7 @@ func (im *instanceManager) CreateInstance(name string, options *instance.CreateI } // GetInstance retrieves an instance by its name. -func (im *instanceManager) GetInstance(name string) (*instance.Instance, error) { +func (im *instanceManager) GetInstance(name string) (*instance.Process, error) { im.mu.RLock() defer im.mu.RUnlock() @@ -130,7 +130,7 @@ func (im *instanceManager) GetInstance(name string) (*instance.Instance, 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.Instance, error) { +func (im *instanceManager) UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) { im.mu.RLock() instance, exists := im.instances[name] im.mu.RUnlock() @@ -205,7 +205,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.Instance, error) { +func (im *instanceManager) StartInstance(name string) (*instance.Process, error) { im.mu.RLock() instance, exists := im.instances[name] im.mu.RUnlock() @@ -232,7 +232,7 @@ func (im *instanceManager) StartInstance(name string) (*instance.Instance, error } // StopInstance stops a running instance and returns it. -func (im *instanceManager) StopInstance(name string) (*instance.Instance, error) { +func (im *instanceManager) StopInstance(name string) (*instance.Process, error) { im.mu.RLock() instance, exists := im.instances[name] im.mu.RUnlock() @@ -259,7 +259,7 @@ func (im *instanceManager) StopInstance(name string) (*instance.Instance, error) } // RestartInstance stops and then starts an instance, returning the updated instance. -func (im *instanceManager) RestartInstance(name string) (*instance.Instance, error) { +func (im *instanceManager) RestartInstance(name string) (*instance.Process, error) { instance, err := im.StopInstance(name) if err != nil { return nil, err @@ -295,7 +295,7 @@ func (im *instanceManager) getNextAvailablePort() (int, error) { } // persistInstance saves an instance to its JSON file -func (im *instanceManager) persistInstance(instance *instance.Instance) error { +func (im *instanceManager) persistInstance(instance *instance.Process) error { if im.instancesConfig.InstancesDir == "" { return nil // Persistence disabled } @@ -336,7 +336,7 @@ func (im *instanceManager) Shutdown() { continue } - go func(name string, inst *instance.Instance) { + go func(name string, inst *instance.Process) { defer wg.Done() fmt.Printf("Stopping instance %s...\n", name) // Attempt to stop the instance gracefully @@ -400,7 +400,7 @@ func (im *instanceManager) loadInstance(name, path string) error { return fmt.Errorf("failed to read instance file: %w", err) } - var persistedInstance instance.Instance + var persistedInstance instance.Process if err := json.Unmarshal(data, &persistedInstance); err != nil { return fmt.Errorf("failed to unmarshal instance: %w", err) } @@ -433,7 +433,7 @@ func (im *instanceManager) loadInstance(name, path string) error { // autoStartInstances starts instances that were running when persisted and have auto-restart enabled func (im *instanceManager) autoStartInstances() { im.mu.RLock() - var instancesToStart []*instance.Instance + var instancesToStart []*instance.Process for _, inst := range im.instances { if inst.Running && // Was running when persisted inst.GetOptions() != nil && diff --git a/pkg/manager/manager_test.go b/pkg/manager/manager_test.go index f31e11b..c2f953c 100644 --- a/pkg/manager/manager_test.go +++ b/pkg/manager/manager_test.go @@ -687,7 +687,7 @@ func TestPersistence_InstancesLoadedFromDisk(t *testing.T) { } // Check instances by name - instancesByName := make(map[string]*instance.Instance) + instancesByName := make(map[string]*instance.Process) for _, inst := range instances { instancesByName[inst.Name] = inst } diff --git a/pkg/server/handlers.go b/pkg/server/handlers.go index e0cc4da..9eeb2a2 100644 --- a/pkg/server/handlers.go +++ b/pkg/server/handlers.go @@ -18,10 +18,10 @@ import ( type Handler struct { InstanceManager manager.InstanceManager - cfg config.Config + cfg config.AppConfig } -func NewHandler(im manager.InstanceManager, cfg config.Config) *Handler { +func NewHandler(im manager.InstanceManager, cfg config.AppConfig) *Handler { return &Handler{ InstanceManager: im, cfg: cfg,