instance

package
v0.8.0-alpha Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package instance keeps to one server per data folder. The first start takes a lock that the system lets go of when the process ends, however it ends, so a crash never leaves anything behind to block the next start. A second start finds the first one through instance.json and its health check, and says so or asks what to do.

Index

Constants

View Source
const (
	TitleRunning  = "WittyMediaServer is already running"
	ButtonOpen    = "Open WittyMediaServer"
	ButtonAnother = "Start another server…"
	ButtonExit    = "Exit"

	TitleAnother = "Start another server"
	ButtonStart  = "Start it"
	ButtonChoose = "Choose another folder…"
	ButtonBack   = "Back"
)

The words of the question, kept here so the tray app and the server say the same.

View Source
const (
	// LockFile is held by the running server for as long as it runs. It's never removed:
	// removing a lock file is how two processes end up each holding one.
	LockFile = "instance.lock"
	// InfoFile says where the running server is. No secrets: it's only how a second
	// start finds the first.
	InfoFile = "instance.json"
	// ExitRunning is the exit code of a start that found its data folder in use. Service
	// managers are told not to restart on it.
	ExitRunning = 3
	// FirstOtherPort is where the search for a second server's port starts.
	FirstOtherPort = 9798
)

Variables

View Source
var (
	// ErrBusy: another process holds the data folder.
	ErrBusy = errors.New("another server is using this data folder")
	// ErrUnsupported: the folder's file system can't lock (some network shares and
	// container file sharing). The server starts anyway, unguarded.
	ErrUnsupported = errors.New("this folder can't be locked")
)

Functions

func AnotherText

func AnotherText(folder string, port int) string

AnotherText explains what a second server is before it starts.

func FreePort

func FreePort(host string, from int, skip ...int) int

FreePort finds the first port from `from` that host can listen on, skipping those in skip. It returns 0 when none of the next hundred are free.

func HealthID

func HealthID(ctx context.Context, base string) (id string, ok bool)

HealthID asks a server at base for its instance id. A WittyMediaServer older than this answers with an empty one; ok says something answered as a WittyMediaServer does.

func Held

func Held(dir string) bool

Held says another process holds dir, for tools that mustn't change its files under a running server.

func HeldBy

func HeldBy(dir string) (pid int, running, known bool)

HeldBy says whether another process is running as a server on dir, and its process number when that's known: this process holding it doesn't count, for the repairs a server runs on itself. known is false for a folder no server of this version has used, where a tool has to fall back on what it did before.

func Holder

func Holder(dir string) int

Holder is the process number of the server holding dir, or 0 when none does or it can't be told.

func Line

func Line(r Running, dir string) string

Line is the one line a start without a desktop prints before it exits.

func NotAnsweringText

func NotAnsweringText(r Running) string

NotAnsweringText is said when the server on the folder doesn't answer.

func PrepareAnother

func PrepareAnother(args []string, listen string, d Decision) ([]string, error)

PrepareAnother gets the second server's folder ready and returns args to start it with. It has a config file of its own in its folder: the first server's wms.json, or WMS_* variables, can name a cache, transcode, artwork or log folder outside the data folder, and two servers must never share those. That file is also how it's started again by hand: wittymediaserver -config <folder>/wms.json.

func RunningText

func RunningText(r Running, now time.Time) string

RunningText is the first question's text.

func SameFolder

func SameFolder(a, b string) bool

SameFolder says two paths are one folder.

func SuggestFolder

func SuggestFolder(dir string) string

SuggestFolder is the folder offered for a second server: beside dir, named after it, and not in use by a server already.

Types

type Ask

type Ask func(title, text string, buttons []string) int

Ask shows a question with buttons and returns the index of the one chosen, or -1 when the question was closed (Escape). The programs pass desktop.Ask; tests pass a fake.

type Choice

type Choice int

Choice is what the person chose when their server was already running.

const (
	ChoiceOpen    Choice = iota // open the running one in the browser (the default)
	ChoiceAnother               // start a second server, on its own folder and port
	ChoiceExit
)

type Decision

type Decision struct {
	Choice Choice
	Folder string // ChoiceAnother: the second server's data folder
	Port   int    // and its port
}

Decision is what to do.

func Decide

func Decide(q Question) Decision

Decide asks what to do about the server already running on q.Dir. Escape or closing the window means Exit, as the design says; Back returns to the first question.

type Info

type Info struct {
	PID      int       `json:"pid"`
	Started  time.Time `json:"started"`
	Version  string    `json:"version"`
	URLs     []string  `json:"urls"`
	Instance string    `json:"instance"`
	// Service: started by a service manager, which the tray app attaches to rather than asking.
	Service bool `json:"service,omitempty"`
}

Info is what instance.json holds.

func Read

func Read(dir string) (Info, error)

Read reads instance.json.

type Lock

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

Lock is this process's hold on a data folder.

func Acquire

func Acquire(dir string) (*Lock, error)

Acquire takes the data folder for this process. ErrBusy means another one has it.

func AcquireWait

func AcquireWait(dir string, wait time.Duration) (*Lock, error)

AcquireWait is Acquire for a start that follows a restart: the process before this one may still be closing, so a busy folder gets up to wait.

func (*Lock) ID

func (l *Lock) ID() string

ID is this run's own random id, which /api/health gives out so that a second start knows it's talking to this server and not some other program on the port.

func (*Lock) Publish

func (l *Lock) Publish(info Info) error

Publish writes instance.json once the server is listening. Its Instance is this lock's.

func (*Lock) Release

func (l *Lock) Release()

Release lets go of the folder, at the very end of a run.

type Outcome

type Outcome struct {
	Code   int      // exit with this, unless one of these is set:
	Again  []string // start again with these arguments: a second server
	Attach bool     // the tray app: show the service that's running there
}

Outcome is what a start that found its data folder in use does next.

func WhenRunning

func WhenRunning(dir, listen string, args []string, interactive, attachToService bool) Outcome

WhenRunning handles a start that found dir in use, for the server and the tray app alike: one line when nobody is there to answer, the question when someone is. listen is the address this start would have used. Only the tray app attaches to a service.

type Question

type Question struct {
	Running Running
	Dir     string // the data folder that's in use
	Ask     Ask
	// Pick chooses a folder. Nil leaves only the one suggested.
	Pick func() (string, error)
	// FreePort finds a port for the second server; 0 means none.
	FreePort func() int
	// InUse says a folder has a server already. Held, unless a test says otherwise.
	InUse func(dir string) bool
	// Suggest is the folder offered. SuggestFolder, unless a test says otherwise.
	Suggest func(dir string) string
	Now     time.Time
}

Question is everything Decide needs.

func DesktopQuestion

func DesktopQuestion(r Running, dir, listen string) Question

DesktopQuestion is the question as the programs ask it: in the system's own dialogs, with its folder picker, and a port free beside the running one.

type Running

type Running struct {
	Info
	// URL answered the health check with this server's id.
	URL string
	// Answering: it did, within the time given. Otherwise it's starting, or hung.
	Answering bool
}

Running is what a second start found.

func Find

func Find(ctx context.Context, dir string, wait time.Duration) Running

Find looks for the server holding dir, for up to wait. It returns what it knows even when that server doesn't answer.

Jump to

Keyboard shortcuts

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