Implement graceful shutdowns
This commit is contained in:
@ -5,11 +5,14 @@ package main
|
|||||||
import (
|
import (
|
||||||
db2 "code.dlmw.ch/dlmw/qv/internal/db"
|
db2 "code.dlmw.ch/dlmw/qv/internal/db"
|
||||||
"code.dlmw.ch/dlmw/qv/internal/models"
|
"code.dlmw.ch/dlmw/qv/internal/models"
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,9 +54,12 @@ func main() {
|
|||||||
WriteTimeout: 10 * time.Second,
|
WriteTimeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = srv.ListenAndServe()
|
go watchForQuitSignals(srv, logger)
|
||||||
|
|
||||||
|
if err = srv.ListenAndServe(); err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func openDb() (*sql.DB, error) {
|
func openDb() (*sql.DB, error) {
|
||||||
@ -63,3 +69,17 @@ func openDb() (*sql.DB, error) {
|
|||||||
}
|
}
|
||||||
return db, err
|
return db, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func watchForQuitSignals(srv *http.Server, logger *slog.Logger) {
|
||||||
|
quit := make(chan os.Signal, 1)
|
||||||
|
|
||||||
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
s := <-quit
|
||||||
|
logger.Info("caught signal", "signal", s.String())
|
||||||
|
srv.Shutdown(ctx)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user