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