daemon

package
v0.63.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 29 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 daemon runs drives for one user, outside any session. Each drive is its own `f10 drive --foreground` child in the client's directory and environment, so leases and crash recovery stay the drive's own.

Index

Constants

View Source
const (
	ReasonUpgrade = "upgrade" // the next daemon resumes it
	ReasonStopped = "stopped" // f10 daemon stop: it waits for its next run
	ReasonResumed = "resumed" // this daemon started it again
)

Why a drive exited ExitDrained or ExitResume.

View Source
const APIVersion = 1

APIVersion is the control-plane API version this binary serves and calls; it rises only when a v2 package lands.

View Source
const ExitDrained = 6

ExitDrained is a drive's exit when it stopped for an upgrade, to be resumed by the next daemon.

View Source
const ExitNothingWatched = 7

ExitNothingWatched is a pass's exit when the project has no task left to watch: the daemon stops watching it.

View Source
const ExitResume = 8

ExitResume is a drive's exit when an answer came as it stopped: the daemon that runs it starts it again with --resume.

View Source
const NumberFileEnv = "F10_DRIVE_FILE"

NumberFileEnv names the file a drive the daemon runs writes its record number to, once the record is open.

View Source
const ResumeEnv = "F10_DRIVE_RESUMES"

ResumeEnv is set on every drive a daemon runs that starts an ExitResume again. A drive under an older daemon, which lacks it, names the --resume command instead of exiting for a restart that never comes.

View Source
const SocketURL = "http://f10-daemon"

SocketURL is the base every call on the socket goes to; the host is a placeholder, since the transport dials the socket whatever it names.

View Source
const Supported = true

Supported reports whether this platform runs a daemon; elsewhere drive runs in the foreground.

Variables

View Source
var ErrNotRunning = errors.New("no f10 daemon is running")

ErrNotRunning is a socket nobody listens on.

View Source
var ErrRunning = errors.New("an f10 daemon is already running")

ErrRunning is a second daemon finding the first one's lease held.

View Source
var ErrStopping = errors.New("the daemon is stopping: run the command again")

ErrStopping is a drive refused because a stop drains the daemon.

Functions

func APIError added in v0.62.0

func APIError(info Info, version string) error

APIError is a daemon kept by the release rule whose API this client does not speak: the two say so instead of failing on a decode.

func DriveEnv added in v0.58.0

func DriveEnv(environ, extra []string) []string

func ExitPath

func ExitPath(log string) string

ExitPath is the file holding a drive log's exit, once its process ended.

func FindLog

func FindLog(dir, root string, n int) string

FindLog is drive n's newest log under root, from the index; "" for none.

func IsLocal added in v0.62.0

func IsLocal(req connect.AnyRequest) bool

IsLocal reports whether req came over the unix socket, or from this process: a unix socket's peer has no network address.

func LocalEnv added in v0.62.0

func LocalEnv(req connect.AnyRequest, env []string) []string

LocalEnv is the environment a request passes, honored only from the unix socket; a request from any other listener runs under the daemon's session.

func LogPath

func LogPath(dir string) string

LogPath is the daemon's own log.

func Newer

func Newer(a, b string) bool

Newer reports a strictly newer than b, both release versions (`0.46.0`, `v0.46.0`); a development build is newer than nothing, and nothing is newer than it, so it is replaced only by `f10 daemon stop`.

func RunDir

func RunDir() string

RunDir holds the socket, the locks, the logs and the drained runs: F10_RUN_DIR, else $XDG_RUNTIME_DIR/f10, else ~/.f10/run.

func SocketHTTP added in v0.62.0

func SocketHTTP(dir string) *http.Client

SocketHTTP is an HTTP client whose every request goes to the socket in dir.

func SocketPath

func SocketPath(dir string) string

SocketPath is where the daemon listens.

func Stop added in v0.62.0

func Stop(ctx context.Context, dir string, drain, upgrade bool) error

Stop stops the daemon in dir: drain lets each drive reach its next tick, and upgrade hands the drained drives to the next daemon.

func WatchProject added in v0.62.0

func WatchProject(ctx context.Context, runDir, root, dir string, env []string) (string, error)

WatchProject hands the project at root, its main checkout dir, to the daemon's watch passes, and returns the pass log.

func WithCaller added in v0.62.0

func WithCaller(ctx context.Context, c Caller) context.Context

WithCaller marks ctx as a call by c, which the tailnet's token check does so Hello can say what the caller may call without this package knowing tokens.

Types

type Caller added in v0.62.0

