Optimise start-up and shut-down
This commit is contained in:
parent
91fd38104c
commit
992005cc97
@ -26,7 +26,7 @@ func InitTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
|
||||
|
||||
// Runs the ticker and handles tick events
|
||||
func runTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
|
||||
log.Info("Starting background ticker", zap.Duration("frequency", frequency))
|
||||
log.Info("Starting background task ticker", zap.Duration("frequency", frequency))
|
||||
ticker := time.NewTicker(frequency)
|
||||
defer ticker.Stop()
|
||||
|
||||
|
@ -36,7 +36,7 @@ func InitDataAccess(cfg *helpers.Configuration) {
|
||||
fmt.Println(err)
|
||||
log.Fatal("Error connecting to database: " + err.Error())
|
||||
} else {
|
||||
log.Info("Database connection successful")
|
||||
log.Info("Database connection initialising")
|
||||
}
|
||||
MongoClient = client
|
||||
}
|
||||
|
27
log/log.go
27
log/log.go
@ -7,27 +7,28 @@ import (
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// Logger object is exported to facilitate migration
|
||||
// to the single logger instance. Use outside `package log`
|
||||
// Use outside `package log`
|
||||
// should be avoided.
|
||||
var post *zap.Logger
|
||||
|
||||
// Initialises the logger
|
||||
func init() {
|
||||
func InitLogger() {
|
||||
var err error
|
||||
mode := os.Getenv("runtime")
|
||||
if mode == "" {
|
||||
mode = "prod"
|
||||
}
|
||||
|
||||
logLevel := zapcore.InfoLevel
|
||||
var level zapcore.Level
|
||||
if mode == "debug" {
|
||||
logLevel = zapcore.DebugLevel
|
||||
level = zap.DebugLevel
|
||||
} else {
|
||||
level = zap.InfoLevel
|
||||
}
|
||||
|
||||
config := zap.NewDevelopmentConfig()
|
||||
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
config.Level = zap.NewAtomicLevelAt(logLevel)
|
||||
config.Level = zap.NewAtomicLevelAt(level)
|
||||
post, err = config.Build(zap.AddCallerSkip(1))
|
||||
if err != nil {
|
||||
panic("Failed to initialize logger: " + err.Error())
|
||||
@ -35,29 +36,41 @@ func init() {
|
||||
|
||||
defer post.Sync()
|
||||
|
||||
Info("Logger initialised", zap.String("level", logLevel.String()), zap.String("runtime", mode))
|
||||
Info("Logger initialised", zap.String("level", level.String()), zap.String("runtime", mode))
|
||||
}
|
||||
|
||||
// Logs message at info level
|
||||
func Info(msg string, fields ...zap.Field) {
|
||||
post.Info(msg, fields...)
|
||||
}
|
||||
|
||||
// Logs message at debug level
|
||||
func Debug(msg string, fields ...zap.Field) {
|
||||
post.Debug(msg, fields...)
|
||||
}
|
||||
|
||||
// Logs message at warn level
|
||||
func Warn(msg string, fields ...zap.Field) {
|
||||
post.Warn(msg, fields...)
|
||||
}
|
||||
|
||||
// Logs message at error level
|
||||
func Error(msg string, fields ...zap.Field) {
|
||||
post.Error(msg, fields...)
|
||||
}
|
||||
|
||||
// Logs message at fatal level then call os.exit(1)
|
||||
func Fatal(msg string, fields ...zap.Field) {
|
||||
post.Fatal(msg, fields...)
|
||||
}
|
||||
|
||||
// Logs message at panic level the panics
|
||||
func Panic(msg string, fields ...zap.Field) {
|
||||
post.Panic(msg, fields...)
|
||||
}
|
||||
|
||||
// Flushes log messages
|
||||
func Cleanup() {
|
||||
Info("Flushing log messages")
|
||||
post.Sync()
|
||||
}
|
||||
|
29
main.go
29
main.go
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
@ -18,7 +19,14 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
printStartupBanner()
|
||||
fmt.Println("Version", helpers.Version, "\n\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.InitLogger()
|
||||
defer log.Cleanup()
|
||||
log.Info("Initialising OwlBoard timetable-mgr", zap.String("version", helpers.Version))
|
||||
cfg, err := helpers.LoadConfig()
|
||||
if err != nil {
|
||||
@ -82,5 +90,26 @@ func cleanup(cfg *helpers.Configuration, stop chan<- struct{}) {
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
log.Cleanup()
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func printStartupBanner() {
|
||||
art := `
|
||||
___ _ ____ _
|
||||
/ _ \__ _| | __ ) ___ __ _ _ __ __| |
|
||||
| | | \ \ /\ / / | _ \ / _ \ / _' | '__/ _' |
|
||||
| |_| |\ V V /| | |_) | (_) | (_| | | | (_| |
|
||||
\___/ \_/\_/ |_|____/ \___/ \__,_|_| \__,_|
|
||||
|
||||
_ _ _ _ _
|
||||
| |_(_)_ __ ___ ___| |_ __ _| |__ | | ___ _ __ ___ __ _ _ __
|
||||
| __| | '_ ' _ \ / _ \ __/ _' | '_ \| |/ _ \_____| '_ ' _ \ / _' | '__|
|
||||
| |_| | | | | | | __/ || (_| | |_) | | __/_____| | | | | | (_| | |
|
||||
\__|_|_| |_| |_|\___|\__\__,_|_.__/|_|\___| |_| |_| |_|\__, |_|
|
||||
|___/
|
||||
`
|
||||
|
||||
fmt.Println(art)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user