daemon.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/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. // Notify systemd that we are ready.
  38. if err := sdNotify(sdNotifyReady); err != nil {
  39. logger.Error("Unable to send readiness notification to systemd: %v", err)
  40. }
  41. <-stop
  42. logger.Info("Shutting down the process...")
  43. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  44. defer cancel()
  45. if httpServer != nil {
  46. httpServer.Shutdown(ctx)
  47. }
  48. logger.Info("Process gracefully stopped")
  49. }