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
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package cli // import "miniflux.app/v2/internal/cli"
|
2018-11-11 18:32:48 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
2018-11-11 18:54:19 -05:00
|
|
|
"net/http"
|
2018-11-11 18:32:48 -05:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
httpd "miniflux.app/v2/internal/http/server"
|
|
|
|
"miniflux.app/v2/internal/metric"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
|
|
|
"miniflux.app/v2/internal/systemd"
|
|
|
|
"miniflux.app/v2/internal/worker"
|
2018-11-11 18:32:48 -05:00
|
|
|
)
|
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
func startDaemon(store *storage.Storage) {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("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() {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Sending readiness notification to Systemd")
|
2021-05-22 21:36:32 -04:00
|
|
|
|
|
|
|
if err := systemd.SdNotify(systemd.SdNotifyReady); err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("Unable to send readiness notification to systemd", slog.Any("error", err))
|
2021-05-22 21:36:32 -04:00
|
|
|
}
|
|
|
|
|
2021-05-23 21:24:57 -04:00
|
|
|
if config.Opts.HasWatchdog() && systemd.HasSystemdWatchdog() {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Activating Systemd watchdog")
|
2021-05-22 21:36:32 -04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
interval, err := systemd.WatchdogInterval()
|
|
|
|
if err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("Unable to get watchdog interval from systemd", slog.Any("error", err))
|
2021-05-22 21:36:32 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2023-09-24 19:32:09 -04:00
|
|
|
if err := store.Ping(); err != nil {
|
|
|
|
slog.Error("Unable to ping database", slog.Any("error", err))
|
2021-05-22 21:36:32 -04:00
|
|
|
} 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
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Shutting down the process")
|
2018-11-11 18:32:48 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Process gracefully stopped")
|
2018-11-11 18:32:48 -05:00
|
|
|
}
|