daemon.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "runtime"
  11. "syscall"
  12. "time"
  13. "miniflux.app/config"
  14. "miniflux.app/logger"
  15. "miniflux.app/reader/feed"
  16. "miniflux.app/service/scheduler"
  17. "miniflux.app/service/httpd"
  18. "miniflux.app/storage"
  19. "miniflux.app/worker"
  20. )
  21. func startDaemon(cfg *config.Config, 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, cfg.WorkerPoolSize())
  28. go showProcessStatistics()
  29. if cfg.HasSchedulerService() {
  30. scheduler.Serve(cfg, store, pool)
  31. }
  32. var httpServer *http.Server
  33. if cfg.HasHTTPService() {
  34. httpServer = httpd.Serve(cfg, store, pool, feedHandler)
  35. }
  36. <-stop
  37. logger.Info("Shutting down the process...")
  38. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  39. defer cancel()
  40. if httpServer != nil {
  41. httpServer.Shutdown(ctx)
  42. }
  43. logger.Info("Process gracefully stopped")
  44. }
  45. func showProcessStatistics() {
  46. for {
  47. var m runtime.MemStats
  48. runtime.ReadMemStats(&m)
  49. logger.Debug("Sys=%vK, InUse=%vK, HeapInUse=%vK, StackSys=%vK, StackInUse=%vK, GoRoutines=%d, NumCPU=%d",
  50. m.Sys/1024, (m.Sys-m.HeapReleased)/1024, m.HeapInuse/1024, m.StackSys/1024, m.StackInuse/1024,
  51. runtime.NumGoroutine(), runtime.NumCPU())
  52. time.Sleep(30 * time.Second)
  53. }
  54. }