mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-18 14:47:03 +00:00
chasquid: Flush logs periodically and before exiting
This patch makes chasquid flush logs periodically, and also before exiting, even if it gets killed via the usual signals (including Ctrl-C). Both things help troubleshooting when there is not much traffic, or we kill the server manually (including during automated tests).
This commit is contained in:
26
chasquid.go
26
chasquid.go
@@ -13,8 +13,10 @@ import (
|
|||||||
"net/mail"
|
"net/mail"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"blitiri.com.ar/go/chasquid/internal/auth"
|
"blitiri.com.ar/go/chasquid/internal/auth"
|
||||||
@@ -44,6 +46,11 @@ var (
|
|||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
setupSignalHandling()
|
||||||
|
|
||||||
|
defer glog.Flush()
|
||||||
|
go periodicallyFlushLogs()
|
||||||
|
|
||||||
// Seed the PRNG, just to prevent for it to be totally predictable.
|
// Seed the PRNG, just to prevent for it to be totally predictable.
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
@@ -134,6 +141,25 @@ func loadDomain(s *Server, name, dir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush logs periodically, to help troubleshooting if there isn't that much
|
||||||
|
// traffic.
|
||||||
|
func periodicallyFlushLogs() {
|
||||||
|
for range time.Tick(5 * time.Second) {
|
||||||
|
glog.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up signal handling, to flush logs when we get killed.
|
||||||
|
func setupSignalHandling() {
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-c
|
||||||
|
glog.Flush()
|
||||||
|
os.Exit(1)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
// Main hostname, used for display only.
|
// Main hostname, used for display only.
|
||||||
Hostname string
|
Hostname string
|
||||||
|
|||||||
Reference in New Issue
Block a user