using gofmtsort to try and make things a bit easier to splice and find
This commit is contained in:
@@ -18,16 +18,6 @@ import (
|
||||
"github.com/kardianos/osext"
|
||||
)
|
||||
|
||||
var (
|
||||
log = log2.GetLogger(indra.PathBase)
|
||||
check = log.E.Chk
|
||||
)
|
||||
|
||||
type HandlerWithSource struct {
|
||||
Source string
|
||||
Fn func()
|
||||
}
|
||||
|
||||
var (
|
||||
Restart bool
|
||||
requested atomic.Bool
|
||||
@@ -42,11 +32,42 @@ var (
|
||||
addHandlerChan = make(chan HandlerWithSource)
|
||||
// HandlersDone is closed after all interrupt handlers run the first
|
||||
// time an interrupt is signaled.
|
||||
HandlersDone = make(chan struct{})
|
||||
HandlersDone = make(chan struct{})
|
||||
interruptCallbackSources []string
|
||||
interruptCallbacks []func()
|
||||
log = log2.GetLogger(indra.PathBase)
|
||||
check = log.E.Chk
|
||||
)
|
||||
|
||||
var interruptCallbacks []func()
|
||||
var interruptCallbackSources []string
|
||||
type HandlerWithSource struct {
|
||||
Source string
|
||||
Fn func()
|
||||
}
|
||||
|
||||
// AddHandler adds a handler to call when a SIGINT (Ctrl+C) is received.
|
||||
func AddHandler(handler func()) {
|
||||
// Create the channel and start the main interrupt handler that invokes
|
||||
// all other callbacks and exits if not already done.
|
||||
_, loc, line, _ := runtime.Caller(1)
|
||||
msg := fmt.Sprintf("%s:%d", loc, line)
|
||||
log.T.Ln("\n"+msg, "added interrupt handler")
|
||||
if ch == nil {
|
||||
ch = make(chan os.Signal)
|
||||
signal.Notify(ch, signals...)
|
||||
go Listener()
|
||||
}
|
||||
addHandlerChan <- HandlerWithSource{
|
||||
msg, handler,
|
||||
}
|
||||
}
|
||||
|
||||
// GoroutineDump returns a string with the current goroutine dump in order to
|
||||
// show what's going on in case of timeout.
|
||||
func GoroutineDump() string {
|
||||
buf := make([]byte, 1<<18)
|
||||
n := runtime.Stack(buf, true)
|
||||
return string(buf[:n])
|
||||
}
|
||||
|
||||
// Listener listens for interrupt signals, registers interrupt callbacks, and
|
||||
// responds to custom shutdown signals as required
|
||||
@@ -82,79 +103,42 @@ func Listener() {
|
||||
}
|
||||
} else {
|
||||
log.I.Ln("doing windows restart")
|
||||
|
||||
// procAttr := new(os.ProcAttr)
|
||||
// procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
|
||||
// os.StartProcess(os.Args[0], os.Args[1:], procAttr)
|
||||
|
||||
var s []string
|
||||
// s = []string{"cmd.exe", "/C", "start"}
|
||||
s = append(s, os.Args[0])
|
||||
// s = append(s, "--delaystart")
|
||||
s = append(s, os.Args[1:]...)
|
||||
cmd := exec.Command(s[0], s[1:]...)
|
||||
log.I.Ln("windows restart done")
|
||||
if e = cmd.Start(); e != nil {
|
||||
log.I.Ln(e)
|
||||
}
|
||||
// // select{}
|
||||
// os.Exit(0)
|
||||
}
|
||||
}
|
||||
// time.Sleep(time.Second * 3)
|
||||
// os.Exit(1)
|
||||
// close(HandlersDone)
|
||||
}
|
||||
out:
|
||||
for {
|
||||
select {
|
||||
case sig := <-ch:
|
||||
// if !requested {
|
||||
// L.Printf("\r>>> received signal (%s)\n", sig)
|
||||
fmt.Print("\r \r")
|
||||
log.W.F("received signal %v", sig)
|
||||
requested.Store(true)
|
||||
invokeCallbacks()
|
||||
// pprof.Lookup("goroutine").WriteTo(os.Stderr, 2)
|
||||
// }
|
||||
break out
|
||||
case <-ShutdownRequestChan:
|
||||
// if !requested {
|
||||
log.I.Ln("received shutdown request - shutting down...")
|
||||
requested.Store(true)
|
||||
invokeCallbacks()
|
||||
break out
|
||||
// }
|
||||
case handler := <-addHandlerChan:
|
||||
// if !requested {
|
||||
interruptCallbacks =
|
||||
append(interruptCallbacks, handler.Fn)
|
||||
interruptCallbackSources =
|
||||
append(interruptCallbackSources, handler.Source)
|
||||
// }
|
||||
case <-HandlersDone:
|
||||
break out
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AddHandler adds a handler to call when a SIGINT (Ctrl+C) is received.
|
||||
func AddHandler(handler func()) {
|
||||
// Create the channel and start the main interrupt handler that invokes
|
||||
// all other callbacks and exits if not already done.
|
||||
_, loc, line, _ := runtime.Caller(1)
|
||||
msg := fmt.Sprintf("%s:%d", loc, line)
|
||||
log.T.Ln("\n"+msg, "added interrupt handler")
|
||||
if ch == nil {
|
||||
ch = make(chan os.Signal)
|
||||
signal.Notify(ch, signals...)
|
||||
go Listener()
|
||||
}
|
||||
addHandlerChan <- HandlerWithSource{
|
||||
msg, handler,
|
||||
}
|
||||
}
|
||||
|
||||
// Request programmatically requests a shutdown
|
||||
func Request() {
|
||||
_, f, l, _ := runtime.Caller(1)
|
||||
@@ -165,7 +149,6 @@ func Request() {
|
||||
}
|
||||
requested.Store(true)
|
||||
close(ShutdownRequestChan)
|
||||
// qu.PrintChanState()
|
||||
var ok bool
|
||||
select {
|
||||
case _, ok = <-ShutdownRequestChan:
|
||||
@@ -177,14 +160,6 @@ func Request() {
|
||||
}
|
||||
}
|
||||
|
||||
// GoroutineDump returns a string with the current goroutine dump in order to
|
||||
// show what's going on in case of timeout.
|
||||
func GoroutineDump() string {
|
||||
buf := make([]byte, 1<<18)
|
||||
n := runtime.Stack(buf, true)
|
||||
return string(buf[:n])
|
||||
}
|
||||
|
||||
// RequestRestart sets the reset flag and requests a restart
|
||||
func RequestRestart() {
|
||||
Restart = true
|
||||
@@ -193,6 +168,4 @@ func RequestRestart() {
|
||||
}
|
||||
|
||||
// Requested returns true if an interrupt has been requested
|
||||
func Requested() bool {
|
||||
return requested.Load()
|
||||
}
|
||||
func Requested() bool { return requested.Load() }
|
||||
|
||||
Reference in New Issue
Block a user