type Caller struct {
	Token      string
	Scopes     []string
	Procedures []string
}

A Caller is who makes a call on the tailnet: the device token's name, its scopes and the procedures they allow. A call with none in its context came over the unix socket, where every procedure is allowed.

func CallerOf added in v0.62.0

func CallerOf(ctx context.Context) (Caller, bool)

CallerOf is the caller WithCaller set, and false for a local call.

type Client added in v0.62.0

type Client struct {
	Dir    string
	Daemon f10v1connect.DaemonServiceClient
	Runs   f10v1connect.RunServiceClient
}

A Client calls the daemon listening in a run dir.

func Dial added in v0.62.0

func Dial(dir string) *Client

Dial is the client of the daemon in dir; nothing is dialled until a call.

type DriveCall added in v0.62.0

type DriveCall struct {
	Args   []string // the drive's arguments, as f10 drive takes them
	Dir    string   // where the drive was typed
	Env    []string // the allowlisted environment
	Resume int      // the record --resume names
}

A DriveCall is one drive for the daemon to run, or to attach to.

func ResumeCall added in v0.62.0

func ResumeCall(dir string, env []string, n int) DriveCall

ResumeCall is the call that picks drive n up again.

type DriveSpec added in v0.62.0

type DriveSpec struct {
	Argv   []string
	Dir    string
	Root   string
	Env    []string
	Resume int
}

A DriveSpec is one drive to run: its arguments, where it was typed, its project's storage root, its environment, and the record a resume names.

type Ensurer

type Ensurer struct {
	Dir     string
	Version string
	// Follow takes any daemon running, whatever its version: a client
	// following a drive into the daemon that resumed it.
	Follow bool
	// Spawn starts a daemon detached; by default `<exe> daemon` in its own
	// session, its output to the daemon log.
	Spawn func(ctx context.Context) error
	// Draining is told when an older daemon is being replaced, Kept when a
	// daemon this client does not replace runs another version.
	Draining, Kept func(running string)
	// StartWait bounds how long a new daemon has to listen; DrainWait how
	// long an old one has to drain.
	StartWait, DrainWait time.Duration
}

An Ensurer finds this user's daemon, or starts one.

func (*Ensurer) Ensure

func (e *Ensurer) Ensure(ctx context.Context) (Info, error)

Ensure returns a daemon listening, starting one when none does. Only a strictly newer client replaces a running one, so two versions never take turns; the start lease keeps two clients at once to one daemon. A daemon kept whose API this client does not speak is an error naming both.

type Exit

type Exit struct {
	Code   int
	Reason string
	Drive  int
}

An Exit is how a drive's process ended: its code, why it drained, and its record number, 0 when it never opened one.

func ReadExit

func ReadExit(log string) (Exit, bool)

ReadExit is how a drive's log ended, false while it runs.

type Info added in v0.62.0

type Info struct {
	Version  string
	API      int
	PID      int
	Started  time.Time
	Stopping bool
	Legacy   bool
}

Info is what Hello answers: which daemon runs, and whether it is stopping. Legacy is a daemon of a release before the Connect API, reached through its JSON protocol only to replace it.

func Hello added in v0.62.0

func Hello(ctx context.Context, dir, version string) (Info, error)

Hello asks the daemon in dir who it is. A daemon serving another API version answers, and APIError says so; one that speaks only the old JSON protocol answers through it, marked Legacy.

type RemoteListener added in v0.62.0

type RemoteListener interface {
	// Addr is the address to bind and whether the user's setting is on;
	// with no address, the third value says why none is wanted or possible.
	Addr() (addr string, on bool, why string)
	Listen(ln net.Listener) net.Listener
	Handler(h http.Handler) http.Handler
}

A RemoteListener is the daemon's listener on the tailnet: the address to bind, the peers it accepts and the check in front of the API. The daemon command builds it, so this package knows nothing of tokens.

type RemoteState added in v0.62.0

type RemoteState struct {
	Enabled     bool
	Addr        string // bound; "" when none is
	Reason      string // why none is bound
	URL         string // the web client's address while bound
	HTTPS       bool
	HTTPSReason string // why the listener serves plain HTTP
}

RemoteState is the tailnet listener as the daemon last reconciled it.

type RemoteTLS added in v0.62.0

type RemoteTLS interface {
	// TLS is the config to serve with and the host name its certificate
	// names; a nil config comes with why there is none, and the listener
	// serves plain HTTP meanwhile.
	TLS() (cfg *tls.Config, host string, why string)
}

