diff --git a/workers.go b/workers.go index 8331a68..ba5e2f0 100644 --- a/workers.go +++ b/workers.go @@ -62,6 +62,7 @@ func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) { msgs <- x } } + log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing queue.\n") s.Close() } @@ -91,76 +92,74 @@ func MQKeepAliveWorker() { log.Printf("MQKeepAliveWorker : Session address : %p.\n", &s) log.Printf("MQKeepAliveWorker : Connection address : %p.\n", &s.MQConnection) log.Printf("MQKeepAliveWorker : Channel address : %p.\n", &s.MQChannel) - q, err := s.MQChannel.QueueDeclare( - s.Queue, // name - false, // durable - false, // delete when unused - false, // exclusive - false, // no-wait - nil, // arguments + /* + q, err := s.MQChannel.QueueDeclare( + s.Queue, // name + false, // durable + false, // delete when unused + false, // exclusive + false, // no-wait + nil, // arguments - ) - m, err := s.MQChannel.Consume( - q.Name, // queue - "", // consumer - true, // auto-ack - false, // exclusive - false, // no-local - false, // no-wait - nil, // args - ) - for d := range m { - log.Printf("MQKeepAliveWorker : Received a message: %s", string(d.Body)) - x := MQKeepAlive{} - err = json.Unmarshal(d.Body, &x) - logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body)) - if err == nil { - if x.Date.Add(10 * time.Second).Before(time.Now()) { - // outdated keep-alive coming from client - } else if v, ok := clientsKeepAlive.Load(x.UserID64); ok { - k := v.(*MQKeepAlive) - k.Date = x.Date - } else { - cs := MQSession{ - User: cfg.Rabbit.User, - Password: cfg.Rabbit.Password, - Host: cfg.Rabbit.Host, - Path: x.Queue, - SSL: false, - Queue: "msg", - isConnected: false, - } - err = cs.Open() - logOnError(err, "MQKeepAliveWorker : Failed to open MQ session") - clientsKeepAlive.Store(x.UserID64, &x) - clientsQueue[x.UserID64] = &cs + ) + m, err := s.MQChannel.Consume( + q.Name, // queue + "", // consumer + true, // auto-ack + false, // exclusive + false, // no-local + false, // no-wait + nil, // args + ) - c := TGCommand{ - Type: commandSendMsg, - ToUserID64: x.UserID64, - Text: "Your client is connected.", - } - TGCmdQueue <- c - c = TGCommand{ - Type: commandSendMsg, - ToUserID64: cfg.Bot.Admin, - Text: fmt.Sprintf("Client `%s` is connected.", x.Nickname), - } - TGCmdQueue <- c - clientSendCWMsg(x.UserID64, `🏅Me`) - /* - c = TGCommand{ - Type: commandSendMsg, - FromUserID64: x.UserID64, - ToChatID64: userID64ChtWrsBot, - Text: `/hero`, + for d := range m { + log.Printf("MQKeepAliveWorker : Received a message: %s", string(d.Body)) + x := MQKeepAlive{} + err = json.Unmarshal(d.Body, &x) + logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body)) + if err == nil { + if x.Date.Add(10 * time.Second).Before(time.Now()) { + // outdated keep-alive coming from client + } else if v, ok := clientsKeepAlive.Load(x.UserID64); ok { + k := v.(*MQKeepAlive) + k.Date = x.Date + } else { + cs := MQSession{ + User: cfg.Rabbit.User, + Password: cfg.Rabbit.Password, + Host: cfg.Rabbit.Host, + Path: x.Queue, + SSL: false, + Queue: "msg", + isConnected: false, } - MQTGCmdQueue <- c - */ + err = cs.Open() + logOnError(err, "MQKeepAliveWorker : Failed to open MQ session") + clientsKeepAlive.Store(x.UserID64, &x) + clientsQueue[x.UserID64] = &cs + + c := TGCommand{ + Type: commandSendMsg, + ToUserID64: x.UserID64, + Text: "Your client is connected.", + } + TGCmdQueue <- c + c = TGCommand{ + Type: commandSendMsg, + ToUserID64: cfg.Bot.Admin, + Text: fmt.Sprintf("Client `%s` is connected.", x.Nickname), + } + TGCmdQueue <- c + + clientSendCWMsg(x.UserID64, `🏅Me`) + clientSendCWMsg(x.UserID64, `/hero`) + } } } - } + */ + s.Close() + log.Printf("MQKeepAliveWorker : Closing queue.\n") } log.Printf("MQKeepAliveWorker : Closing.")