daemon.go 2.1 KB

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