serverorder

package
v0.207.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package serverorder is the laptop's half of a ready server (ADR-114): a server Caprock sells, created for the buyer and set up with the same script New server writes (internal/serverscript), which joins this computer with a key made here for that one order.

On the user's click (Settings → Computers → Add a computer → Get a ready server) the daemon makes an order id and a server key that lives 24 hours (peers.OrderJoinTTL), renders the setup script with that key inside, and registers the order with caprock.dev (Register): the order id, the size, how often it is paid (monthly or yearly) and the script, nothing else. caprock.dev keeps the script sealed until the server is created and deletes it then. It answers the Stripe payment link for that order, which the screen opens. Afterwards the daemon reads the order's status (Read) while the Computers screen shows it: whether it is paid, being created, booting, running, failed or stopping on a date.

The orders are kept in a 0600 file in the data directory (Store), with the kept form of each one's key (peers.KeptJoinKey, the secret's hash only), so a server that boots while this daemon restarts still finds its key open.

Index

Constants

View Source
const (
	// BucketSpan is one bucket; BucketsKept a week of them.
	BucketSpan  = 10 * time.Minute
	BucketsKept = int(7 * 24 * time.Hour / BucketSpan)
)
View Source
const (
	Monthly = "month"
	Yearly  = "year"
)

How often a ready server is paid: the payment link caprock.dev answers.

View Source
const (
	DefaultBase = "https://caprock.dev"
	BaseEnv     = "CAPROCK_SERVER_ORDER_URL"
)

DefaultBase is caprock.dev. BaseEnv replaces it — for a preview stand or a test, which must never register an order with the real site.

View Source
const (
	WaitingPayment = "waiting_payment" // registered, not paid yet
	Creating       = "creating"        // paid; the server is being created
	Booting        = "booting"         // created; not running with an address yet
	Running        = "running"         // running: the setup script is installing Caprock
	Failed         = "failed"          // Reason says why, in words for the buyer
	Stopped        = "stopped"         // the subscription ended; the server is stopped
	Unknown        = "unknown"         // caprock.dev has no such order (never paid, and expired)
)

The statuses caprock.dev answers (Remote.Status).

View Source
const File = "server-orders.json"

File is the orders' file in the data directory, mode 0600.

Variables

View Source
var (
	// ErrNotOnSale is caprock.dev not taking orders: switched off, or no
	// payment link for that size and period.
	ErrNotOnSale = errors.New("ready servers are not on sale yet")
	// ErrUnreachable is caprock.dev not answering.
	ErrUnreachable = errors.New("could not reach caprock.dev")
)

Errors the screen tells apart.

View Source
var Sizes = []Size{
	{ID: "24", Memory: "24 GB", PriceUSD: 39, YearUSD: 390},
	{ID: "48", Memory: "48 GB", PriceUSD: 59, YearUSD: 590},
}

Sizes are the two ready servers and their prices, the one place the app states them: the Stripe prices of the products "Caprock Server — 24 GB" (prod_Vi0ETgAutCfxdn) and "Caprock Server — 48 GB" (prod_Vi0FcXDgNS1GIS). Monthly, $39 and $59, set by the owner on 2026-11-28; yearly, $390 and $590 (two months free), created in Stripe on 2026-11-29 (ADR-114; caprock-web docs/servers.md). A change of price in Stripe changes these in the same commit as the docs that state them.

Functions

func Base

func Base() string

Base is where orders are registered and read.

func CheckPayURL

func CheckPayURL(raw, size, id string) error

CheckPayURL holds caprock.dev's payment link to Stripe's pages and to this order's reference, so the screen never opens anything else.

func GB added in v0.207.0

func GB(b uint64) string

GB is memory in binary units, as Activity Monitor and the memory popover show it: "9.1 GB", "14 GB".

func NewID

func NewID() (string, error)

NewID is an order id: 128 random bits, hex. It is the only thing that reads the order's status at caprock.dev, so it is unguessable.

func OnSale added in v0.207.0

func OnSale(ctx context.Context, hc *http.Client, base string) bool

OnSale asks caprock.dev whether ready servers are on sale: a plain GET of /api/server-order — no body, no query string, no cookie, no identifier. Only {"on_sale": true} with a 200 is a yes; any other answer — a site that does not answer this, switched off, unreachable — is no, so a banner offering a ready server never shows while they are not on sale. Asked only when the banner would otherwise show (internal/daemon/serveradvice.go), at most once a week.

func PeriodOr added in v0.206.0

func PeriodOr(p string) string

PeriodOr is p, or Monthly for "" (an order kept before yearly existed).

func Reference

func Reference(size, id string) string

Reference is the payment link's client_reference_id for an order: "server-24-<id>", monthly or yearly alike: caprock.dev keeps the period with the order, so the reference never changed. Stripe takes letters, digits, "-" and "_" there, up to 200 characters, and silently drops anything else (so no ".").

func Register

func Register(ctx context.Context, hc *http.Client, base, id, size, period, setup string) (string, error)

Register sends {order_id, size, period, setup} to caprock.dev and answers the order's Stripe payment link, monthly or yearly as period says. The error never carries the script.

func ShortDays added in v0.207.0

func ShortDays(buckets []LoadBucket, now time.Time) int

ShortDays is on how many of the last suggestWindow days, each the 24 hours ending a whole number of days before now, memory ran short at least once: a bucket with shortBucket or more short minutes.

func ShortMinute added in v0.207.0

func ShortMinute(critical, swapGrew bool, used, total uint64) bool

ShortMinute is whether one minute's reading is memory running short: the OS says its pressure is critical, swap grew by a quarter of a gigabyte since the minute before, or 95% of memory is in use (Windows, which keeps no pressure level, says it only this way).

func Suggest added in v0.207.0

