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