Rename CreateInstanceRequest to CreateInstanceOptions and update related functions for consistency

This commit is contained in:
2025-07-19 21:20:10 +02:00
parent 301e170974
commit 4529959e11
4 changed files with 34 additions and 42 deletions

View File

@@ -127,7 +127,7 @@ func (h *Handler) CreateInstance() http.HandlerFunc {
return return
} }
var options CreateInstanceRequest var options CreateInstanceOptions
if err := json.NewDecoder(r.Body).Decode(&options); err != nil { if err := json.NewDecoder(r.Body).Decode(&options); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest) http.Error(w, "Invalid request body", http.StatusBadRequest)
return return
@@ -199,7 +199,7 @@ func (h *Handler) UpdateInstance() http.HandlerFunc {
return return
} }
var options CreateInstanceRequest var options CreateInstanceOptions
if err := json.NewDecoder(r.Body).Decode(&options); err != nil { if err := json.NewDecoder(r.Body).Decode(&options); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest) http.Error(w, "Invalid request body", http.StatusBadRequest)
return return

View File

@@ -42,7 +42,7 @@ func (d Duration) ToDuration() time.Duration {
return time.Duration(d) return time.Duration(d)
} }
type CreateInstanceRequest struct { type CreateInstanceOptions struct {
// Auto restart // Auto restart
AutoRestart bool `json:"auto_restart,omitempty"` AutoRestart bool `json:"auto_restart,omitempty"`
MaxRestarts int `json:"max_restarts,omitempty"` MaxRestarts int `json:"max_restarts,omitempty"`
@@ -53,14 +53,14 @@ type CreateInstanceRequest struct {
// Instance represents a running instance of the llama server // Instance represents a running instance of the llama server
type Instance struct { type Instance struct {
Name string `json:"name"` Name string `json:"name"`
options *CreateInstanceRequest `json:"-"` // Now unexported - access via GetOptions/SetOptions options *CreateInstanceOptions `json:"-"`
globalSettings *InstancesConfig
// Status // Status
Running bool `json:"running"` Running bool `json:"running"`
// Log files // Log file
logPath string `json:"-"`
logFile *os.File `json:"-"` logFile *os.File `json:"-"`
// internal // internal
@@ -75,25 +75,20 @@ type Instance 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, logPath string, options *CreateInstanceRequest) *Instance { func NewInstance(name string, globalSettings *InstancesConfig, options *CreateInstanceOptions) *Instance {
return &Instance{ return &Instance{
Name: name, Name: name,
options: options, options: options,
globalSettings: globalSettings,
Running: false, Running: false,
logPath: logPath,
} }
} }
// createLogFiles creates and opens the log files for stdout and stderr // createLogFile creates and opens the log files for stdout and stderr
func (i *Instance) createLogFiles() error { func (i *Instance) createLogFile() error {
if i.logPath == "" { logPath := i.globalSettings.LogDirectory + "/" + i.Name + ".log"
return fmt.Errorf("log file path not set") logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
}
// Create stdout log file
logFile, err := os.OpenFile(i.logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil { if err != nil {
return fmt.Errorf("failed to create stdout log file: %w", err) return fmt.Errorf("failed to create stdout log file: %w", err)
} }
@@ -107,8 +102,8 @@ func (i *Instance) createLogFiles() error {
return nil return nil
} }
// closeLogFiles closes the log files // closeLogFile closes the log files
func (i *Instance) closeLogFiles() { func (i *Instance) closeLogFile() {
if i.logFile != nil { if i.logFile != nil {
timestamp := time.Now().Format("2006-01-02 15:04:05") timestamp := time.Now().Format("2006-01-02 15:04:05")
fmt.Fprintf(i.logFile, "=== Instance %s stopped at %s ===\n\n", i.Name, timestamp) fmt.Fprintf(i.logFile, "=== Instance %s stopped at %s ===\n\n", i.Name, timestamp)
@@ -117,13 +112,13 @@ func (i *Instance) closeLogFiles() {
} }
} }
func (i *Instance) GetOptions() *CreateInstanceRequest { func (i *Instance) GetOptions() *CreateInstanceOptions {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
return i.options return i.options
} }
func (i *Instance) SetOptions(options *CreateInstanceRequest) { func (i *Instance) SetOptions(options *CreateInstanceOptions) {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
if options == nil { if options == nil {
@@ -165,7 +160,7 @@ func (i *Instance) Start() error {
} }
// Create log files // Create log files
if err := i.createLogFiles(); err != nil { if err := i.createLogFile(); err != nil {
return fmt.Errorf("failed to create log files: %w", err) return fmt.Errorf("failed to create log files: %w", err)
} }
@@ -184,13 +179,13 @@ func (i *Instance) Start() error {
var err error var err error
i.stdout, err = i.cmd.StdoutPipe() i.stdout, err = i.cmd.StdoutPipe()
if err != nil { if err != nil {
i.closeLogFiles() // Ensure log files are closed on error i.closeLogFile() // Ensure log files are closed on error
return fmt.Errorf("failed to get stdout pipe: %w", err) return fmt.Errorf("failed to get stdout pipe: %w", err)
} }
i.stderr, err = i.cmd.StderrPipe() i.stderr, err = i.cmd.StderrPipe()
if err != nil { if err != nil {
i.stdout.Close() // Ensure stdout is closed on error i.stdout.Close() // Ensure stdout is closed on error
i.closeLogFiles() // Ensure log files are closed on error i.closeLogFile() // Ensure log files are closed on error
return fmt.Errorf("failed to get stderr pipe: %w", err) return fmt.Errorf("failed to get stderr pipe: %w", err)
} }
@@ -241,7 +236,7 @@ func (i *Instance) Stop() error {
i.Running = false i.Running = false
i.closeLogFiles() // Close log files after stopping i.closeLogFile() // Close log files after stopping
return nil return nil
} }
@@ -282,10 +277,7 @@ func (i *Instance) GetLogs(num_lines int) (string, error) {
} }
// Return the last N lines // Return the last N lines
start := len(lines) - num_lines start := max(len(lines)-num_lines, 0)
if start < 0 {
start = 0
}
return strings.Join(lines[start:], "\n"), nil return strings.Join(lines[start:], "\n"), nil
} }
@@ -315,7 +307,7 @@ func (i *Instance) monitorProcess() {
} }
i.Running = false i.Running = false
i.closeLogFiles() i.closeLogFile()
// Log the exit // Log the exit
if err != nil { if err != nil {
@@ -355,7 +347,7 @@ func (i *Instance) MarshalJSON() ([]byte, error) {
// Create a temporary struct with exported fields for JSON marshalling // Create a temporary struct with exported fields for JSON marshalling
temp := struct { temp := struct {
Name string `json:"name"` Name string `json:"name"`
Options *CreateInstanceRequest `json:"options,omitempty"` Options *CreateInstanceOptions `json:"options,omitempty"`
Running bool `json:"running"` Running bool `json:"running"`
}{ }{
Name: i.Name, Name: i.Name,
@@ -371,7 +363,7 @@ func (i *Instance) UnmarshalJSON(data []byte) error {
// Create a temporary struct for unmarshalling // Create a temporary struct for unmarshalling
temp := struct { temp := struct {
Name string `json:"name"` Name string `json:"name"`
Options *CreateInstanceRequest `json:"options,omitempty"` Options *CreateInstanceOptions `json:"options,omitempty"`
Running bool `json:"running"` Running bool `json:"running"`
}{} }{}

View File

@@ -7,9 +7,9 @@ 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, error) ListInstances() ([]*Instance, error)
CreateInstance(name string, options *CreateInstanceRequest) (*Instance, error) CreateInstance(name string, options *CreateInstanceOptions) (*Instance, error)
GetInstance(name string) (*Instance, error) GetInstance(name string) (*Instance, error)
UpdateInstance(name string, options *CreateInstanceRequest) (*Instance, error) UpdateInstance(name string, options *CreateInstanceOptions) (*Instance, error)
DeleteInstance(name string) error DeleteInstance(name string) error
StartInstance(name string) (*Instance, error) StartInstance(name string) (*Instance, error)
StopInstance(name string) (*Instance, error) StopInstance(name string) (*Instance, error)
@@ -43,7 +43,7 @@ func (im *instanceManager) ListInstances() ([]*Instance, 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 *CreateInstanceRequest) (*Instance, error) { func (im *instanceManager) CreateInstance(name string, options *CreateInstanceOptions) (*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")
} }
@@ -72,7 +72,7 @@ func (im *instanceManager) CreateInstance(name string, options *CreateInstanceRe
options.Port = port options.Port = port
} }
instance := NewInstance(name, im.instancesConfig.LogDirectory, options) instance := NewInstance(name, &im.instancesConfig, options)
im.instances[instance.Name] = instance im.instances[instance.Name] = instance
return instance, nil return instance, nil
@@ -88,7 +88,7 @@ func (im *instanceManager) GetInstance(name string) (*Instance, error) {
} }
// UpdateInstance updates the options of an existing instance and returns it. // UpdateInstance updates the options of an existing instance and returns it.
func (im *instanceManager) UpdateInstance(name string, options *CreateInstanceRequest) (*Instance, error) { func (im *instanceManager) UpdateInstance(name string, options *CreateInstanceOptions) (*Instance, error) {
instance, exists := im.instances[name] instance, exists := im.instances[name]
if !exists { if !exists {
return nil, fmt.Errorf("instance with name %s not found", name) return nil, fmt.Errorf("instance with name %s not found", name)

View File

@@ -32,7 +32,7 @@ func validateStringForInjection(value string) error {
} }
// ValidateInstanceOptions performs minimal security validation // ValidateInstanceOptions performs minimal security validation
func ValidateInstanceOptions(options *CreateInstanceRequest) error { func ValidateInstanceOptions(options *CreateInstanceOptions) error {
if options == nil { if options == nil {
return ValidationError(fmt.Errorf("options cannot be nil")) return ValidationError(fmt.Errorf("options cannot be nil"))
} }