daemon.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "syscall"
  10. "time"
  11. "github.com/miniflux/miniflux/config"
  12. "github.com/miniflux/miniflux/locale"
  13. "github.com/miniflux/miniflux/logger"
  14. "github.com/miniflux/miniflux/reader/feed"
  15. "github.com/miniflux/miniflux/scheduler"
  16. "github.com/miniflux/miniflux/storage"
  17. )
  18. // Run starts the daemon.
  19. func Run(cfg *config.Config, store *storage.Storage) {
  20. logger.Info("Starting Miniflux...")
  21. stop := make(chan os.Signal, 1)
  22. signal.Notify(stop, os.Interrupt)
  23. signal.Notify(stop, syscall.SIGTERM)
  24. translator := locale.Load()
  25. feedHandler := feed.NewFeedHandler(store, translator)
  26. pool := scheduler.NewWorkerPool(feedHandler, cfg.WorkerPoolSize())
  27. server := newServer(cfg, store, pool, feedHandler, translator)
  28. scheduler.NewFeedScheduler(
  29. store,
  30. pool,
  31. cfg.PollingFrequency(),
  32. cfg.BatchSize(),
  33. )
  34. scheduler.NewSessionScheduler(store, cfg.SessionCleanupFrequency())
  35. <-stop
  36. logger.Info("Shutting down the server...")
  37. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  38. server.Shutdown(ctx)
  39. store.Close()
  40. logger.Info("Server gracefully stopped")
  41. }