core: Run startup/shutdown functions only once

Even if defined for multiple hosts. Startup or shutdown callbacks registered by any directive (startup, shutdown, markdown, git, log, etc.) will only run as many times as it appears in the Caddyfile, not repeated for each host that shares that server block. Fixing this involved refactoring three packages (yeesh) and we need to restore some tests that are no longer valid (that used to verify splitting a multiServerBlock into multiple serverBlocks).
This commit is contained in:
Matthew Holt 2015-08-01 13:08:31 -06:00
parent 73397a0973
commit 0ac8bf58ea
7 changed files with 144 additions and 219 deletions

View file

@ -83,18 +83,23 @@ func (s *Server) Serve() error {
// Execute shutdown commands on exit
if len(vh.config.Shutdown) > 0 {
go func() {
go func(vh virtualHost) {
// Wait for signal
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill) // TODO: syscall.SIGQUIT? (Ctrl+\, Unix-only)
signal.Notify(interrupt, os.Interrupt, os.Kill)
<-interrupt
// Run callbacks
exitCode := 0
for _, shutdownFunc := range vh.config.Shutdown {
err := shutdownFunc()
if err != nil {
log.Fatal(err)
exitCode = 1
log.Println(err)
}
}
os.Exit(0)
}()
os.Exit(exitCode) // BUG: Other shutdown goroutines might be running; use sync.WaitGroup
}(vh)
}
}