2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-11-12 00:32:48 +01:00
|
|
|
|
|
|
|
package cli // import "miniflux.app/cli"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-12 00:54:19 +01:00
|
|
|
"net/http"
|
2018-11-12 00:32:48 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"miniflux.app/config"
|
2023-06-25 20:23:23 +02:00
|
|
|
httpd "miniflux.app/http/server"
|
2018-11-12 00:32:48 +01:00
|
|
|
"miniflux.app/logger"
|
2020-09-28 01:01:06 +02:00
|
|
|
"miniflux.app/metric"
|
2018-11-12 00:32:48 +01:00
|
|
|
"miniflux.app/storage"
|
2021-05-23 03:36:32 +02:00
|
|
|
"miniflux.app/systemd"
|
2018-11-12 00:32:48 +01:00
|
|
|
"miniflux.app/worker"
|
|
|
|
)
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
func startDaemon(store *storage.Storage) {
|
2023-06-25 20:23:23 +02:00
|
|
|
logger.Info("Starting daemon...")
|
2018-11-12 00:32:48 +01:00
|
|
|
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, os.Interrupt)
|
|
|
|
signal.Notify(stop, syscall.SIGTERM)
|
|
|
|
|
2021-01-03 01:33:41 +01:00
|
|
|
pool := worker.NewPool(store, config.Opts.WorkerPoolSize())
|
2018-11-12 00:32:48 +01:00
|
|
|
|
2020-09-13 03:31:45 +02:00
|
|
|
if config.Opts.HasSchedulerService() && !config.Opts.HasMaintenanceMode() {
|
2023-06-25 20:23:23 +02:00
|
|
|
runScheduler(store, pool)
|
2018-11-12 00:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var httpServer *http.Server
|
2019-06-02 03:18:09 +02:00
|
|
|
if config.Opts.HasHTTPService() {
|
2023-06-25 20:23:23 +02:00
|
|
|
httpServer = httpd.StartWebServer(store, pool)
|
2018-11-12 00:54:19 +01:00
|
|
|
}
|
2018-11-12 00:32:48 +01:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-05-24 03:24:57 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-03-11 00:10:17 +01:00
|
|
|
}
|
|
|
|
|
2018-11-12 00:32:48 +01:00
|
|
|
<-stop
|
|
|
|
logger.Info("Shutting down the process...")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-11-12 00:54:19 +01:00
|
|
|
if httpServer != nil {
|
|
|
|
httpServer.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
2018-11-12 00:32:48 +01:00
|
|
|
logger.Info("Process gracefully stopped")
|
|
|
|
}
|