miniflux/cli/daemon.go

88 lines
2 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package cli // import "miniflux.app/cli"
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"miniflux.app/config"
httpd "miniflux.app/http/server"
"miniflux.app/logger"
2020-09-28 01:01:06 +02:00
"miniflux.app/metric"
"miniflux.app/storage"
2021-05-23 03:36:32 +02:00
"miniflux.app/systemd"
"miniflux.app/worker"
)
func startDaemon(store *storage.Storage) {
logger.Info("Starting daemon...")
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
signal.Notify(stop, syscall.SIGTERM)
pool := worker.NewPool(store, config.Opts.WorkerPoolSize())
2020-09-13 03:31:45 +02:00
if config.Opts.HasSchedulerService() && !config.Opts.HasMaintenanceMode() {
runScheduler(store, pool)
}
var httpServer *http.Server
if config.Opts.HasHTTPService() {
httpServer = httpd.StartWebServer(store, pool)
}
2020-09-28 01:01:06 +02:00
if config.Opts.HasMetricsCollector() {
collector := metric.NewCollector(store, config.Opts.MetricsRefreshInterval())
go collector.GatherStorageMetrics()
}
2021-05-23 03:36:32 +02:00
if systemd.HasNotifySocket() {
logger.Info("Sending readiness notification to Systemd")
if err := systemd.SdNotify(systemd.SdNotifyReady); err != nil {
logger.Error("Unable to send readiness notification to systemd: %v", err)
}
if config.Opts.HasWatchdog() && systemd.HasSystemdWatchdog() {
2021-05-23 03:36:32 +02:00
logger.Info("Activating Systemd watchdog")
go func() {
interval, err := systemd.WatchdogInterval()
if err != nil {
logger.Error("Unable to parse watchdog interval from systemd: %v", err)
return
}
for {
err := store.Ping()
if err != nil {
logger.Error(`Systemd Watchdog: %v`, err)
} else {
systemd.SdNotify(systemd.SdNotifyWatchdog)
}
2021-05-23 05:25:38 +02:00
time.Sleep(interval / 3)
2021-05-23 03:36:32 +02:00
}
}()
}
}
<-stop
logger.Info("Shutting down the process...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if httpServer != nil {
httpServer.Shutdown(ctx)
}
logger.Info("Process gracefully stopped")
}