okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufrefactor(cli): apply Go modernize simplifications02d2013

main
3.7 KiB107 linesraw
1package daemon
2
3import (
4	"context"
5	"fmt"
6	"io"
7	"net/http"
8	"strings"
9	"time"
10
11	"albedo/cli/internal/daemon/protocol"
12)
13
14const completedActionTimeout = 200 * time.Second
15
16type ReloadRequest struct{ Target string }
17
18type SessionReloadResult protocol.ReloadResult
19type KernelUpgradeResult protocol.KernelUpgradeResult
20type CompactionResult protocol.CompactionResult
21
22func ReloadSession(ctx context.Context, conn *Connection, id string, request ReloadRequest) (SessionReloadResult, error) {
23	var w protocol.ReloadResult
24	err := executeMutation(ctx, conn, operation{Name: "reload session", BuildRequest: func(base string, body io.Reader) (*http.Request, error) {
25		return protocol.NewReloadSessionRequestWithBody(base, id, "application/json", body)
26	}, Body: protocol.ReloadRequest{Target: optionalText(request.Target)}, Policy: noRecovery, Timeout: completedActionTimeout}, []int{200}, func(data []byte, _ int) error {
27		if err := decodeRequired(data, &w); err != nil {
28			return err
29		}
30		if w.CachePolicy != nil && (w.CachePolicy.State != "refreshed" && w.CachePolicy.State != "failed" || (w.CachePolicy.State == "failed") != (w.CachePolicy.Failure != nil)) {
31			return fieldError("cache policy reload outcome")
32		}
33		return nil
34	})
35	return SessionReloadResult(w), err
36}
37
38func (r SessionReloadResult) Message() string {
39	message := "Reload completed."
40	if r.Session != nil {
41		if r.Session.State != "applied" {
42			message = "Session reload " + r.Session.State + "."
43		}
44		if r.Session.RestartRequired {
45			message += " Kernel restart required."
46		}
47		if r.Session.Failure != nil {
48			message = r.Session.Failure.Detail
49		}
50		for _, warning := range r.Session.Warnings {
51			message += " " + warning.Detail
52		}
53	}
54	for _, model := range r.Models {
55		if model.Failure != nil {
56			message += " " + model.Failure.Detail
57		}
58	}
59	if r.CachePolicy != nil && r.CachePolicy.Failure != nil {
60		message += " Cache policy reload failed: " + r.CachePolicy.Failure.Detail
61	}
62	return message
63}
64func UpgradeKernel(ctx context.Context, conn *Connection, id string) (KernelUpgradeResult, error) {
65	var w protocol.KernelUpgradeResult
66	err := executeMutation(ctx, conn, operation{Name: "upgrade kernel", BuildRequest: func(base string, body io.Reader) (*http.Request, error) {
67		return protocol.NewUpgradeKernelRequestWithBody(base, id, "application/json", body)
68	}, Body: struct{}{}, Policy: noRecovery, Timeout: completedActionTimeout}, []int{200}, func(data []byte, _ int) error {
69		return decodeRequired(data, &w)
70	})
71	return KernelUpgradeResult(w), err
72}
73
74func (r KernelUpgradeResult) Message() string {
75	var message strings.Builder
76	fmt.Fprintf(&message, "Kernel %s; %d background jobs stopped.", r.State, len(r.StoppedJobs))
77	for _, warning := range r.Warnings {
78		message.WriteByte(' ')
79		message.WriteString(warning.Detail)
80	}
81	if r.Failure != nil {
82		message.WriteByte(' ')
83		message.WriteString(r.Failure.Detail)
84	}
85	return message.String()
86}
87func CompactSession(ctx context.Context, conn *Connection, id, strategy string) (CompactionResult, error) {
88	body := protocol.CompactionRequest{Strategy: optionalText(strategy)}
89	var w protocol.CompactionResult
90	err := executeMutation(ctx, conn, operation{Name: "compact session", BuildRequest: func(base string, body io.Reader) (*http.Request, error) {
91		return protocol.NewCompactSessionRequestWithBody(base, id, "application/json", body)
92	}, Body: body, Policy: noRecovery, Timeout: completedActionTimeout}, []int{200}, func(data []byte, _ int) error {
93		return decodeRequired(data, &w)
94	})
95	return CompactionResult(w), err
96}
97
98func (r CompactionResult) Message() string {
99	message := "Context " + r.State + "."
100	if r.Observation != nil {
101		message += fmt.Sprintf(" %d entries evicted.", r.Observation.EvictedEntries)
102	}
103	if r.Failure != nil {
104		message += " " + r.Failure.Detail
105	}
106	return message
107}