Files
KlipperAnywhere/cmd/server/install_jobs.go
T
2026-05-14 02:27:26 +01:00

200 lines
5.0 KiB
Go

package main
import (
"strings"
"sync"
"time"
)
type installJobStep struct {
At time.Time `json:"at"`
Stage string `json:"stage"`
Message string `json:"message"`
}
type installJobSnapshot struct {
ID string `json:"id"`
PrinterID string `json:"printer_id"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
HostType string `json:"host_type,omitempty"`
Status string `json:"status"`
Stage string `json:"stage"`
Message string `json:"message"`
Steps []installJobStep `json:"steps"`
Output string `json:"output"`
Result *sshInstallResponse `json:"result,omitempty"`
Error string `json:"error,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
}
type installJob struct {
mu sync.RWMutex
ID string
PrinterID string
Host string
Port int
HostType string
Status string
Stage string
Message string
Steps []installJobStep
Output []string
Result *sshInstallResponse
Error string
CreatedAt time.Time
UpdatedAt time.Time
CompletedAt *time.Time
}
func (s *server) newInstallJob(req sshInstallRequest) (*installJob, error) {
id, err := newID()
if err != nil {
return nil, err
}
job := &installJob{
ID: id,
PrinterID: strings.TrimSpace(req.PrinterID),
Host: strings.TrimSpace(req.Host),
Port: req.Port,
HostType: normalizeInstallHostType(req.HostType),
Status: "queued",
Stage: "queued",
Message: "Waiting to start installer job.",
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
}
s.installJobsMu.Lock()
s.installJobs[id] = job
s.installJobsMu.Unlock()
return job, nil
}
func (s *server) getInstallJob(id string) (*installJob, bool) {
s.installJobsMu.RLock()
job, ok := s.installJobs[id]
s.installJobsMu.RUnlock()
return job, ok
}
func (j *installJob) addStep(stage, message string) {
j.mu.Lock()
defer j.mu.Unlock()
now := time.Now().UTC()
j.Stage = stage
j.Message = message
j.UpdatedAt = now
j.Steps = append(j.Steps, installJobStep{At: now, Stage: stage, Message: message})
if len(j.Steps) > 120 {
j.Steps = append([]installJobStep(nil), j.Steps[len(j.Steps)-120:]...)
}
if message != "" {
j.Output = append(j.Output, formatInstallJobLine(now, stage, message))
if len(j.Output) > 240 {
j.Output = append([]string(nil), j.Output[len(j.Output)-240:]...)
}
}
}
func (j *installJob) markRunning(stage, message string) {
j.mu.Lock()
defer j.mu.Unlock()
now := time.Now().UTC()
j.Status = "running"
j.Stage = stage
j.Message = message
j.UpdatedAt = now
j.Steps = append(j.Steps, installJobStep{At: now, Stage: stage, Message: message})
if len(j.Steps) > 120 {
j.Steps = append([]installJobStep(nil), j.Steps[len(j.Steps)-120:]...)
}
if message != "" {
j.Output = append(j.Output, formatInstallJobLine(now, stage, message))
if len(j.Output) > 240 {
j.Output = append([]string(nil), j.Output[len(j.Output)-240:]...)
}
}
}
func (j *installJob) addOutput(stage, message string) {
j.mu.Lock()
defer j.mu.Unlock()
now := time.Now().UTC()
j.UpdatedAt = now
j.Output = append(j.Output, formatInstallJobLine(now, stage, message))
if len(j.Output) > 240 {
j.Output = append([]string(nil), j.Output[len(j.Output)-240:]...)
}
}
func (j *installJob) finish(status, stage, message string, resp *sshInstallResponse, err error) {
j.mu.Lock()
defer j.mu.Unlock()
now := time.Now().UTC()
j.Status = status
j.Stage = stage
j.Message = message
j.UpdatedAt = now
if status == "succeeded" || status == "failed" {
j.CompletedAt = &now
}
if resp != nil {
j.Result = resp
}
if err != nil {
j.Error = err.Error()
}
if message != "" {
j.Output = append(j.Output, formatInstallJobLine(now, stage, message))
}
}
func (j *installJob) snapshot() installJobSnapshot {
j.mu.RLock()
defer j.mu.RUnlock()
steps := append([]installJobStep(nil), j.Steps...)
output := strings.Join(j.Output, "\n")
var completedAt *time.Time
if j.CompletedAt != nil {
t := *j.CompletedAt
completedAt = &t
}
var result *sshInstallResponse
if j.Result != nil {
r := *j.Result
result = &r
}
return installJobSnapshot{
ID: j.ID,
PrinterID: j.PrinterID,
Host: j.Host,
Port: j.Port,
HostType: j.HostType,
Status: j.Status,
Stage: j.Stage,
Message: j.Message,
Steps: steps,
Output: output,
Result: result,
Error: j.Error,
CreatedAt: j.CreatedAt,
UpdatedAt: j.UpdatedAt,
CompletedAt: completedAt,
}
}
func formatInstallJobLine(at time.Time, stage, message string) string {
ts := at.Format("15:04:05")
stage = strings.TrimSpace(stage)
message = strings.TrimSpace(message)
if stage == "" {
return ts + " " + message
}
if message == "" {
return ts + " [" + stage + "]"
}
return ts + " [" + stage + "] " + message
}