daemon.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. <-stop
  38. logger.Info("Shutting down the process...")
  39. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  40. defer cancel()
  41. if httpServer != nil {
  42. httpServer.Shutdown(ctx)
  43. }
  44. logger.Info("Process gracefully stopped")
  45. }