daemon.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package cli // import "miniflux.app/v2/internal/cli"
  4. import (
  5. "context"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "time"
  11. "miniflux.app/v2/internal/config"
  12. httpd "miniflux.app/v2/internal/http/server"
  13. "miniflux.app/v2/internal/logger"
  14. "miniflux.app/v2/internal/metric"
  15. "miniflux.app/v2/internal/storage"
  16. "miniflux.app/v2/internal/systemd"
  17. "miniflux.app/v2/internal/worker"
  18. )
  19. func startDaemon(store *storage.Storage) {
  20. logger.Info("Starting daemon...")
  21. stop := make(chan os.Signal, 1)
  22. signal.Notify(stop, os.Interrupt)
  23. signal.Notify(stop, syscall.SIGTERM)
  24. pool := worker.NewPool(store, config.Opts.WorkerPoolSize())
  25. if config.Opts.HasSchedulerService() && !config.Opts.HasMaintenanceMode() {
  26. runScheduler(store, pool)
  27. }
  28. var httpServer *http.Server
  29. if config.Opts.HasHTTPService() {
  30. httpServer = httpd.StartWebServer(store, pool)
  31. }
  32. if config.Opts.HasMetricsCollector() {
  33. collector := metric.NewCollector(store, config.Opts.MetricsRefreshInterval())
  34. go collector.GatherStorageMetrics()
  35. }
  36. if systemd.HasNotifySocket() {
  37. logger.Info("Sending readiness notification to Systemd")
  38. if err := systemd.SdNotify(systemd.SdNotifyReady); err != nil {
  39. logger.Error("Unable to send readiness notification to systemd: %v", err)
  40. }
  41. if config.Opts.HasWatchdog() && systemd.HasSystemdWatchdog() {
  42. logger.Info("Activating Systemd watchdog")
  43. go func() {
  44. interval, err := systemd.WatchdogInterval()
  45. if err != nil {
  46. logger.Error("Unable to parse watchdog interval from systemd: %v", err)
  47. return
  48. }
  49. for {
  50. err := store.Ping()
  51. if err != nil {
  52. logger.Error(`Systemd Watchdog: %v`, err)
  53. } else {
  54. systemd.SdNotify(systemd.SdNotifyWatchdog)
  55. }
  56. time.Sleep(interval / 3)
  57. }
  58. }()
  59. }
  60. }
  61. <-stop
  62. logger.Info("Shutting down the process...")
  63. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  64. defer cancel()
  65. if httpServer != nil {
  66. httpServer.Shutdown(ctx)
  67. }
  68. logger.Info("Process gracefully stopped")
  69. }