Handle the SIGINT and SIGTERM signals to support stopping a test cleanly (#84)

This commit is contained in:
Arturo Filastò 2019-12-27 11:32:08 +01:00 committed by Simone Basso
commit 7bbbab8774
3 changed files with 48 additions and 5 deletions

23
ooni.go
View file

@ -3,6 +3,7 @@ package ooni
import (
"io/ioutil"
"os"
"sync/atomic"
"github.com/apex/log"
"github.com/ooni/probe-cli/config"
@ -29,6 +30,10 @@ type Context struct {
dbPath string
configPath string
// We need to use a int64 in order to use the atomic.AddInt64/LoadInt64
// operations to ensure consistent reads of the variables.
isTerminatedAtomicInt int64
}
// MaybeLocationLookup will lookup the location of the user unless it's already cached
@ -36,6 +41,17 @@ func (c *Context) MaybeLocationLookup() error {
return c.Session.MaybeLookupLocation()
}
// IsTerminated checks to see if the isTerminatedAtomicInt is set to a non zero
// value and therefore we have received the signal to shutdown the running test
func (c *Context) IsTerminated() bool {
i := atomic.LoadInt64(&c.isTerminatedAtomicInt)
return i != 0
}
func (c *Context) Terminate() {
atomic.AddInt64(&c.isTerminatedAtomicInt, 1)
}
// Init the OONI manager
func (c *Context) Init() error {
var err error
@ -94,9 +110,10 @@ func (c *Context) Init() error {
// NewContext creates a new context instance.
func NewContext(configPath string, homePath string) *Context {
return &Context{
Home: homePath,
Config: &config.Config{},
configPath: configPath,
Home: homePath,
Config: &config.Config{},
configPath: configPath,
isTerminatedAtomicInt: 0,
}
}