1

Fix shutdown, fix broken announces

This commit is contained in:
Vladimir Hodakov 2019-01-09 03:24:12 +04:00
parent 7612ce778f
commit dca290beb1
Signed by: Vladimir Hodakov
GPG Key ID: 673980B6882F82C6
4 changed files with 16 additions and 10 deletions

View File

@ -13,12 +13,14 @@ const VERSION = "0.0.2"
// Context is the main application context. // Context is the main application context.
type Context struct { type Context struct {
Config *config.Struct Config *config.Struct
Logger zerolog.Logger Logger zerolog.Logger
ShutdownDone chan bool
} }
// NewContext is an initialization function for Context // NewContext is an initialization function for Context
func NewContext() *Context { func NewContext() *Context {
c := &Context{} c := &Context{}
c.ShutdownDone = make(chan bool, 1)
return c return c
} }

View File

@ -16,6 +16,8 @@ func fwMessagesFilter(msg *tdlib.TdMessage) bool {
// ZookeeperReceiver adds announcement functionality to bot // ZookeeperReceiver adds announcement functionality to bot
func ZookeeperReceiver(client *tdlib.Client) { func ZookeeperReceiver(client *tdlib.Client) {
// Where to send announces
announcerID := int64(665790161)
log.Debug().Msg("Adding receiver for @FWorldBot messages...") log.Debug().Msg("Adding receiver for @FWorldBot messages...")
// Here we can add a receiver to retreive any message type we want // Here we can add a receiver to retreive any message type we want
// We like to get UpdateNewMessage events and with a specific FilterFunc // We like to get UpdateNewMessage events and with a specific FilterFunc
@ -63,10 +65,13 @@ func ZookeeperReceiver(client *tdlib.Client) {
replyText := battleType + " " + battleTag replyText := battleType + " " + battleTag
reply := tdlib.NewInputMessageText(tdlib.NewFormattedText(replyText, nil), true, true) reply := tdlib.NewInputMessageText(tdlib.NewFormattedText(replyText, nil), true, true)
_, err := client.SendMessage(updateMsg.Message.ChatID, 0, false, true, nil, reply) _, err := client.SendMessage(announcerID, 0, false, true, nil, reply)
if err != nil { if err != nil {
log.Error().Err(err) log.Error().Err(err)
} }
} }
<-c.ShutdownDone
return
} }
} }

View File

@ -78,14 +78,14 @@ func Connect() {
log.Debug().Msg("Connection with Telegram established") log.Debug().Msg("Connection with Telegram established")
for update := range rawUpdates { for update := range rawUpdates {
log.Debug().Msgf("Update of type %s received", update.Data["@type"]) log.Debug().Msgf("Update of type %s received", update.Data["@type"])
<-c.ShutdownDone
return
} }
} }
// Shutdown disconnects from Telegram // Shutdown disconnects from Telegram
func Shutdown() { func Shutdown() {
_, err := client.Destroy() client.DestroyInstance()
if err != nil {
log.Error().Err(err).Msg("Failed to destroy Telegram client instance")
}
log.Info().Msg("Connection with Telegram closed") log.Info().Msg("Connection with Telegram closed")
} }

View File

@ -27,7 +27,6 @@ func main() {
// CTRL+C handler. // CTRL+C handler.
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt) signal.Notify(interrupt)
shutdownDone := make(chan bool, 1)
go func() { go func() {
signalThing := <-interrupt signalThing := <-interrupt
if signalThing == syscall.SIGTERM || signalThing == syscall.SIGINT { if signalThing == syscall.SIGTERM || signalThing == syscall.SIGINT {
@ -35,14 +34,14 @@ func main() {
telegram.Shutdown() telegram.Shutdown()
shutdownDone <- true c.ShutdownDone <- true
} }
}() }()
announcesv1.New(c) announcesv1.New(c)
telegram.New(c) telegram.New(c)
<-shutdownDone <-c.ShutdownDone
os.Exit(0) os.Exit(0)
} }