mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-05 16:44:22 +00:00
Use %w for error wrapping in log messages across multiple files
This commit is contained in:
@@ -38,7 +38,7 @@ func main() {
|
|||||||
configPath := os.Getenv("LLAMACTL_CONFIG_PATH")
|
configPath := os.Getenv("LLAMACTL_CONFIG_PATH")
|
||||||
cfg, err := config.LoadConfig(configPath)
|
cfg, err := config.LoadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error loading config: %v\n", err)
|
fmt.Printf("Error loading config: %w\n", err)
|
||||||
fmt.Println("Using default configuration.")
|
fmt.Println("Using default configuration.")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,12 +50,12 @@ func main() {
|
|||||||
// Create the data directory if it doesn't exist
|
// Create the data directory if it doesn't exist
|
||||||
if cfg.Instances.AutoCreateDirs {
|
if cfg.Instances.AutoCreateDirs {
|
||||||
if err := os.MkdirAll(cfg.Instances.InstancesDir, 0755); err != nil {
|
if err := os.MkdirAll(cfg.Instances.InstancesDir, 0755); err != nil {
|
||||||
fmt.Printf("Error creating config directory %s: %v\n", cfg.Instances.InstancesDir, err)
|
fmt.Printf("Error creating config directory %s: %w\n", cfg.Instances.InstancesDir, err)
|
||||||
fmt.Println("Persistence will not be available.")
|
fmt.Println("Persistence will not be available.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(cfg.Instances.LogsDir, 0755); err != nil {
|
if err := os.MkdirAll(cfg.Instances.LogsDir, 0755); err != nil {
|
||||||
fmt.Printf("Error creating log directory %s: %v\n", cfg.Instances.LogsDir, err)
|
fmt.Printf("Error creating log directory %s: %w\n", cfg.Instances.LogsDir, err)
|
||||||
fmt.Println("Instance logs will not be available.")
|
fmt.Println("Instance logs will not be available.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ func main() {
|
|||||||
go func() {
|
go func() {
|
||||||
fmt.Printf("Llamactl server listening on %s:%d\n", cfg.Server.Host, cfg.Server.Port)
|
fmt.Printf("Llamactl server listening on %s:%d\n", cfg.Server.Host, cfg.Server.Port)
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
fmt.Printf("Error starting server: %v\n", err)
|
fmt.Printf("Error starting server: %w\n", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ func main() {
|
|||||||
fmt.Println("Shutting down server...")
|
fmt.Println("Shutting down server...")
|
||||||
|
|
||||||
if err := server.Close(); err != nil {
|
if err := server.Close(); err != nil {
|
||||||
fmt.Printf("Error shutting down server: %v\n", err)
|
fmt.Printf("Error shutting down server: %w\n", err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Server shut down gracefully.")
|
fmt.Println("Server shut down gracefully.")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ func (p *process) stop() error {
|
|||||||
// Stop the process with SIGINT if cmd exists
|
// Stop the process with SIGINT if cmd exists
|
||||||
if p.cmd != nil && p.cmd.Process != nil {
|
if p.cmd != nil && p.cmd.Process != nil {
|
||||||
if err := p.cmd.Process.Signal(syscall.SIGINT); err != nil {
|
if err := p.cmd.Process.Signal(syscall.SIGINT); err != nil {
|
||||||
log.Printf("Failed to send SIGINT to instance %s: %v", p.instance.Name, err)
|
log.Printf("Failed to send SIGINT to instance %s: %w", p.instance.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +161,7 @@ func (p *process) stop() error {
|
|||||||
if p.cmd != nil && p.cmd.Process != nil {
|
if p.cmd != nil && p.cmd.Process != nil {
|
||||||
killErr := p.cmd.Process.Kill()
|
killErr := p.cmd.Process.Kill()
|
||||||
if killErr != nil {
|
if killErr != nil {
|
||||||
log.Printf("Failed to force kill instance %s: %v", p.instance.Name, killErr)
|
log.Printf("Failed to force kill instance %s: %w", p.instance.Name, killErr)
|
||||||
}
|
}
|
||||||
log.Printf("Instance %s did not stop in time, force killed", p.instance.Name)
|
log.Printf("Instance %s did not stop in time, force killed", p.instance.Name)
|
||||||
|
|
||||||
@@ -292,7 +292,7 @@ func (p *process) monitorProcess() {
|
|||||||
|
|
||||||
// Log the exit
|
// Log the exit
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Instance %s crashed with error: %v", p.instance.Name, err)
|
log.Printf("Instance %s crashed with error: %w", p.instance.Name, err)
|
||||||
// Handle auto-restart logic
|
// Handle auto-restart logic
|
||||||
p.handleAutoRestart(err)
|
p.handleAutoRestart(err)
|
||||||
} else {
|
} else {
|
||||||
@@ -376,7 +376,7 @@ func (p *process) handleAutoRestart(err error) {
|
|||||||
|
|
||||||
// Restart the instance
|
// Restart the instance
|
||||||
if err := p.start(); err != nil {
|
if err := p.start(); err != nil {
|
||||||
log.Printf("Failed to restart instance %s: %v", p.instance.Name, err)
|
log.Printf("Failed to restart instance %s: %w", p.instance.Name, err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Successfully restarted instance %s", p.instance.Name)
|
log.Printf("Successfully restarted instance %s", p.instance.Name)
|
||||||
// Clear the cancel function
|
// Clear the cancel function
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ func (l *lifecycleManager) checkTimeouts() {
|
|||||||
for _, name := range timeoutInstances {
|
for _, name := range timeoutInstances {
|
||||||
log.Printf("Instance %s has timed out, stopping it", name)
|
log.Printf("Instance %s has timed out, stopping it", name)
|
||||||
if _, err := l.manager.StopInstance(name); err != nil {
|
if _, err := l.manager.StopInstance(name); err != nil {
|
||||||
log.Printf("Error stopping instance %s: %v", name, err)
|
log.Printf("Error stopping instance %s: %w", name, err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Instance %s stopped successfully", name)
|
log.Printf("Instance %s stopped successfully", name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,13 +56,13 @@ func New(globalConfig *config.AppConfig) InstanceManager {
|
|||||||
portRange := globalConfig.Instances.PortRange
|
portRange := globalConfig.Instances.PortRange
|
||||||
ports, err := newPortAllocator(portRange[0], portRange[1])
|
ports, err := newPortAllocator(portRange[0], portRange[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create port allocator: %v", err)
|
log.Fatalf("Failed to create port allocator: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize persistence
|
// Initialize persistence
|
||||||
persistence, err := newInstancePersister(globalConfig.Instances.InstancesDir)
|
persistence, err := newInstancePersister(globalConfig.Instances.InstancesDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create instance persister: %v", err)
|
log.Fatalf("Failed to create instance persister: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize remote manager
|
// Initialize remote manager
|
||||||
@@ -83,7 +83,7 @@ func New(globalConfig *config.AppConfig) InstanceManager {
|
|||||||
|
|
||||||
// Load existing instances from disk
|
// Load existing instances from disk
|
||||||
if err := im.loadInstances(); err != nil {
|
if err := im.loadInstances(); err != nil {
|
||||||
log.Printf("Error loading instances: %v", err)
|
log.Printf("Error loading instances: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the lifecycle manager
|
// Start the lifecycle manager
|
||||||
@@ -116,7 +116,7 @@ func (im *instanceManager) Shutdown() {
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
fmt.Printf("Stopping instance %s...\n", inst.Name)
|
fmt.Printf("Stopping instance %s...\n", inst.Name)
|
||||||
if err := inst.Stop(); err != nil {
|
if err := inst.Stop(); err != nil {
|
||||||
fmt.Printf("Error stopping instance %s: %v\n", inst.Name, err)
|
fmt.Printf("Error stopping instance %s: %w\n", inst.Name, err)
|
||||||
}
|
}
|
||||||
}(inst)
|
}(inst)
|
||||||
}
|
}
|
||||||
@@ -140,7 +140,7 @@ func (im *instanceManager) loadInstances() error {
|
|||||||
// Process each loaded instance
|
// Process each loaded instance
|
||||||
for _, persistedInst := range instances {
|
for _, persistedInst := range instances {
|
||||||
if err := im.loadInstance(persistedInst); err != nil {
|
if err := im.loadInstance(persistedInst); err != nil {
|
||||||
log.Printf("Failed to load instance %s: %v", persistedInst.Name, err)
|
log.Printf("Failed to load instance %s: %w", persistedInst.Name, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -251,12 +251,12 @@ func (im *instanceManager) autoStartInstances() {
|
|||||||
// Remote instance - use remote manager with context
|
// Remote instance - use remote manager with context
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if _, err := im.remote.startInstance(ctx, node, inst.Name); err != nil {
|
if _, err := im.remote.startInstance(ctx, node, inst.Name); err != nil {
|
||||||
log.Printf("Failed to auto-start remote instance %s: %v", inst.Name, err)
|
log.Printf("Failed to auto-start remote instance %s: %w", inst.Name, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Local instance - call Start() directly
|
// Local instance - call Start() directly
|
||||||
if err := inst.Start(); err != nil {
|
if err := inst.Start(); err != nil {
|
||||||
log.Printf("Failed to auto-start instance %s: %v", inst.Name, err)
|
log.Printf("Failed to auto-start instance %s: %w", inst.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ func (im *instanceManager) CreateInstance(name string, options *instance.Options
|
|||||||
|
|
||||||
// Persist instance (best-effort, don't fail if persistence fails)
|
// Persist instance (best-effort, don't fail if persistence fails)
|
||||||
if err := im.persistInstance(inst); err != nil {
|
if err := im.persistInstance(inst); err != nil {
|
||||||
log.Printf("Warning: failed to persist instance %s: %v", name, err)
|
log.Printf("Warning: failed to persist instance %s: %w", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return inst, nil
|
return inst, nil
|
||||||
@@ -394,7 +394,7 @@ func (im *instanceManager) StartInstance(name string) (*instance.Instance, error
|
|||||||
|
|
||||||
// Persist instance (best-effort, don't fail if persistence fails)
|
// Persist instance (best-effort, don't fail if persistence fails)
|
||||||
if err := im.persistInstance(inst); err != nil {
|
if err := im.persistInstance(inst); err != nil {
|
||||||
log.Printf("Warning: failed to persist instance %s: %v", name, err)
|
log.Printf("Warning: failed to persist instance %s: %w", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return inst, nil
|
return inst, nil
|
||||||
@@ -453,7 +453,7 @@ func (im *instanceManager) StopInstance(name string) (*instance.Instance, error)
|
|||||||
|
|
||||||
// Persist instance (best-effort, don't fail if persistence fails)
|
// Persist instance (best-effort, don't fail if persistence fails)
|
||||||
if err := im.persistInstance(inst); err != nil {
|
if err := im.persistInstance(inst); err != nil {
|
||||||
log.Printf("Warning: failed to persist instance %s: %v", name, err)
|
log.Printf("Warning: failed to persist instance %s: %w", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return inst, nil
|
return inst, nil
|
||||||
@@ -499,7 +499,7 @@ func (im *instanceManager) RestartInstance(name string) (*instance.Instance, err
|
|||||||
|
|
||||||
// Persist the restarted instance
|
// Persist the restarted instance
|
||||||
if err := im.persistInstance(inst); err != nil {
|
if err := im.persistInstance(inst); err != nil {
|
||||||
log.Printf("Warning: failed to persist instance %s: %v", name, err)
|
log.Printf("Warning: failed to persist instance %s: %w", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return inst, nil
|
return inst, nil
|
||||||
|
|||||||
@@ -162,8 +162,8 @@ func (p *instancePersister) loadAll() ([]*instance.Instance, error) {
|
|||||||
|
|
||||||
inst, err := p.loadInstanceFile(instanceName, instancePath)
|
inst, err := p.loadInstanceFile(instanceName, instancePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to load instance %s: %v", instanceName, err)
|
log.Printf("Failed to load instance %s: %w", instanceName, err)
|
||||||
loadErrors = append(loadErrors, fmt.Sprintf("%s: %v", instanceName, err))
|
loadErrors = append(loadErrors, fmt.Sprintf("%s: %w", instanceName, err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func writeError(w http.ResponseWriter, status int, code, details string) {
|
|||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
if err := json.NewEncoder(w).Encode(errorResponse{Error: code, Details: details}); err != nil {
|
if err := json.NewEncoder(w).Encode(errorResponse{Error: code, Details: details}); err != nil {
|
||||||
log.Printf("Failed to encode error response: %v", err)
|
log.Printf("Failed to encode error response: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ func writeJSON(w http.ResponseWriter, status int, data any) {
|
|||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||||
log.Printf("Failed to encode JSON response: %v", err)
|
log.Printf("Failed to encode JSON response: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ func writeText(w http.ResponseWriter, status int, data string) {
|
|||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
if _, err := w.Write([]byte(data)); err != nil {
|
if _, err := w.Write([]byte(data)); err != nil {
|
||||||
log.Printf("Failed to write text response: %v", err)
|
log.Printf("Failed to write text response: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ func SetupRouter(handler *Handler) *chi.Mux {
|
|||||||
|
|
||||||
// Serve WebUI files
|
// Serve WebUI files
|
||||||
if err := webui.SetupWebUI(r); err != nil {
|
if err := webui.SetupWebUI(r); err != nil {
|
||||||
fmt.Printf("Failed to set up WebUI: %v\n", err)
|
fmt.Printf("Failed to set up WebUI: %w\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|||||||
Reference in New Issue
Block a user