2018-11-11 18:32:48 -05:00
|
|
|
// 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 cli // import "miniflux.app/cli"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-11 18:54:19 -05:00
|
|
|
"net/http"
|
2018-11-11 18:32:48 -05:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"miniflux.app/config"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/reader/feed"
|
|
|
|
"miniflux.app/service/scheduler"
|
|
|
|
"miniflux.app/service/httpd"
|
|
|
|
"miniflux.app/storage"
|
|
|
|
"miniflux.app/worker"
|
|
|
|
)
|
|
|
|
|
|
|
|
func startDaemon(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)
|
|
|
|
|
|
|
|
feedHandler := feed.NewFeedHandler(store)
|
|
|
|
pool := worker.NewPool(feedHandler, cfg.WorkerPoolSize())
|
|
|
|
|
|
|
|
go showProcessStatistics()
|
|
|
|
|
2018-11-11 18:54:19 -05:00
|
|
|
if cfg.HasSchedulerService() {
|
|
|
|
scheduler.Serve(cfg, store, pool)
|
|
|
|
}
|
|
|
|
|
|
|
|
var httpServer *http.Server
|
|
|
|
if cfg.HasHTTPService() {
|
|
|
|
httpServer = httpd.Serve(cfg, store, pool, feedHandler)
|
|
|
|
}
|
2018-11-11 18:32:48 -05:00
|
|
|
|
|
|
|
<-stop
|
|
|
|
logger.Info("Shutting down the process...")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-11-11 18:54:19 -05:00
|
|
|
if httpServer != nil {
|
|
|
|
httpServer.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:32:48 -05:00
|
|
|
logger.Info("Process gracefully stopped")
|
|
|
|
}
|
|
|
|
|
|
|
|
func showProcessStatistics() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|