mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 09:04:27 +00:00
Update API documentation and instance configuration
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -46,8 +45,8 @@ type InstancesConfig struct {
|
||||
// Default max restarts for new instances
|
||||
DefaultMaxRestarts int `yaml:"default_max_restarts"`
|
||||
|
||||
// Default restart delay for new instances
|
||||
DefaultRestartDelay Duration `yaml:"default_restart_delay"`
|
||||
// Default restart delay for new instances (in seconds)
|
||||
DefaultRestartDelay int `yaml:"default_restart_delay"`
|
||||
}
|
||||
|
||||
// LoadConfig loads configuration with the following precedence:
|
||||
@@ -68,7 +67,7 @@ func LoadConfig(configPath string) (Config, error) {
|
||||
LlamaExecutable: "llama-server",
|
||||
DefaultAutoRestart: false,
|
||||
DefaultMaxRestarts: 3,
|
||||
DefaultRestartDelay: Duration(5 * time.Second),
|
||||
DefaultRestartDelay: 5,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -147,29 +146,12 @@ func loadEnvVars(cfg *Config) {
|
||||
}
|
||||
}
|
||||
if restartDelay := os.Getenv("LLAMACTL_DEFAULT_RESTART_DELAY"); restartDelay != "" {
|
||||
if d, err := parseDelaySeconds(restartDelay); err == nil {
|
||||
cfg.Instances.DefaultRestartDelay = Duration(d)
|
||||
if seconds, err := strconv.Atoi(restartDelay); err == nil {
|
||||
cfg.Instances.DefaultRestartDelay = seconds
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parseDelaySeconds parses a string as seconds and returns a time.Duration
|
||||
// Accepts both plain numbers (seconds) and duration strings like "5s", "30s"
|
||||
func parseDelaySeconds(s string) (time.Duration, error) {
|
||||
// If it contains letters, try parsing as duration
|
||||
if strings.ContainsAny(s, "smh") {
|
||||
return time.ParseDuration(s)
|
||||
}
|
||||
|
||||
// Otherwise parse as seconds
|
||||
seconds, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return time.Duration(seconds * float64(time.Second)), nil
|
||||
}
|
||||
|
||||
// parsePortRange parses port range from string formats like "8000-9000" or "8000,9000"
|
||||
func parsePortRange(s string) [2]int {
|
||||
var parts []string
|
||||
|
||||
@@ -25,7 +25,7 @@ func NewHandler(im InstanceManager) *Handler {
|
||||
// @Summary Get help for llama server
|
||||
// @Description Returns the help text for the llama server command
|
||||
// @Tags server
|
||||
// #Produces text/plain
|
||||
// @Produces text/plain
|
||||
// @Success 200 {string} string "Help text"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
// @Router /server/help [get]
|
||||
@@ -46,7 +46,7 @@ func (h *Handler) HelpHandler() http.HandlerFunc {
|
||||
// @Summary Get version of llama server
|
||||
// @Description Returns the version of the llama server command
|
||||
// @Tags server
|
||||
// #Produces text/plain
|
||||
// @Produces text/plain
|
||||
// @Success 200 {string} string "Version information"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
// @Router /server/version [get]
|
||||
@@ -67,7 +67,7 @@ func (h *Handler) VersionHandler() http.HandlerFunc {
|
||||
// @Summary List available devices for llama server
|
||||
// @Description Returns a list of available devices for the llama server
|
||||
// @Tags server
|
||||
// #Produces text/plain
|
||||
// @Produces text/plain
|
||||
// @Success 200 {string} string "List of devices"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
// @Router /server/devices [get]
|
||||
@@ -88,7 +88,7 @@ func (h *Handler) ListDevicesHandler() http.HandlerFunc {
|
||||
// @Summary List all instances
|
||||
// @Description Returns a list of all instances managed by the server
|
||||
// @Tags instances
|
||||
// @Produce json
|
||||
// @Produces json
|
||||
// @Success 200 {array} Instance "List of instances"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
// @Router /instances [get]
|
||||
@@ -113,12 +113,13 @@ func (h *Handler) ListInstances() http.HandlerFunc {
|
||||
// @Description Creates a new instance with the provided configuration options
|
||||
// @Tags instances
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param options body InstanceOptions true "Instance configuration options"
|
||||
// @Produces json
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Param options body CreateInstanceOptions true "Instance configuration options"
|
||||
// @Success 201 {object} Instance "Created instance details"
|
||||
// @Failure 400 {string} string "Invalid request body"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
// @Router /instances [post]
|
||||
// @Router /instances/{name} [post]
|
||||
func (h *Handler) CreateInstance() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
name := chi.URLParam(r, "name")
|
||||
@@ -152,6 +153,7 @@ func (h *Handler) CreateInstance() http.HandlerFunc {
|
||||
// @Summary Get details of a specific instance
|
||||
// @Description Returns the details of a specific instance by name
|
||||
// @Tags instances
|
||||
// @Produces json
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Success 200 {object} Instance "Instance details"
|
||||
// @Failure 400 {string} string "Invalid name format"
|
||||
@@ -184,9 +186,9 @@ func (h *Handler) GetInstance() http.HandlerFunc {
|
||||
// @Description Updates the configuration of a specific instance by name
|
||||
// @Tags instances
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Produces json
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Param options body InstanceOptions true "Instance configuration options"
|
||||
// @Param options body CreateInstanceOptions true "Instance configuration options"
|
||||
// @Success 200 {object} Instance "Updated instance details"
|
||||
// @Failure 400 {string} string "Invalid name format"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
@@ -229,7 +231,7 @@ func (h *Handler) UpdateInstance() http.HandlerFunc {
|
||||
// @Summary Start a stopped instance
|
||||
// @Description Starts a specific instance by name
|
||||
// @Tags instances
|
||||
// @Produce json
|
||||
// @Produces json
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Success 200 {object} Instance "Started instance details"
|
||||
// @Failure 400 {string} string "Invalid name format"
|
||||
@@ -261,7 +263,7 @@ func (h *Handler) StartInstance() http.HandlerFunc {
|
||||
// @Summary Stop a running instance
|
||||
// @Description Stops a specific instance by name
|
||||
// @Tags instances
|
||||
// @Produce json
|
||||
// @Produces json
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Success 200 {object} Instance "Stopped instance details"
|
||||
// @Failure 400 {string} string "Invalid name format"
|
||||
@@ -293,7 +295,7 @@ func (h *Handler) StopInstance() http.HandlerFunc {
|
||||
// @Summary Restart a running instance
|
||||
// @Description Restarts a specific instance by name
|
||||
// @Tags instances
|
||||
// @Produce json
|
||||
// @Produces json
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Success 200 {object} Instance "Restarted instance details"
|
||||
// @Failure 400 {string} string "Invalid name format"
|
||||
@@ -325,7 +327,6 @@ func (h *Handler) RestartInstance() http.HandlerFunc {
|
||||
// @Summary Delete an instance
|
||||
// @Description Stops and removes a specific instance by name
|
||||
// @Tags instances
|
||||
// @Produce json
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Success 204 "No Content"
|
||||
// @Failure 400 {string} string "Invalid name format"
|
||||
@@ -348,6 +349,17 @@ func (h *Handler) DeleteInstance() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// GetInstanceLogs godoc
|
||||
// @Summary Get logs from a specific instance
|
||||
// @Description Returns the logs from a specific instance by name with optional line limit
|
||||
// @Tags instances
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Param lines query string false "Number of lines to retrieve (default: all lines)"
|
||||
// @Produces text/plain
|
||||
// @Success 200 {string} string "Instance logs"
|
||||
// @Failure 400 {string} string "Invalid name format or lines parameter"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
// @Router /instances/{name}/logs [get]
|
||||
func (h *Handler) GetInstanceLogs() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
name := chi.URLParam(r, "name")
|
||||
@@ -384,6 +396,16 @@ func (h *Handler) GetInstanceLogs() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// ProxyToInstance godoc
|
||||
// @Summary Proxy requests to a specific instance
|
||||
// @Description Forwards HTTP requests to the llama-server instance running on a specific port
|
||||
// @Tags instances
|
||||
// @Param name path string true "Instance Name"
|
||||
// @Success 200 "Request successfully proxied to instance"
|
||||
// @Failure 400 {string} string "Invalid name format"
|
||||
// @Failure 500 {string} string "Internal Server Error"
|
||||
// @Failure 503 {string} string "Instance is not running"
|
||||
// @Router /instances/{name}/proxy [get]
|
||||
func (h *Handler) ProxyToInstance() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
name := chi.URLParam(r, "name")
|
||||
|
||||
@@ -18,35 +18,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Duration is a custom type that wraps time.Duration for better JSON/Swagger support
|
||||
// @description Duration in seconds
|
||||
type Duration time.Duration
|
||||
|
||||
// MarshalJSON implements json.Marshaler for Duration
|
||||
func (d Duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Duration(d).Seconds())
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler for Duration
|
||||
func (d *Duration) UnmarshalJSON(data []byte) error {
|
||||
var seconds float64
|
||||
if err := json.Unmarshal(data, &seconds); err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(time.Duration(seconds * float64(time.Second)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToDuration converts Duration to time.Duration
|
||||
func (d Duration) ToDuration() time.Duration {
|
||||
return time.Duration(d)
|
||||
}
|
||||
|
||||
type CreateInstanceOptions struct {
|
||||
// Auto restart
|
||||
AutoRestart *bool `json:"auto_restart,omitempty"`
|
||||
MaxRestarts *int `json:"max_restarts,omitempty"`
|
||||
RestartDelay *Duration `json:"restart_delay,omitempty"` // Duration in seconds
|
||||
AutoRestart *bool `json:"auto_restart,omitempty"`
|
||||
MaxRestarts *int `json:"max_restarts,omitempty"`
|
||||
// RestartDelay duration in seconds
|
||||
RestartDelay *int `json:"restart_delay_seconds,omitempty"`
|
||||
|
||||
LlamaServerOptions `json:",inline"`
|
||||
}
|
||||
@@ -332,12 +309,13 @@ func (i *Instance) monitorProcess() {
|
||||
// Handle restart if process crashed and auto-restart is enabled
|
||||
if err != nil && *i.options.AutoRestart && i.restarts < *i.options.MaxRestarts {
|
||||
i.restarts++
|
||||
delayDuration := time.Duration(*i.options.RestartDelay) * time.Second
|
||||
log.Printf("Auto-restarting instance %s (attempt %d/%d) in %v",
|
||||
i.Name, i.restarts, i.options.MaxRestarts, i.options.RestartDelay.ToDuration())
|
||||
i.Name, i.restarts, i.options.MaxRestarts, delayDuration)
|
||||
|
||||
// Unlock mutex during sleep to avoid blocking other operations
|
||||
i.mu.Unlock()
|
||||
time.Sleep(i.options.RestartDelay.ToDuration())
|
||||
time.Sleep(delayDuration)
|
||||
i.mu.Lock()
|
||||
|
||||
// Attempt restart
|
||||
|
||||
Reference in New Issue
Block a user