daemon.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/reader/feed"
  16. "miniflux.app/service/httpd"
  17. "miniflux.app/service/scheduler"
  18. "miniflux.app/storage"
  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. feedHandler := feed.NewFeedHandler(store)
  27. pool := worker.NewPool(feedHandler, config.Opts.WorkerPoolSize())
  28. if config.Opts.HasSchedulerService() && !config.Opts.HasMaintenanceMode() {
  29. scheduler.Serve(store, pool)
  30. }
  31. var httpServer *http.Server
  32. if config.Opts.HasHTTPService() {
  33. httpServer = httpd.Serve(store, pool, feedHandler)
  34. }
  35. if config.Opts.HasMetricsCollector() {
  36. collector := metric.NewCollector(store, config.Opts.MetricsRefreshInterval())
  37. go collector.GatherStorageMetrics()
  38. }
  39. <-stop
  40. logger.Info("Shutting down the process...")
  41. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  42. defer cancel()
  43. if httpServer != nil {
  44. httpServer.Shutdown(ctx)
  45. }
  46. logger.Info("Process gracefully stopped")
  47. }