Graceful shutdown

Fixes #25
parent 60074731
......@@ -2,6 +2,7 @@
## Version x.x.x
* Add: `-config`-flag to specify config file when running CrashDragon
* Change: Now gracefully shutting down on `SIGINT` (See #25)
## Version 1.1.1
* Add: `govendor sync` step to update guide
......
......@@ -18,6 +18,11 @@ import (
var rchan = make(chan database.Report, 5000)
// QueueSize returns the number of reports in the queue
func QueueSize() int {
return len(rchan)
}
// StartQueue runs the processor queue
func StartQueue() {
// Spawn 4 processors
......
package main
import (
"context"
"flag"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"time"
"code.videolan.org/videolan/CrashDragon/config"
"code.videolan.org/videolan/CrashDragon/database"
......@@ -116,9 +121,54 @@ func main() {
router := initRouter()
srv := &http.Server{
Handler: router,
}
// See: https://github.com/gin-gonic/gin/blob/master/examples/graceful-shutdown/graceful-shutdown/server.go
if config.C.UseSocket {
router.RunUnix(config.C.BindSocket)
os.Remove(config.C.BindSocket)
listener, err := net.Listen("unix", config.C.BindSocket)
if err != nil {
log.Fatalf("FAT Socket error: %+v", err)
return
}
defer listener.Close()
go func() {
// service connections
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
} else {
router.Run(config.C.BindAddress)
srv.Addr = config.C.BindAddress
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
}
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Closing Server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server stopped, waiting for processor queue to empty...")
for processor.QueueSize() > 0 {
}
log.Println("Queue empty, closing database...")
database.Db.Close()
log.Println("Closed database, good bye!")
os.Exit(0)
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment