Skip to content

Commit bba22c5

Browse files
committed
Use ctx everywhere & render exit errors properly
The `context.Context` related changes are neccessary as otherwise you will have to wait for 5m when having Icinga notification started with invalid database config due to the internal database.Connect retrials. Lastly, the logging related changes are useful as we want to properly render the stack traces when we unexpectedly exit from the main function instead of wrapping the error in a confusing `zap.Error()` json key.
1 parent 778e5fa commit bba22c5

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

cmd/icinga-notifications-daemon/main.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/icinga/icinga-notifications/internal/icinga2"
1515
"github.com/icinga/icinga-notifications/internal/incident"
1616
"github.com/icinga/icinga-notifications/internal/listener"
17-
"go.uber.org/zap"
17+
"github.com/pkg/errors"
1818
"os"
1919
"os/signal"
2020
"runtime"
@@ -62,24 +62,25 @@ func main() {
6262
}
6363

6464
logger := logs.GetLogger()
65+
defer func() { _ = logger.Sync() }()
66+
6567
logger.Infof("Starting Icinga Notifications daemon (%s)", internal.Version.Version)
6668
db, err := database.NewDbFromConfig(&conf.Database, logs.GetChildLogger("database"), database.RetryConnectorCallbacks{})
6769
if err != nil {
68-
logger.Fatalw("cannot create database connection from config", zap.Error(err))
70+
logger.Fatalf("%+v", errors.Wrap(err, "cannot create database connection from config"))
6971
}
7072
defer db.Close()
71-
{
72-
logger.Infof("Connecting to database at '%s'", db.GetAddr())
73-
if err := db.Ping(); err != nil {
74-
logger.Fatalw("cannot connect to database", zap.Error(err))
75-
}
76-
}
77-
78-
channel.UpsertPlugins(conf.ChannelPluginDir, logs.GetChildLogger("channel"), db)
7973

8074
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
8175
defer cancel()
8276

77+
logger.Infof("Connecting to database at '%s'", db.GetAddr())
78+
if err := db.PingContext(ctx); err != nil {
79+
logger.Fatalf("Cannot connect to the database: %+v", err)
80+
}
81+
82+
channel.UpsertPlugins(ctx, conf.ChannelPluginDir, logs.GetChildLogger("channel"), db)
83+
8384
icinga2Launcher := &icinga2.Launcher{
8485
Ctx: ctx,
8586
Logs: logs,
@@ -89,7 +90,7 @@ func main() {
8990

9091
runtimeConfig := config.NewRuntimeConfig(icinga2Launcher.Launch, logs, db)
9192
if err := runtimeConfig.UpdateFromDatabase(ctx); err != nil {
92-
logger.Fatalw("failed to load config from database", zap.Error(err))
93+
logger.Fatalf("Failed to load config from database %+v", err)
9394
}
9495

9596
icinga2Launcher.RuntimeConfig = runtimeConfig
@@ -98,13 +99,13 @@ func main() {
9899

99100
err = incident.LoadOpenIncidents(ctx, db, logs.GetChildLogger("incident"), runtimeConfig)
100101
if err != nil {
101-
logger.Fatalw("Can't load incidents from database", zap.Error(err))
102+
logger.Fatalf("%+v", errors.Wrap(err, "cannot load incidents from database"))
102103
}
103104

104105
// Wait to load open incidents from the database before either starting Event Stream Clients or starting the Listener.
105106
icinga2Launcher.Ready()
106107
if err := listener.NewListener(db, runtimeConfig, logs).Run(ctx); err != nil {
107-
logger.Errorw("Listener has finished with an error", zap.Error(err))
108+
logger.Errorf("%+v", errors.Wrap(err, "listener has finished with an error"))
108109
} else {
109110
logger.Info("Listener has finished")
110111
}

internal/channel/plugin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package channel
22

33
import (
44
"bufio"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"github.com/icinga/icinga-go-library/database"
@@ -166,7 +167,7 @@ func forwardLogs(errPipe io.Reader, logger *zap.SugaredLogger) {
166167
}
167168

168169
// UpsertPlugins upsert the available_channel_type table with working plugins
169-
func UpsertPlugins(channelPluginDir string, logger *logging.Logger, db *database.DB) {
170+
func UpsertPlugins(ctx context.Context, channelPluginDir string, logger *logging.Logger, db *database.DB) {
170171
logger.Debug("Updating available channel types")
171172
files, err := os.ReadDir(channelPluginDir)
172173
if err != nil {
@@ -209,7 +210,7 @@ func UpsertPlugins(channelPluginDir string, logger *logging.Logger, db *database
209210
}
210211

211212
stmt, _ := db.BuildUpsertStmt(&plugin.Info{})
212-
_, err = db.NamedExec(stmt, pluginInfos)
213+
_, err = db.NamedExecContext(ctx, stmt, pluginInfos)
213214
if err != nil {
214215
logger.Errorw("Failed to update available channel types", zap.Error(err))
215216
} else {

0 commit comments

Comments
 (0)