|
@@ -46,18 +46,19 @@ func unregister(name string) {
|
|
|
// It acts as a store for *containers*, and allows manipulation of these
|
|
|
// containers by executing *jobs*.
|
|
|
type Engine struct {
|
|
|
- handlers map[string]Handler
|
|
|
- catchall Handler
|
|
|
- hack Hack // data for temporary hackery (see hack.go)
|
|
|
- id string
|
|
|
- Stdout io.Writer
|
|
|
- Stderr io.Writer
|
|
|
- Stdin io.Reader
|
|
|
- Logging bool
|
|
|
- tasks sync.WaitGroup
|
|
|
- l sync.RWMutex // lock for shutdown
|
|
|
- shutdown bool
|
|
|
- onShutdown []func() // shutdown handlers
|
|
|
+ handlers map[string]Handler
|
|
|
+ catchall Handler
|
|
|
+ hack Hack // data for temporary hackery (see hack.go)
|
|
|
+ id string
|
|
|
+ Stdout io.Writer
|
|
|
+ Stderr io.Writer
|
|
|
+ Stdin io.Reader
|
|
|
+ Logging bool
|
|
|
+ tasks sync.WaitGroup
|
|
|
+ l sync.RWMutex // lock for shutdown
|
|
|
+ shutdownWait sync.WaitGroup
|
|
|
+ shutdown bool
|
|
|
+ onShutdown []func() // shutdown handlers
|
|
|
}
|
|
|
|
|
|
func (eng *Engine) Register(name string, handler Handler) error {
|
|
@@ -143,6 +144,7 @@ func (eng *Engine) Job(name string, args ...string) *Job {
|
|
|
func (eng *Engine) OnShutdown(h func()) {
|
|
|
eng.l.Lock()
|
|
|
eng.onShutdown = append(eng.onShutdown, h)
|
|
|
+ eng.shutdownWait.Add(1)
|
|
|
eng.l.Unlock()
|
|
|
}
|
|
|
|
|
@@ -156,6 +158,7 @@ func (eng *Engine) Shutdown() {
|
|
|
eng.l.Lock()
|
|
|
if eng.shutdown {
|
|
|
eng.l.Unlock()
|
|
|
+ eng.shutdownWait.Wait()
|
|
|
return
|
|
|
}
|
|
|
eng.shutdown = true
|
|
@@ -180,17 +183,15 @@ func (eng *Engine) Shutdown() {
|
|
|
|
|
|
// Call shutdown handlers, if any.
|
|
|
// Timeout after 10 seconds.
|
|
|
- var wg sync.WaitGroup
|
|
|
for _, h := range eng.onShutdown {
|
|
|
- wg.Add(1)
|
|
|
go func(h func()) {
|
|
|
- defer wg.Done()
|
|
|
h()
|
|
|
+ eng.shutdownWait.Done()
|
|
|
}(h)
|
|
|
}
|
|
|
done := make(chan struct{})
|
|
|
go func() {
|
|
|
- wg.Wait()
|
|
|
+ eng.shutdownWait.Wait()
|
|
|
close(done)
|
|
|
}()
|
|
|
select {
|