From e0557d8961eb6ad679984546b82d8f6b054e40fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 23 May 2021 18:24:57 -0700 Subject: [PATCH] Add option to disable watchdog and set default timeout to DB healthcheck --- cli/daemon.go | 2 +- config/options.go | 9 +++++++++ config/parser.go | 2 ++ storage/storage.go | 7 ++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cli/daemon.go b/cli/daemon.go index bdadf9ed..5953bb95 100644 --- a/cli/daemon.go +++ b/cli/daemon.go @@ -52,7 +52,7 @@ func startDaemon(store *storage.Storage) { logger.Error("Unable to send readiness notification to systemd: %v", err) } - if systemd.HasSystemdWatchdog() { + if config.Opts.HasWatchdog() && systemd.HasSystemdWatchdog() { logger.Info("Activating Systemd watchdog") go func() { diff --git a/config/options.go b/config/options.go index 91ec48c4..b676e50f 100644 --- a/config/options.go +++ b/config/options.go @@ -64,6 +64,7 @@ const ( defaultMetricsCollector = false defaultMetricsRefreshInterval = 60 defaultMetricsAllowedNetworks = "127.0.0.1/8" + defaultWatchdog = true ) var defaultHTTPClientUserAgent = "Mozilla/5.0 (compatible; Miniflux/" + version.Version + "; +https://miniflux.app)" @@ -128,6 +129,7 @@ type Options struct { metricsCollector bool metricsRefreshInterval int metricsAllowedNetworks []string + watchdog bool } // NewOptions returns Options with default values. @@ -183,6 +185,7 @@ func NewOptions() *Options { metricsCollector: defaultMetricsCollector, metricsRefreshInterval: defaultMetricsRefreshInterval, metricsAllowedNetworks: []string{defaultMetricsAllowedNetworks}, + watchdog: defaultWatchdog, } } @@ -457,6 +460,11 @@ func (o *Options) HTTPClientUserAgent() string { return o.httpClientUserAgent } +// HasWatchdog returns true if the systemd watchdog is enabled. +func (o *Options) HasWatchdog() bool { + return o.watchdog +} + // SortedOptions returns options as a list of key value pairs, sorted by keys. func (o *Options) SortedOptions() []*Option { var keyValues = map[string]interface{}{ @@ -512,6 +520,7 @@ func (o *Options) SortedOptions() []*Option { "SCHEDULER_SERVICE": o.schedulerService, "SERVER_TIMING_HEADER": o.serverTimingHeader, "WORKER_POOL_SIZE": o.workerPoolSize, + "WATCHDOG": o.watchdog, } keys := make([]string, 0, len(keyValues)) diff --git a/config/parser.go b/config/parser.go index 64e2a699..a3f587df 100644 --- a/config/parser.go +++ b/config/parser.go @@ -189,6 +189,8 @@ func (p *Parser) parseLines(lines []string) (err error) { p.opts.metricsAllowedNetworks = parseStringList(value, []string{defaultMetricsAllowedNetworks}) case "FETCH_YOUTUBE_WATCH_TIME": p.opts.fetchYouTubeWatchTime = parseBool(value, defaultFetchYouTubeWatchTime) + case "WATCHDOG": + p.opts.watchdog = parseBool(value, defaultWatchdog) } } diff --git a/storage/storage.go b/storage/storage.go index 947011bb..ca3d2360 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -5,7 +5,9 @@ package storage // import "miniflux.app/storage" import ( + "context" "database/sql" + "time" ) // Storage handles all operations related to the database. @@ -31,7 +33,10 @@ func (s *Storage) DatabaseVersion() string { // Ping checks if the database connection works. func (s *Storage) Ping() error { - return s.db.Ping() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + return s.db.PingContext(ctx) } // DBStats returns database statistics.