test
This commit is contained in:
parent
80b4027776
commit
b98069f4ba
127
workers.go
127
workers.go
@ -62,6 +62,7 @@ func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
|
|||||||
msgs <- x
|
msgs <- x
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing queue.\n")
|
||||||
s.Close()
|
s.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,76 +92,74 @@ func MQKeepAliveWorker() {
|
|||||||
log.Printf("MQKeepAliveWorker : Session address : %p.\n", &s)
|
log.Printf("MQKeepAliveWorker : Session address : %p.\n", &s)
|
||||||
log.Printf("MQKeepAliveWorker : Connection address : %p.\n", &s.MQConnection)
|
log.Printf("MQKeepAliveWorker : Connection address : %p.\n", &s.MQConnection)
|
||||||
log.Printf("MQKeepAliveWorker : Channel address : %p.\n", &s.MQChannel)
|
log.Printf("MQKeepAliveWorker : Channel address : %p.\n", &s.MQChannel)
|
||||||
q, err := s.MQChannel.QueueDeclare(
|
/*
|
||||||
s.Queue, // name
|
q, err := s.MQChannel.QueueDeclare(
|
||||||
false, // durable
|
s.Queue, // name
|
||||||
false, // delete when unused
|
false, // durable
|
||||||
false, // exclusive
|
false, // delete when unused
|
||||||
false, // no-wait
|
false, // exclusive
|
||||||
nil, // arguments
|
false, // no-wait
|
||||||
|
nil, // arguments
|
||||||
|
|
||||||
)
|
)
|
||||||
m, err := s.MQChannel.Consume(
|
m, err := s.MQChannel.Consume(
|
||||||
q.Name, // queue
|
q.Name, // queue
|
||||||
"", // consumer
|
"", // consumer
|
||||||
true, // auto-ack
|
true, // auto-ack
|
||||||
false, // exclusive
|
false, // exclusive
|
||||||
false, // no-local
|
false, // no-local
|
||||||
false, // no-wait
|
false, // no-wait
|
||||||
nil, // args
|
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
|
|
||||||
|
|
||||||
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`)
|
for d := range m {
|
||||||
/*
|
log.Printf("MQKeepAliveWorker : Received a message: %s", string(d.Body))
|
||||||
c = TGCommand{
|
x := MQKeepAlive{}
|
||||||
Type: commandSendMsg,
|
err = json.Unmarshal(d.Body, &x)
|
||||||
FromUserID64: x.UserID64,
|
logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body))
|
||||||
ToChatID64: userID64ChtWrsBot,
|
if err == nil {
|
||||||
Text: `/hero`,
|
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.")
|
log.Printf("MQKeepAliveWorker : Closing.")
|
||||||
|
Loading…
Reference in New Issue
Block a user