From 487852f07eb191ef56967b7b7d7f01537a55eabd Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 11 Nov 2018 15:32:48 -0800 Subject: Replace daemon and scheduler package with service package --- cli/cli.go | 33 ++++++++++++++++----------------- cli/daemon.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 cli/daemon.go (limited to 'cli') diff --git a/cli/cli.go b/cli/cli.go index aa4d4b3..629367a 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -9,7 +9,6 @@ import ( "fmt" "miniflux.app/config" - "miniflux.app/daemon" "miniflux.app/database" "miniflux.app/logger" "miniflux.app/storage" @@ -17,27 +16,27 @@ import ( ) const ( - flagInfoHelp = "Show application information" - flagVersionHelp = "Show application version" - flagMigrateHelp = "Run SQL migrations" - flagFlsuhSessionsHelp = "Flush all sessions (disconnect users)" - flagCreateAdminHelp = "Create admin user" - flagResetPasswordHelp = "Reset user password" + flagInfoHelp = "Show application information" + flagVersionHelp = "Show application version" + flagMigrateHelp = "Run SQL migrations" + flagFlsuhSessionsHelp = "Flush all sessions (disconnect users)" + flagCreateAdminHelp = "Create admin user" + flagResetPasswordHelp = "Reset user password" flagResetFeedErrorsHelp = "Clear all feed errors for all users" - flagDebugModeHelp = "Show debug logs" + flagDebugModeHelp = "Show debug logs" ) // Parse parses command line arguments. func Parse() { var ( - flagInfo bool - flagVersion bool - flagMigrate bool - flagFlushSessions bool - flagCreateAdmin bool - flagResetPassword bool + flagInfo bool + flagVersion bool + flagMigrate bool + flagFlushSessions bool + flagCreateAdmin bool + flagResetPassword bool flagResetFeedErrors bool - flagDebugMode bool + flagDebugMode bool ) flag.BoolVar(&flagInfo, "info", false, flagInfoHelp) @@ -49,7 +48,7 @@ func Parse() { flag.BoolVar(&flagCreateAdmin, "create-admin", false, flagCreateAdminHelp) flag.BoolVar(&flagResetPassword, "reset-password", false, flagResetPasswordHelp) flag.BoolVar(&flagResetFeedErrors, "reset-feed-errors", false, flagResetFeedErrorsHelp) - flag.BoolVar(&flagDebugMode,"debug", false, flagDebugModeHelp) + flag.BoolVar(&flagDebugMode, "debug", false, flagDebugModeHelp) flag.Parse() cfg := config.NewConfig() @@ -111,5 +110,5 @@ func Parse() { createAdmin(store) } - daemon.Run(cfg, store) + startDaemon(cfg, store) } diff --git a/cli/daemon.go b/cli/daemon.go new file mode 100644 index 0000000..436729c --- /dev/null +++ b/cli/daemon.go @@ -0,0 +1,57 @@ +// 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" + "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 scheduler.Serve(cfg, store, pool) + go showProcessStatistics() + + httpServer := httpd.Serve(cfg, store, pool, feedHandler) + + <-stop + logger.Info("Shutting down the process...") + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + httpServer.Shutdown(ctx) + 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) + } +} \ No newline at end of file -- cgit v1.2.3