-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.go
More file actions
181 lines (153 loc) · 4.92 KB
/
Copy pathstatus.go
File metadata and controls
181 lines (153 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2026 Matt Wise
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"sync"
)
// AppState represents the current lifecycle state of the application.
type AppState int
const (
StateIdle AppState = iota
StateStarting
StateRunning
StateStopping
StateError
)
func (s AppState) String() string {
switch s {
case StateIdle:
return "idle"
case StateStarting:
return "starting"
case StateRunning:
return "running"
case StateStopping:
return "stopping"
case StateError:
return "error"
default:
return "unknown"
}
}
// RunnerStatus holds the current state of a single runner VM for the
// control socket API.
type RunnerStatus struct {
// Name is the unique runner VM name (e.g., "runner-a1b2c3d4").
Name string `json:"name"`
// State is the runner lifecycle state: "idle" or "busy".
State string `json:"state"`
// Job is the display name of the GitHub Actions job assigned to this
// runner, if any.
Job string `json:"job,omitempty"`
// Repo is the "owner/repo" of the repository that owns the assigned job.
Repo string `json:"repo,omitempty"`
// CPUPercent is the combined CPU usage of the tart process and its
// proportional share of XPC hypervisor processes.
CPUPercent float64 `json:"cpu_percent,omitempty"`
// MemoryRSS is the combined resident set size (bytes) of the tart
// process and its proportional share of XPC hypervisor memory.
MemoryRSS uint64 `json:"memory_rss,omitempty"`
// UptimeSecs is the wall-clock time (seconds) since the runner VM was
// started.
UptimeSecs float64 `json:"uptime_seconds,omitempty"`
// JobDurSecs is the wall-clock time (seconds) since the current job
// started, or zero if the runner is idle.
JobDurSecs float64 `json:"job_duration_seconds,omitempty"`
}
// StatusSnapshot is the JSON response for the /status control endpoint.
// It provides a complete view of the application state for the Swift UI.
type StatusSnapshot struct {
// State is the application lifecycle state (idle, starting, running,
// stopping, or error).
State string `json:"state"`
// IdleRunners is the number of runner VMs waiting for a job.
IdleRunners int `json:"idle_runners"`
// BusyRunners is the number of runner VMs currently executing a job.
BusyRunners int `json:"busy_runners"`
// Runners is the per-runner detail list, including state and metrics.
Runners []RunnerStatus `json:"runners"`
// Error holds the most recent fatal error message, if the application
// is in the error state.
Error string `json:"error,omitempty"`
// Host contains system-wide CPU, memory, and disk usage metrics.
// Nil when no metrics collector is running.
Host *HostMetrics `json:"host,omitempty"`
// Aggregate contains lifetime job counters (completed, succeeded,
// failed, total duration). Nil when no metrics collector is running.
Aggregate *AggregateMetrics `json:"aggregate,omitempty"`
}
// AppStatus provides thread-safe observable state for the CLI process.
// It is read by the control socket server to generate StatusSnapshot
// responses for the Swift UI.
type AppStatus struct {
mu sync.Mutex
state AppState
idleRunners int
busyRunners int
runners []RunnerStatus
lastError string
}
func NewAppStatus() *AppStatus {
return &AppStatus{state: StateIdle}
}
func (s *AppStatus) SetState(state AppState) {
s.mu.Lock()
defer s.mu.Unlock()
s.state = state
if state != StateError {
s.lastError = ""
}
}
func (s *AppStatus) SetError(err error) {
s.mu.Lock()
defer s.mu.Unlock()
s.state = StateError
if err != nil {
s.lastError = err.Error()
}
}
func (s *AppStatus) SetRunners(idle, busy int) {
s.mu.Lock()
defer s.mu.Unlock()
s.idleRunners = idle
s.busyRunners = busy
}
// SetRunnerDetails updates the per-runner status list. Called by the
// scaler when runner state changes.
func (s *AppStatus) SetRunnerDetails(runners []RunnerStatus) {
s.mu.Lock()
defer s.mu.Unlock()
s.runners = runners
}
// Snapshot returns a JSON-serializable copy of the current state.
func (s *AppStatus) Snapshot() StatusSnapshot {
s.mu.Lock()
defer s.mu.Unlock()
runners := s.runners
if runners == nil {
runners = []RunnerStatus{}
}
return StatusSnapshot{
State: s.state.String(),
IdleRunners: s.idleRunners,
BusyRunners: s.busyRunners,
Runners: runners,
Error: s.lastError,
}
}
// SnapshotJSON returns the snapshot as JSON bytes.
func (s *AppStatus) SnapshotJSON() ([]byte, error) {
return json.Marshal(s.Snapshot())
}