| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "log/slog" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "sync" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/release" |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | ) |
| 16 | |
| 17 | // Platform reports whether a runner on goos/goarch has the page |
| 18 | // renderer: Linux amd64 only. A Mac runner and a Linux arm64 one neither |
| 19 | // install, check nor report one. |
| 20 | func Platform(goos, goarch string) bool { return goos == "linux" && goarch == "amd64" } |
| 21 | |
| 22 | // Installed is the launch configuration of the renderer installed beside |
| 23 | // the runner's executable, for pin: the tree release.RendererTree(<its |
| 24 | // directory>, pin), bubblewrap bwrap and the runner itself as the |
| 25 | // sandbox's first process; and the scratch directory a fixture render |
| 26 | // uses, <its directory>/renderer, which the installer made private. |
| 27 | func Installed(bwrap, executable, pin string) (LaunchConfig, string) { |
| 28 | dir := filepath.Dir(executable) |
| 29 | return LaunchConfig{Bwrap: bwrap, Binary: executable, Tree: release.RendererTree(dir, pin), Pin: pin}, filepath.Join(dir, "renderer") |
| 30 | } |
| 31 | |
| 32 | // ErrNotChecked is a tree that verifies but has not rendered the fixture |
| 33 | // page since this runner started: the check is still running, or the |
| 34 | // render failed. Such a tree is not used. |
| 35 | var ErrNotChecked = errors.New("render: the renderer has not rendered its check page") |
| 36 | |
| 37 | // FixtureRetry is how long a tree whose fixture render failed waits |
| 38 | // before a check renders it again. |
| 39 | const FixtureRetry = 10 * time.Minute |
| 40 | |
| 41 | // errTreeMoved is a tree that was replaced while it was checked. |
| 42 | var errTreeMoved = &release.RendererError{Health: wire.ToolMismatch, Reason: "the tree was replaced while it was checked"} |
| 43 | |
| 44 | // Health is the runner's word on its page renderer, ToolHealth.Renderer |
| 45 | // (spec section 3, "Pin and provisioning"). It is ok only when the tree |
| 46 | // verifies against the linked pin now and this very tree (the same |
| 47 | // directory, not only the same path) has rendered the fixture page since |
| 48 | // the runner started. Otherwise it is the state VerifyRenderer gives |
| 49 | // (missing, mismatch or unpinned), or missing for a tree that verifies |
| 50 | // but has not rendered: still being checked, or unable to render, as a |
| 51 | // host that cannot nest the browser's sandbox is. |
| 52 | // |
| 53 | // The fixture is rendered once per tree: a failure is not tried again |
| 54 | // for FixtureRetry, since another try soon after does not help a |
| 55 | // renderer that cannot render (the runner needs updating); past that, |
| 56 | // the next check (Ready's or a report's) renders it once more, so a |
| 57 | // failure on a loaded box does not stick until the runner restarts. A |
| 58 | // fixture whose sandbox could not be shown to |
| 59 | // have ended (ErrNotEnded) stops every later render of this process: the |
| 60 | // runner needs attention. |
| 61 | // |
| 62 | // A nil Health is a runner without a renderer: it reports nothing and is |
| 63 | // never ready. |
| 64 | type Health struct { |
| 65 | // Config is Installed's launch configuration and Scratch its scratch |
| 66 | // directory. |
| 67 | Config LaunchConfig |
| 68 | Scratch string |
| 69 | // Verify and Fixture are release.VerifyRenderer and CheckFixture |
| 70 | // (nil for those); tests replace them. |
| 71 | Verify func(tree, pin string) error |
| 72 | Fixture func(ctx context.Context, cfg LaunchConfig, scratch string) error |
| 73 | // Changed hears that the reported state changed, so the next hello |
| 74 | // says it; it is never called with Health's lock held. |
| 75 | Changed func() |
| 76 | // Logger hears why a check was not ok, for the operator. |
| 77 | Logger *slog.Logger |
| 78 | // Now is the clock (nil: time.Now); tests replace it. |
| 79 | Now func() time.Time |
| 80 | |
| 81 | mu sync.Mutex |
| 82 | base context.Context |
| 83 | state string |
| 84 | passed os.FileInfo |
| 85 | tried []tried |
| 86 | stuck error |
| 87 | running chan struct{} |
| 88 | } |
| 89 | |
| 90 | // Start checks the renderer in the background (it does not hold up the |
| 91 | // runner's connect): the tree is verified and the fixture page rendered. |
| 92 | // Background checks run with ctx, the runner's. |
| 93 | func (h *Health) Start(ctx context.Context) { |
| 94 | if h == nil { |
| 95 | return |
| 96 | } |
| 97 | h.mu.Lock() |
| 98 | h.base = ctx |
| 99 | h.mu.Unlock() |
| 100 | h.kick() |
| 101 | } |
| 102 | |
| 103 | // Report is the state for ToolHealth.Renderer ("" without a renderer): |
| 104 | // the last check's, missing before the first has ended. It never waits |
| 105 | // on the disk; it starts another check in the background, when none is |
| 106 | // running, so the files are verified again for the next report, and a |
| 107 | // change of state is told to Changed. |
| 108 | func (h *Health) Report(context.Context) string { |
| 109 | if h == nil { |
| 110 | return "" |
| 111 | } |
| 112 | h.mu.Lock() |
| 113 | state := reported(h.state) |
| 114 | h.mu.Unlock() |
| 115 | h.kick() |
| 116 | return state |
| 117 | } |
| 118 | |
| 119 | // Ready is the render path's check before each render: it waits for a |
| 120 | // running check, then verifies the tree again now. It returns the tree |
| 121 | // to launch with when the health is ok; otherwise "", the health and why. |
| 122 | // Launch verifies the tree once more as it mounts it. |
| 123 | func (h *Health) Ready(ctx context.Context) (tree, health string, err error) { |
| 124 | if h == nil { |
| 125 | return "", wire.ToolMissing, errors.New("render: this runner has no page renderer") |
| 126 | } |
| 127 | if health, err = h.run(ctx); err != nil { |
| 128 | return "", health, err |
| 129 | } |
| 130 | return h.Config.Tree, health, nil |
| 131 | } |
| 132 | |
| 133 | // Ended is told how each render with the tree ended (its error, or |
| 134 | // nil). A sandbox that could not be shown to have ended (ErrNotEnded) |
| 135 | // stops every later render of this process, as a fixture's does: the |
| 136 | // runner needs attention, never another try. |
| 137 | func (h *Health) Ended(err error) { |
| 138 | if h == nil || !errors.Is(err, ErrNotEnded) { |
| 139 | return |
| 140 | } |
| 141 | h.mu.Lock() |
| 142 | before := reported(h.state) |
| 143 | if h.stuck == nil { |
| 144 | h.stuck = err |
| 145 | } |
| 146 | h.state = wire.ToolMissing |
| 147 | h.mu.Unlock() |
| 148 | if before != wire.ToolMissing && h.Changed != nil { |
| 149 | h.Changed() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // tried is a tree whose fixture was rendered and failed (at failed; zero |
| 154 | // while it renders). |
| 155 | type tried struct { |
| 156 | tree os.FileInfo |
| 157 | failed time.Time |
| 158 | } |
| 159 | |
| 160 | func (h *Health) now() time.Time { |
| 161 | if h.Now != nil { |
| 162 | return h.Now() |
| 163 | } |
| 164 | return time.Now() |
| 165 | } |
| 166 | |
| 167 | // reported is a state as reported: missing before the first check. |
| 168 | func reported(state string) string { |
| 169 | if state == "" { |
| 170 | return wire.ToolMissing |
| 171 | } |
| 172 | return state |
| 173 | } |
| 174 | |
| 175 | // kick runs a check in the background unless one is running or Start |
| 176 | // was not called. |
| 177 | func (h *Health) kick() { |
| 178 | h.mu.Lock() |
| 179 | ctx := h.base |
| 180 | if ctx == nil || h.running != nil || ctx.Err() != nil { |
| 181 | h.mu.Unlock() |
| 182 | return |
| 183 | } |
| 184 | done := make(chan struct{}) |
| 185 | h.running = done |
| 186 | h.mu.Unlock() |
| 187 | go h.finish(ctx, done) |
| 188 | } |
| 189 | |
| 190 | // wait waits for a running check to end. |
| 191 | func (h *Health) wait(ctx context.Context) { |
| 192 | for { |
| 193 | h.mu.Lock() |
| 194 | ch := h.running |
| 195 | h.mu.Unlock() |
| 196 | if ch == nil { |
| 197 | return |
| 198 | } |
| 199 | select { |
| 200 | case <-ch: |
| 201 | case <-ctx.Done(): |
| 202 | return |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // run is one check, one at a time: it waits for a running one first. |
| 208 | func (h *Health) run(ctx context.Context) (string, error) { |
| 209 | h.mu.Lock() |
| 210 | for h.running != nil { |
| 211 | ch := h.running |
| 212 | h.mu.Unlock() |
| 213 | select { |
| 214 | case <-ch: |
| 215 | case <-ctx.Done(): |
| 216 | return wire.ToolMissing, ctx.Err() |
| 217 | } |
| 218 | h.mu.Lock() |
| 219 | } |
| 220 | done := make(chan struct{}) |
| 221 | h.running = done |
| 222 | h.mu.Unlock() |
| 223 | return h.finish(ctx, done) |
| 224 | } |
| 225 | |
| 226 | // finish runs the check that holds done (h.running), records its state, |
| 227 | // lets the next check start and tells Changed of a change. |
| 228 | func (h *Health) finish(ctx context.Context, done chan struct{}) (string, error) { |
| 229 | state, err := h.check(ctx) |
| 230 | if err != nil && h.Logger != nil { |
| 231 | h.Logger.Warn("page renderer not ready", "health", state, "err", err) |
| 232 | } |
| 233 | h.mu.Lock() |
| 234 | before := reported(h.state) |
| 235 | h.state, h.running = state, nil |
| 236 | h.mu.Unlock() |
| 237 | close(done) |
| 238 | if before != state && h.Changed != nil { |
| 239 | h.Changed() |
| 240 | } |
| 241 | return state, err |
| 242 | } |
| 243 | |
| 244 | // check verifies the tree and, for a tree that has not been tried, |
| 245 | // renders the fixture page with it. |
| 246 | func (h *Health) check(ctx context.Context) (string, error) { |
| 247 | cfg := h.Config |
| 248 | verify, fixture := h.Verify, h.Fixture |
| 249 | if verify == nil { |
| 250 | verify = release.VerifyRenderer |
| 251 | } |
| 252 | if fixture == nil { |
| 253 | fixture = CheckFixture |
| 254 | } |
| 255 | before, _ := os.Lstat(cfg.Tree) |
| 256 | if err := verify(cfg.Tree, cfg.Pin); err != nil { |
| 257 | return release.RendererHealth(err), err |
| 258 | } |
| 259 | tree, err := os.Lstat(cfg.Tree) |
| 260 | if err != nil || before == nil || !os.SameFile(before, tree) { |
| 261 | return wire.ToolMismatch, errTreeMoved |
| 262 | } |
| 263 | h.mu.Lock() |
| 264 | stuck, passed, wasTried := h.stuck, h.passed != nil && os.SameFile(h.passed, tree), false |
| 265 | for i, t := range h.tried { |
| 266 | if !os.SameFile(t.tree, tree) { |
| 267 | continue |
| 268 | } |
| 269 | // A failure more than FixtureRetry old gets one more try now. |
| 270 | if stuck == nil && !passed && !t.failed.IsZero() && h.now().Sub(t.failed) > FixtureRetry { |
| 271 | h.tried = append(h.tried[:i], h.tried[i+1:]...) |
| 272 | } else { |
| 273 | wasTried = true |
| 274 | } |
| 275 | break |
| 276 | } |
| 277 | if stuck == nil && !passed && !wasTried { |
| 278 | h.tried = append(h.tried, tried{tree: tree}) |
| 279 | } |
| 280 | h.mu.Unlock() |
| 281 | switch { |
| 282 | case stuck != nil: |
| 283 | return wire.ToolMissing, stuck |
| 284 | case passed: |
| 285 | return wire.ToolHealthy, nil |
| 286 | case wasTried: |
| 287 | return wire.ToolMissing, ErrNotChecked |
| 288 | } |
| 289 | err = fixture(ctx, cfg, h.Scratch) |
| 290 | if after, serr := os.Lstat(cfg.Tree); err == nil && (serr != nil || !os.SameFile(tree, after)) { |
| 291 | err = errTreeMoved |
| 292 | } |
| 293 | switch { |
| 294 | case errors.Is(err, ErrNotEnded): |
| 295 | h.mu.Lock() |
| 296 | h.stuck = err |
| 297 | h.mu.Unlock() |
| 298 | return wire.ToolMissing, err |
| 299 | case err != nil && ctx.Err() != nil: |
| 300 | // Cut short (the runner stopping, a render call cancelled): the |
| 301 | // tree was not given its try. |
| 302 | h.mu.Lock() |
| 303 | h.tried = h.tried[:len(h.tried)-1] |
| 304 | h.mu.Unlock() |
| 305 | return wire.ToolMissing, fmt.Errorf("%w: %w", ErrNotChecked, err) |
| 306 | case err != nil: |
| 307 | h.mu.Lock() |
| 308 | h.tried[len(h.tried)-1].failed = h.now() |
| 309 | h.mu.Unlock() |
| 310 | var re *release.RendererError |
| 311 | if errors.As(err, &re) { |
| 312 | return release.RendererHealth(err), fmt.Errorf("%w: %w", ErrNotChecked, err) |
| 313 | } |
| 314 | return wire.ToolMissing, fmt.Errorf("%w: %w", ErrNotChecked, err) |
| 315 | } |
| 316 | h.mu.Lock() |
| 317 | h.passed = tree |
| 318 | h.mu.Unlock() |
| 319 | return wire.ToolHealthy, nil |
| 320 | } |
| 321 | |