daemon.go 2.0 KB

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