From becd086865f35d64a80d01ffb1ca96d0493dfa03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 11 Nov 2018 15:54:19 -0800 Subject: [PATCH] Add config options to disable HTTP and scheduler services --- cli/daemon.go | 16 +++++++++-- config/config.go | 10 +++++++ config/config_test.go | 50 ++++++++++++++++++++++++++++++++++ miniflux.1 | 6 ++++ service/scheduler/scheduler.go | 1 + 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/cli/daemon.go b/cli/daemon.go index 436729cd..aa673d45 100644 --- a/cli/daemon.go +++ b/cli/daemon.go @@ -6,6 +6,7 @@ package cli // import "miniflux.app/cli" import ( "context" + "net/http" "os" "os/signal" "runtime" @@ -31,17 +32,26 @@ func startDaemon(cfg *config.Config, store *storage.Storage) { feedHandler := feed.NewFeedHandler(store) pool := worker.NewPool(feedHandler, cfg.WorkerPoolSize()) - go scheduler.Serve(cfg, store, pool) go showProcessStatistics() - httpServer := httpd.Serve(cfg, store, pool, feedHandler) + if cfg.HasSchedulerService() { + scheduler.Serve(cfg, store, pool) + } + + var httpServer *http.Server + if cfg.HasHTTPService() { + httpServer = httpd.Serve(cfg, store, pool, feedHandler) + } <-stop logger.Info("Shutting down the process...") ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - httpServer.Shutdown(ctx) + if httpServer != nil { + httpServer.Shutdown(ctx) + } + logger.Info("Process gracefully stopped") } diff --git a/config/config.go b/config/config.go index 281a8a3a..7ba3a935 100644 --- a/config/config.go +++ b/config/config.go @@ -214,6 +214,16 @@ func (c *Config) ProxyImages() string { return getStringValue("PROXY_IMAGES", defaultProxyImages) } +// HasHTTPService returns true if the HTTP service is enabled. +func (c *Config) HasHTTPService() bool { + return !getBooleanValue("DISABLE_HTTP_SERVICE") +} + +// HasSchedulerService returns true if the scheduler service is enabled. +func (c *Config) HasSchedulerService() bool { + return !getBooleanValue("DISABLE_SCHEDULER_SERVICE") +} + // NewConfig returns a new Config. func NewConfig() *Config { cfg := &Config{ diff --git a/config/config_test.go b/config/config_test.go index 76b3ac53..1c188db3 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -658,6 +658,56 @@ func TestHSTS(t *testing.T) { } } +func TestDisableHTTPServiceWhenUnset(t *testing.T) { + os.Clearenv() + + cfg := NewConfig() + expected := true + result := cfg.HasHTTPService() + + if result != expected { + t.Fatalf(`Unexpected DISABLE_HTTP_SERVICE value, got %v instead of %v`, result, expected) + } +} + +func TestDisableHTTPService(t *testing.T) { + os.Clearenv() + os.Setenv("DISABLE_HTTP_SERVICE", "1") + + cfg := NewConfig() + expected := false + result := cfg.HasHTTPService() + + if result != expected { + t.Fatalf(`Unexpected DISABLE_HTTP_SERVICE value, got %v instead of %v`, result, expected) + } +} + +func TestDisableSchedulerServiceWhenUnset(t *testing.T) { + os.Clearenv() + + cfg := NewConfig() + expected := true + result := cfg.HasSchedulerService() + + if result != expected { + t.Fatalf(`Unexpected DISABLE_SCHEDULER_SERVICE value, got %v instead of %v`, result, expected) + } +} + +func TestDisableSchedulerService(t *testing.T) { + os.Clearenv() + os.Setenv("DISABLE_SCHEDULER_SERVICE", "1") + + cfg := NewConfig() + expected := false + result := cfg.HasSchedulerService() + + if result != expected { + t.Fatalf(`Unexpected DISABLE_SCHEDULER_SERVICE value, got %v instead of %v`, result, expected) + } +} + func TestRunMigrationsWhenUnset(t *testing.T) { os.Clearenv() diff --git a/miniflux.1 b/miniflux.1 index f9a0816d..4a687d4e 100644 --- a/miniflux.1 +++ b/miniflux.1 @@ -110,6 +110,12 @@ Forces cookies to use secure flag and send HSTS header\&. .B DISABLE_HSTS Disable HTTP Strict Transport Security header if \fBHTTPS\fR is set\&. .TP +.B DISABLE_HTTP_SERVICE +Set the value to 1 to disable the HTTP service\&. +.TP +.B DISABLE_SCHEDULER_SERVICE +Set the value to 1 to disable the internal scheduler service\&. +.TP .B CERT_FILE Path to SSL certificate\&. .TP diff --git a/service/scheduler/scheduler.go b/service/scheduler/scheduler.go index 31c63e76..e938aa12 100644 --- a/service/scheduler/scheduler.go +++ b/service/scheduler/scheduler.go @@ -15,6 +15,7 @@ import ( // Serve starts the internal scheduler. func Serve(cfg *config.Config, store *storage.Storage, pool *worker.Pool) { + logger.Info(`Starting scheduler...`) go feedScheduler(store, pool, cfg.PollingFrequency(), cfg.BatchSize()) go cleanupScheduler(store, cfg.CleanupFrequency()) }