daemon.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 daemon
  5. import (
  6. "context"
  7. "os"
  8. "os/signal"
  9. "runtime"
  10. "syscall"
  11. "time"
  12. "github.com/miniflux/miniflux/config"
  13. "github.com/miniflux/miniflux/locale"
  14. "github.com/miniflux/miniflux/logger"
  15. "github.com/miniflux/miniflux/reader/feed"
  16. "github.com/miniflux/miniflux/scheduler"
  17. "github.com/miniflux/miniflux/storage"
  18. )
  19. // Run starts the daemon.
  20. func Run(cfg *config.Config, 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. go func() {
  26. for {
  27. var m runtime.MemStats
  28. runtime.ReadMemStats(&m)
  29. logger.Debug("Alloc=%vK, TotalAlloc=%vK, Sys=%vK, NumGC=%v, GoRoutines=%d, NumCPU=%d",
  30. m.Alloc/1024, m.TotalAlloc/1024, m.Sys/1024, m.NumGC, runtime.NumGoroutine(), runtime.NumCPU())
  31. time.Sleep(30 * time.Second)
  32. }
  33. }()
  34. translator := locale.Load()
  35. feedHandler := feed.NewFeedHandler(store, translator)
  36. pool := scheduler.NewWorkerPool(feedHandler, cfg.WorkerPoolSize())
  37. server := newServer(cfg, store, pool, feedHandler, translator)
  38. scheduler.NewFeedScheduler(
  39. store,
  40. pool,
  41. cfg.PollingFrequency(),
  42. cfg.BatchSize(),
  43. )
  44. scheduler.NewCleanupScheduler(store, cfg.CleanupFrequency())
  45. <-stop
  46. logger.Info("Shutting down the server...")
  47. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  48. defer cancel()
  49. server.Shutdown(ctx)
  50. store.Close()
  51. logger.Info("Server gracefully stopped")
  52. }