aboutsummaryrefslogtreecommitdiffhomepage
path: root/daemon/daemon.go
blob: de91d71ae9b4f8f314825ae13e757495487f0639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package daemon

import (
	"context"
	"os"
	"os/signal"
	"runtime"
	"syscall"
	"time"

	"github.com/miniflux/miniflux/config"
	"github.com/miniflux/miniflux/locale"
	"github.com/miniflux/miniflux/logger"
	"github.com/miniflux/miniflux/reader/feed"
	"github.com/miniflux/miniflux/scheduler"
	"github.com/miniflux/miniflux/storage"
)

// Run starts the daemon.
func Run(cfg *config.Config, store *storage.Storage) {
	logger.Info("Starting Miniflux...")

	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt)
	signal.Notify(stop, syscall.SIGTERM)

	go func() {
		for {
			var m runtime.MemStats
			runtime.ReadMemStats(&m)
			logger.Debug("Sys=%vK, InUse=%vK, HeapInUse=%vK, StackSys=%vK, StackInUse=%vK, GoRoutines=%d, NumCPU=%d",
				m.Sys/1024, (m.Sys-m.HeapReleased)/1024, m.HeapInuse/1024, m.StackSys/1024, m.StackInuse/1024,
				runtime.NumGoroutine(), runtime.NumCPU())
			time.Sleep(30 * time.Second)
		}
	}()

	translator := locale.Load()
	feedHandler := feed.NewFeedHandler(store, translator)
	pool := scheduler.NewWorkerPool(feedHandler, cfg.WorkerPoolSize())
	server := newServer(cfg, store, pool, feedHandler, translator)

	scheduler.NewFeedScheduler(
		store,
		pool,
		cfg.PollingFrequency(),
		cfg.BatchSize(),
	)

	scheduler.NewCleanupScheduler(store, cfg.CleanupFrequency())

	<-stop
	logger.Info("Shutting down the server...")
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	server.Shutdown(ctx)
	logger.Info("Server gracefully stopped")
}