code refactor

This commit is contained in:
Ulric Qin 2020-11-07 08:18:33 +08:00
parent 282aede691
commit 712d0051d9
4 changed files with 19 additions and 14 deletions

View File

@ -77,5 +77,5 @@ func endingProc() {
logger.Close()
http.Shutdown()
fmt.Println("portal stopped successfully")
fmt.Println("process stopped successfully")
}

View File

@ -105,5 +105,5 @@ func endingProc() {
logger.Close()
http.Shutdown()
fmt.Println("portal stopped successfully")
fmt.Println("process stopped successfully")
}

View File

@ -6,6 +6,8 @@ import (
"github.com/streadway/amqp"
"github.com/toolkits/pkg/logger"
"github.com/didi/nightingale/src/modules/rdb/config"
)
var (
@ -13,7 +15,14 @@ var (
exit = make(chan bool)
)
func Init(url string) {
func Init() {
if config.Config.RabbitMQ.Enable {
dial(config.Config.RabbitMQ.Addr)
go Consume(config.Config.RabbitMQ.Addr, config.Config.RabbitMQ.Queue)
}
}
func dial(url string) {
var err error
conn, err = amqp.Dial(url)
if err != nil {
@ -70,6 +79,8 @@ func close() {
}
func Shutdown() {
conn.Close()
exit <- true
if config.Config.RabbitMQ.Enable {
conn.Close()
exit <- true
}
}

View File

@ -68,10 +68,7 @@ func main() {
cron.InitWorker()
// 初始化 rabbitmq 处理部分异步逻辑
if config.Config.RabbitMQ.Enable {
rabbitmq.Init(config.Config.RabbitMQ.Addr)
go rabbitmq.Consume(config.Config.RabbitMQ.Addr, config.Config.RabbitMQ.Queue)
}
rabbitmq.Init()
go cron.ConsumeMail()
go cron.ConsumeSms()
@ -101,10 +98,7 @@ func endingProc() {
logger.Close()
http.Shutdown()
redisc.CloseRedis()
rabbitmq.Shutdown()
if config.Config.RabbitMQ.Enable {
rabbitmq.Shutdown()
}
fmt.Println("stopped successfully")
fmt.Println("process stopped successfully")
}