daemon.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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("Sys=%vK, InUse=%vK, HeapInUse=%vK, StackSys=%vK, StackInUse=%vK, GoRoutines=%d, NumCPU=%d",
  30. m.Sys/1024, (m.Sys-m.HeapReleased)/1024, m.HeapInuse/1024, m.StackSys/1024, m.StackInuse/1024,
  31. runtime.NumGoroutine(), runtime.NumCPU())
  32. time.Sleep(30 * time.Second)
  33. }
  34. }()
  35. translator := locale.Load()
  36. feedHandler := feed.NewFeedHandler(store, translator)
  37. pool := scheduler.NewWorkerPool(feedHandler, cfg.WorkerPoolSize())
  38. server := newServer(cfg, store, pool, feedHandler, translator)
  39. scheduler.NewFeedScheduler(
  40. store,
  41. pool,
  42. cfg.PollingFrequency(),
  43. cfg.BatchSize(),
  44. )
  45. scheduler.NewCleanupScheduler(store, cfg.CleanupFrequency())
  46. <-stop
  47. logger.Info("Shutting down the server...")
  48. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  49. defer cancel()
  50. server.Shutdown(ctx)
  51. store.Close()
  52. logger.Info("Server gracefully stopped")
  53. }