A RemoteTLS listener serves HTTPS once it holds a certificate, because a browser installs a web app and shows notifications only over HTTPS.

type Run

type Run struct {
	Root  string    `json:"root"`
	Dir   string    `json:"dir"`
	Drive int       `json:"drive,omitempty"` // 0 until the drive printed its number
	PID   int       `json:"pid"`
	Since time.Time `json:"since"`
	Log   string    `json:"log"`
}

A Run is one drive process the daemon supervises.

type Server

type Server struct {
	Dir     string // the run dir
	Version string
	// Child is the command a drive's argv runs as: by default the daemon's
	// own binary, `drive --foreground <argv>`.
	Child func(argv []string) (string, []string)
	// Pass is the command one watch pass runs as in a project's main
	// checkout: by default the daemon's own binary, `watch --pass`.
	Pass       func(dir string) (string, []string)
	WatchEvery time.Duration // how often each watched project gets a pass
	Idle       time.Duration // no drive, no watched project and no call this long: exit
	DrainWait  time.Duration // how long a drain waits for drives before SIGTERM
	Tick       time.Duration // how often idleness is checked
	Log        *log.Logger
	// Handler is the API the socket serves, built over this server; the
	// daemon command passes the Connect handlers, a test its own.
	Handler func(s *Server) http.Handler
	// Key is the attach key of a drive's arguments, "" for a drive that must
	// start its own process (one given answers).
	Key func(argv []string) string
	// Remote is the tailnet listener, nil for the socket alone. While its
	// setting is on the daemon never idles out.
	Remote RemoteListener
	// Service is a daemon launchd or systemd keeps: it never idles out, and
	// while another daemon holds the lease it waits to take over.
	Service bool
	// contains filtered or unexported fields
}

A Server supervises drive processes for one user.

func (*Server) Info added in v0.62.0

func (s *Server) Info() Info

Info is who this daemon is.

func (*Server) Remember added in v0.62.0

func (s *Server) Remember(env []string)

Remember keeps env as the newest local session, when it comes from inside one, as a watch or a drive call does.

func (*Server) ResumeDrive added in v0.62.0

func (s *Server) ResumeDrive(root, dir string, env []string, n int) (Started, error)

ResumeDrive starts drive n again with --resume, as a stopped drive's answer does.

func (*Server) RunDrive added in v0.62.0

func (s *Server) RunDrive(spec DriveSpec) (Started, error)

RunDrive attaches to the drive already running this record, else starts it, and returns its log.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve holds the daemon's lease, listens, and supervises until it is idle, stopped, or ctx ends (a drain).

func (*Server) SessionEnv added in v0.62.0

func (s *Server) SessionEnv() []string

SessionEnv is the newest local session's environment, nil before any client registered one.

func (*Server) Status added in v0.62.0

func (s *Server) Status() Status

Status is what this daemon runs and watches.

func (*Server) Stop added in v0.62.0

func (s *Server) Stop(drain, upgrade bool)

Stop stops the daemon: drain lets each drive reach its next tick, and upgrade hands the drained ones to the next daemon. A second stop can only cut a drain short.

func (*Server) WatchProject added in v0.62.0

func (s *Server) WatchProject(root, dir string, env []string) (string, error)

WatchProject adds or refreshes a project, newest env winning, and starts a pass for it now, so a task just started is seen without waiting a tick.

type Started added in v0.62.0

type Started struct {
	Log    string
	Attach bool
	Drive  int
}

Started is a drive the daemon runs: its log, and whether the call joined one already running.

func RunDrive added in v0.62.0

func RunDrive(ctx context.Context, dir string, c DriveCall) (Started, error)

RunDrive asks the daemon in dir to run the drive, or to attach to it.

type Status added in v0.62.0

type Status struct {
	Info

	Runs    []Run
	Watches []Watch
	Remote  RemoteState
}

Status is what the daemon runs and watches, with who it is.

func GetStatus added in v0.62.0

func GetStatus(ctx context.Context, dir, version string) (Status, error)

GetStatus is who the daemon in dir is, and what it runs and watches.

type Watch

type Watch struct {
	Root string    `json:"root"`
	Dir  string    `json:"dir"`           // the main checkout
	Last time.Time `json:"last,omitzero"` // when the last pass ended
	Exit int       `json:"exit"`          // its exit code
	Log  string    `json:"log"`           // the project's pass log
}

A Watch is one project the daemon watches for tasks no drive owns: a `watch --pass` in its main checkout every few minutes.

Jump to

Keyboard shortcuts

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