Skip to content
quarry Artifact [fad2a5b059]
Login

Artifact [fad2a5b059]

Artifact fad2a5b059b4b598b77612e63f39bd1b711776283045565f808c9deabaed3cf5:


package main

import (
	"bufio"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"craftdesign.group/quarry/internal/zitcli"
)

// What a waiting maintenance task says it is waiting on.
const (
	maintWaitZit    = "store calls busy"
	maintWaitMemory = "memory pressure"
	maintWaitDeploy = "deploy running"
	maintWaitBackup = "backup running"
)

const (
	// memStallPercent is the share of the last 10 s in which some task
	// stalled on memory (PSI "some avg10") above which upkeep waits.
	memStallPercent = 10.0
	// memAvailableFloor is MemAvailable below which upkeep waits.
	memAvailableFloor = 2 << 30
)

// loadSources are the signals maintenance admission reads. A nil probe
// is a signal that is never up.
type loadSources struct {
	zitBusy func() bool
	memory  func() bool
	deploy  func() bool
	backup  func() bool
}

// reasons names every signal that is up, in a fixed order.
func (s loadSources) reasons() []string {
	var out []string
	for _, p := range []struct {
		up   func() bool
		name string
	}{{s.zitBusy, maintWaitZit}, {s.memory, maintWaitMemory}, {s.deploy, maintWaitDeploy}, {s.backup, maintWaitBackup}} {
		if p.up != nil && p.up() {
			out = append(out, p.name)
		}
	}
	return out
}

// productionLoad reads the real signals: the store gate's interactive
// pressure, /proc, the deploy lock. There is no backup marker: backup.sh
// takes only a per-repository lock, and nothing global says one is
// running, so that probe is a hook that is false until one exists.
func productionLoad() func() []string {
	lock := os.Getenv("QUARRY_DEPLOY_LOCK")
	if lock == "" {
		lock = "/run/quarry-deploy.lock"
	}
	s := loadSources{
		zitBusy: zitcli.Pressured,
		memory:  func() bool { return memoryPressure("/proc") },
		deploy:  func() bool { return deployRunning(lock) },
		backup:  func() bool { return false },
	}
	return s.reasons
}

// memoryPressure reports whether the box is short of memory: processes
// stalling on it (/proc/pressure/memory, where the kernel has PSI) or
// MemAvailable under the floor. Either file absent is that signal being
// down, so a non-Linux host never waits.
func memoryPressure(proc string) bool {
	if f, err := os.Open(filepath.Join(proc, "pressure", "memory")); err == nil {
		defer f.Close()
		sc := bufio.NewScanner(f)
		for sc.Scan() {
			fields := strings.Fields(sc.Text())
			if len(fields) < 2 || fields[0] != "some" {
				continue
			}
			for _, kv := range fields[1:] {
				if v, ok := strings.CutPrefix(kv, "avg10="); ok {
					if n, err := strconv.ParseFloat(v, 64); err == nil && n > memStallPercent {
						return true
					}
				}
			}
		}
	}
	if f, err := os.Open(filepath.Join(proc, "meminfo")); err == nil {
		defer f.Close()
		sc := bufio.NewScanner(f)
		for sc.Scan() {
			fields := strings.Fields(sc.Text())
			if len(fields) >= 2 && fields[0] == "MemAvailable:" {
				if kb, err := strconv.ParseUint(fields[1], 10, 64); err == nil && kb<<10 < memAvailableFloor {
					return true
				}
			}
		}
	}
	return false
}