Skip to content

Commit

Permalink
Add monitor shutdown func
Browse files Browse the repository at this point in the history
Signed-off-by: Toby Yan <me@tobyan.com>
  • Loading branch information
toby1991 committed Aug 2, 2019
1 parent 770f880 commit 21090ee
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 55 deletions.
23 changes: 23 additions & 0 deletions graceful/log.go
@@ -0,0 +1,23 @@
package graceful

import (
"github.com/totoval/framework/helpers/log"
"github.com/totoval/framework/helpers/toto"
)

func panicRecover(quietly bool) {
if err := recover(); err != nil {
logFatal(quietly, "Totoval shutting down failed", toto.V{"error": err})
}
}

func logInfo(quietly bool, msg string, v ...toto.V) {
if !quietly {
log.Info(msg, v...)
}
}
func logFatal(quietly bool, msg string, v ...toto.V) {
if !quietly {
log.Fatal(msg, v...)
}
}
42 changes: 42 additions & 0 deletions graceful/proccesor.go
@@ -0,0 +1,42 @@
package graceful

import (
"github.com/totoval/framework/cache"
"github.com/totoval/framework/helpers/m"
"github.com/totoval/framework/helpers/toto"
"github.com/totoval/framework/monitor"
"github.com/totoval/framework/queue"
)

func closeQueue(quietly bool) {
defer panicRecover(quietly)
logInfo(quietly, "Queue closing")
if err := queue.Queue().Close(); err != nil {
logFatal(quietly, "queue close failed", toto.V{"error": err})
}
logInfo(quietly, "Queue closed")
}
func closeDB(quietly bool) {
defer panicRecover(quietly)
logInfo(quietly, "Database closing")
if err := m.H().DB().Close(); err != nil {
logFatal(quietly, "database close failed", toto.V{"error": err})
}
logInfo(quietly, "Database closed")
}
func closeCache(quietly bool) {
defer panicRecover(quietly)
logInfo(quietly, "Cache closing")
if err := cache.Cache().Close(); err != nil {
logFatal(quietly, "cache close failed", toto.V{"error": err})
}
logInfo(quietly, "Cache closed")
}
func closeMonitor(quietly bool) {
defer panicRecover(quietly)
logInfo(quietly, "Monitor closing")
if err := monitor.Shutdown(); err != nil {
logFatal(quietly, "monitor close failed", toto.V{"error": err})
}
logInfo(quietly, "Monitor closed")
}
51 changes: 1 addition & 50 deletions graceful/shutdown.go
@@ -1,59 +1,10 @@
package graceful

import (
"github.com/totoval/framework/cache"
"github.com/totoval/framework/helpers/log"
"github.com/totoval/framework/helpers/m"
"github.com/totoval/framework/helpers/toto"
"github.com/totoval/framework/queue"
)

func ShutDown(quietly bool) {
logInfo(quietly, "Totoval is shutting down")
closeQueue(quietly)
closeCache(quietly)
closeDB(quietly)
closeMonitor(quietly)
logInfo(quietly, "Totoval is shut down")
}

func closeQueue(quietly bool) {
defer panicRecover(quietly)
logInfo(quietly, "Queue closing")
if err := queue.Queue().Close(); err != nil {
logFatal(quietly, "queue close failed", toto.V{"error": err})
}
logInfo(quietly, "Queue closed")
}
func closeDB(quietly bool) {
defer panicRecover(quietly)
logInfo(quietly, "Database closing")
if err := m.H().DB().Close(); err != nil {
logFatal(quietly, "database close failed", toto.V{"error": err})
}
logInfo(quietly, "Database closed")
}
func closeCache(quietly bool) {
defer panicRecover(quietly)
logInfo(quietly, "Cache closing")
if err := cache.Cache().Close(); err != nil {
logFatal(quietly, "cache close failed", toto.V{"error": err})
}
logInfo(quietly, "Cache closed")
}

func panicRecover(quietly bool) {
if err := recover(); err != nil {
logFatal(quietly, "Totoval shutting down failed", toto.V{"error": err})
}
}

func logInfo(quietly bool, msg string, v ...toto.V) {
if !quietly {
log.Info(msg, v...)
}
}
func logFatal(quietly bool, msg string, v ...toto.V) {
if !quietly {
log.Fatal(msg, v...)
}
}
3 changes: 3 additions & 0 deletions monitor/app/logics/dashboard/dashboard.go
Expand Up @@ -48,3 +48,6 @@ func (f *flow) notify() {
func (f *flow) Current() chan *FlowData {
return f.chanData
}
func (f *flow) Close() {
close(f.chanData)
}
5 changes: 0 additions & 5 deletions monitor/serve.go
Expand Up @@ -54,10 +54,5 @@ func HttpMonitorServe(parentCtx context.Context, wg *sync.WaitGroup) {
log.Fatal("Monitor Server Shutdown: ", toto.V{"error": err})
}

// totoval framework shutdown
//graceful.ShutDown(false)

log.Info("Monitor Server exiting")

wg.Done()
}
8 changes: 8 additions & 0 deletions monitor/shutdown.go
@@ -0,0 +1,8 @@
package monitor

import "github.com/totoval/framework/monitor/app/logics/dashboard"

func Shutdown() error {
dashboard.Flow.Close()
return nil
}

0 comments on commit 21090ee

Please sign in to comment.