Operator conventions
This page is for the operator, the person who runs the binary, not
the one who writes it. Every program built on
gonsole
follows these rules. The examples use a program called myapp.
With no command, the program lists every command and exits 0. A
program can be built to run serve instead. The listing looks like
this:
myapp Version 1.4.0
Usage: myapp <command> [flags] [arguments]
Every command answers -h. A command that offers -json answers one JSON document. A command that offers -yes is a dry run until -yes.
Available commands: check check every setting, every plugin and every command name help print the help of one command list list every command serve run the server version print the version report report:create create a report report:list list every reportA program lists serve, migrate and seed only when it offers
them. Every command answers -h with its help page. A line that
holds -h only prints help, even with -yes on it.
Reading the answer
Section titled “Reading the answer”A command prints its answer on stdout. With -json, on a command
that offers it, the answer is one JSON document for scripts to read.
Progress, warnings and errors go to stderr, and every error line
starts with myapp:.
| Exit code | Means |
|---|---|
| 0 | the command finished, printed help, or made a dry run that changed nothing |
| 1 | the command ran and failed, a panic included |
| 2 | the program could not read the command line |
Each of these lines exits 2:
$ myapp reprotmyapp: unknown command "reprot", run "myapp list" to see every command$ myapp reportmyapp: unknown command "report", want report:create or report:list$ myapp report:list extramyapp: report:list takes no arguments, got 1$ myapp report:create Q3myapp: report:create wants -owner <email>The last two also print the help page on stderr, left out here. The last one names a flag the command cannot run without. Pass it with a value. An empty value or spaces alone count as missing, and nothing runs until you set it.
A 2 with no myapp: line is different. It is a crash the program
could not catch, such as a stack overflow or a panic in background
work the command started.
Flags and arguments may come in any order. Everything after -- is
an argument, even when it starts with a dash, so a title like -Q3
goes after it.
Changing things safely
Section titled “Changing things safely”A command that offers -yes, such as report:create, is a dry run
until you pass it. It prints what it would change, writes nothing,
and ends with myapp: dry run, nothing changed, pass -yes to apply.
A command whose help page lists no -yes has no dry run.
A command that needs a permission wants -as <email>, the account
you act as. The program checks that account before it runs, dry runs
included, and records who applied the change. When the listing shows
account:records, that command lists who applied which change,
newest first. A missing or blank -as exits 2, and an account
without the permission exits 1. An account command also exits 1, dry
runs included, when the role it gives, or the role of the account it
changes, carries a permission your own role lacks. It does the same
when you try to disable your own account or change your own role.
If recording fails, the run exits 1 even though the change was made.
Check before you run it again.
account:enable also exits 1, dry runs included, on an account that
holds no role or a role the program no longer declares. Give it a
declared role with account:role first. When someone changes an
account’s role between the check and the write, account:role,
account:disable and account:enable exit 1 and change nothing.
Run the command again.
An old spelling with a space may still run, after a warning such as
myapp: "report create" is deprecated, use "report:create".
Running it in production
Section titled “Running it in production”Before you deploy a new release, run myapp check with its
settings. It checks every setting, plugin and command name without
starting the server. On a problem it exits 1 and names it, such as
myapp: MYAPP_REGION is required.
Run myapp migrate before a release that changes the database
schema. It applies every change at once and has no dry run. Never
run myapp seed -yes against production, because seed stores demo
data.
The first Ctrl-C, or the SIGTERM a deployment sends, asks the
command to stop. serve then gives running requests time to finish,
as Serving explains. A second Ctrl-C ends
the program at once.
Processor and memory limits
Section titled “Processor and memory limits”Go reads GOMAXPROCS and GOMEMLIMIT before your program starts, so
a line for either in a .env file the program loads itself does
nothing. Set MYAPP_MAX_PROCS and MYAPP_MEMORY_LIMIT_MB instead.
gonsole.Main reads them before any command runs and hands them to
Go:
MYAPP_MAX_PROCSis how many processors run Go code at once, from 1 to the number of CPUs. Go still starts a thread for each call that waits on the system, so it lowers the thread count without capping it.MYAPP_MEMORY_LIMIT_MBis the memory in megabytes Go tries to stay under, from 1 to 1073741824. It is a soft limit. Go collects garbage harder as the program nears it, and a program that holds more keeps running while the collector works almost without pause. Set it well above what the program uses at rest.
A setting you leave unset leaves Go its own default, which follows
GOMAXPROCS, GOMEMLIMIT and the container’s CPU quota. A set one
only ever lowers what Go holds, so the tighter of the two wins. Once
MYAPP_MAX_PROCS takes effect, Go no longer follows a change to the
container’s CPU quota while the program runs.
check and serve exit 1 on a value out of range, such as
myapp: MYAPP_MAX_PROCS: must stand above zero, got "0". Every other
command runs without the limits, so version and help still work.
Call gonsole.Main after the program loads its .env, so the limits
in that file reach Go.