Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions book/src/command/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ The global options are shared by all commands and include:
* `-h` display help
* `-help` display help
* `-hidden` show hidden options
* `-signals` catch `SIGINT`, `SIGTERM`, and `SIGPIPE` and exit gracefully
* `-version` print version and exit

The `-signals` option is off by default except for
[super db serve](db.md#super-db-serve), which always enables
the catching of `SIGINT` and `SIGTERM` (but not `SIGPIPE`). When `-signals` is enabled,
the `super` command is interrupted but continues to a natural stopping point (e.g., flushing partial output files, removing spill files from temporary storage, etc.).
In the current implementation,
this can be unpredictably long so the default is to exit immediately,
potentially leaving behind corrupt outputs, etc.

## Query

The query options are available to commands that invoke the query runtime,
Expand Down
23 changes: 18 additions & 5 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Flags struct {
cpuprofile string
memprofile string
trace string
signals bool
cpuProfileFile *os.File
traceFile *os.File
}
Expand All @@ -27,6 +28,7 @@ func (f *Flags) SetFlags(fs *flag.FlagSet) {
fs.StringVar(&f.cpuprofile, "cpuprofile", "", "write cpu profile to given file name")
fs.StringVar(&f.memprofile, "memprofile", "", "write memory profile to given file name")
fs.StringVar(&f.trace, "trace", "", "write trace to given file name")
fs.BoolVar(&f.signals, "signals", false, "catch SIGINT, SIGPIPE, and SIGTERM and exit gracefully")
}

type Initializer interface {
Expand All @@ -35,7 +37,10 @@ type Initializer interface {

// Init is equivalent to InitWithSignals with SIGINT, SIGPIPE, and SIGTERM.
func (f *Flags) Init(all ...Initializer) (context.Context, context.CancelFunc, error) {
return f.InitWithSignals(all, syscall.SIGINT, syscall.SIGPIPE, syscall.SIGTERM)
if f.signals {
return f.InitWithSignals(all, syscall.SIGINT, syscall.SIGPIPE, syscall.SIGTERM)
}
return f.InitWithSignals(all)
}

// InitWithSignals handles the flags defined in SetFlags, calls the Init method
Expand Down Expand Up @@ -65,10 +70,18 @@ func (f *Flags) InitWithSignals(all []Initializer, signals ...os.Signal) (contex
}
trace.Start(f.traceFile)
}
ctx, cancel := signalContext(context.Background(), signals...)
cleanup := func() {
cancel()
f.cleanup()
var ctx context.Context
var cleanup context.CancelFunc
if len(signals) == 0 {
ctx = context.Background()
cleanup = func() {}
} else {
var cancel context.CancelFunc
ctx, cancel = signalContext(context.Background(), signals...)
cleanup = func() {
cancel()
f.cleanup()
}
}
return ctx, cleanup, nil
}
Expand Down