func Suggest(buckets []LoadBucket, now time.Time) (bool, string)

Suggest says whether this computer was overloaded enough in the last week to suggest a ready server, and the measured reason in words: "Memory ran short on 4 of the last 7 days."

func ValidID

func ValidID(id string) bool

ValidID says id is one NewID made.

func ValidPeriod added in v0.206.0

func ValidPeriod(p string) bool

ValidPeriod says p is Monthly or Yearly.

Types

type Advice added in v0.207.0

type Advice struct {
	Stats LoadStats `json:"stats"`
	// Recommend is a Size's ID, "" when there is too little to go on.
	Recommend string `json:"recommend,omitempty"`
	// Reason is one line in plain words; NoAdvice says why there is none.
	Reason   string `json:"reason,omitempty"`
	NoAdvice string `json:"no_advice,omitempty"`
}

Advice is a recommended size and why, or why none.

func Advise added in v0.207.0

func Advise(buckets []LoadBucket, now time.Time, here string) Advice

Advise recommends a size from the last week of buckets. here names the computer in the reason: "Mac", "computer".

type LoadBucket added in v0.207.0

type LoadBucket struct {
	// At is the bucket's start, unix ms.
	At int64 `json:"at"`
	// Minutes is how many minutes of it were sampled: none while the
	// computer slept or Caprock was not running.
	Minutes int `json:"m"`
	// Agents is the most agents running at once; AgentBytes the most memory
	// they held together (each agent's process tree, its own memory).
	Agents     int    `json:"a"`
	AgentBytes uint64 `json:"b"`
	// Total is the machine's memory.
	Total uint64 `json:"t,omitempty"`
	// Short is how many sampled minutes memory ran short (ShortMinute).
	Short int `json:"s,omitempty"`
}

LoadBucket is BucketSpan of this computer's load.

type LoadStats added in v0.207.0

type LoadStats struct {
	// Days is how many days the history covers (up to 7); Minutes how
	// many minutes of them were sampled.
	Days    int `json:"days"`
	Minutes int `json:"minutes"`
	// PeakAgents and TypicalAgents are the most agents at once and the
	// median of each bucket's most while any ran.
	PeakAgents    int    `json:"peak_agents"`
	TypicalAgents int    `json:"typical_agents"`
	PeakBytes     uint64 `json:"peak_agent_bytes"`
	Total         uint64 `json:"total,omitempty"`
	// ShortTimes is how many episodes memory ran short; ShortMinutes how
	// many minutes in all.
	ShortTimes   int `json:"short_times"`
	ShortMinutes int `json:"short_minutes"`
}

LoadStats is what the history says, as the screen states it.

func Stats added in v0.207.0

func Stats(buckets []LoadBucket, since, now time.Time) LoadStats

Stats sums the buckets since a moment.

type Order

type Order struct {
	ID   string `json:"id"`
	Size string `json:"size"`
	// Period is "month" or "year", as the buyer chose; absent on an order
	// kept before yearly existed, which was monthly (PeriodOr).
	Period string `json:"period,omitempty"`
	// PayURL is the order's Stripe payment link, to open again until it is paid.
	PayURL string `json:"pay_url,omitempty"`
	// CreatedAt and ExpiresAt are unix milliseconds; ExpiresAt is the key's.
	CreatedAt int64 `json:"created_at"`
	ExpiresAt int64 `json:"expires_at"`
	// Key reopens the order's key after a restart, until the server joins
	// or the key expires; then it is dropped.
	Key *peers.KeptJoinKey `json:"key,omitempty"`
	// Machine is the computer that joined with the key, as GET /v1/machines
	// lists it.
	Machine string `json:"machine,omitempty"`
	// KeyLost says the key stopped waiting without a join (withdrawn, burned
	// by wrong tries, or not reopened): the server cannot join with it.
	KeyLost bool `json:"key_lost,omitempty"`
	// Remote is what caprock.dev said last, and ReadAt when (unix ms).
	Remote Remote `json:"remote"`
	ReadAt int64  `json:"read_at,omitempty"`
}

Order is one ready server ordered from this computer.

type Registered

type Registered struct {
	PayURL string `json:"pay_url"`
}

Registered is caprock.dev's answer to a registration.

type Remote

type Remote struct {
	Status string `json:"status"`
	Size   string `json:"size,omitempty"`
	// Period is "month" or "year": the subscription's own interval once
	// Stripe has said it. A site from before yearly sends none.
	Period string `json:"period,omitempty"`
	Reason string `json:"reason,omitempty"`
	// StopsAt is unix seconds: the subscription is cancelled and the server
	// stops then.
	StopsAt int64 `json:"stops_at,omitempty"`
}

Remote is GET /api/server-order/<id>'s answer.

func Read

func Read(ctx context.Context, hc *http.Client, base, id string) (Remote, error)

Read is the order's status at caprock.dev. A 404 is Unknown, not an error.

type Size

type Size struct {
	// ID is what an order and the payment link's reference carry: "24", "48".
	ID string `json:"id"`
	// Memory is what the screen names the size by.
	Memory string `json:"memory"`
	// PriceUSD is the monthly Stripe price, US dollars a month.
	PriceUSD int `json:"price_usd"`
	// YearUSD is the yearly Stripe price, US dollars a year.
	YearUSD int `json:"year_usd"`
}

Size is one server Caprock sells.

func SizeByID

func SizeByID(id string) (Size, bool)

SizeByID is the size an id names.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store keeps the orders in File.

func Open

func Open(dir string) *Store

Open is the store in dir ("" keeps nothing on disk: tests).

func (*Store) List

func (s *Store) List() ([]Order, error)

List is every order, newest first.

func (*Store) Update

func (s *Store) Update(fn func([]Order) []Order) error

Update changes the orders under the store's lock and writes them back.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL