test clients backup

This commit is contained in:
shoopea 2020-01-16 21:28:34 +08:00
parent 5e25478137
commit 1c44e5bc34
4 changed files with 58 additions and 24 deletions

View File

@ -6,8 +6,33 @@ import (
"log" "log"
"strings" "strings"
"time" "time"
"github.com/jinzhu/copier"
) )
func loadClients() {
var clts []ChirpClient
b, err := ioutil.ReadFile("./clients.json")
logOnError(err, "loadClients : ReadFile(./clients.json)")
if err != nil {
return err
}
err = json.Unmarshal(b, &clts)
logOnError(err, "loadClients : Unmarshal(./clients.json)")
muxClients.Lock()
for _, c := range clts {
cx := ChirpClient{}
copier.Copy(&cx, &c)
cx.Active = false
clients[cx.TGUserID64] = cx
}
muxClients.Unlock()
}
func getLockedRoleClient(role string) (*ChirpClient, error) { func getLockedRoleClient(role string) (*ChirpClient, error) {
muxClients.RLock() muxClients.RLock()
defer muxClients.RUnlock() defer muxClients.RUnlock()
@ -119,9 +144,10 @@ func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) {
muxClients.RUnlock() muxClients.RUnlock()
return c, true return c, true
} else if createMissing { } else if createMissing {
c := new(ChirpClient) c := ChirpClient{
c.TGUserID64 = id TGUserID64: id,
c.Active = false Active: false,
}
c.Mux.Lock() c.Mux.Lock()
muxClients.RUnlock() muxClients.RUnlock()
muxClients.Lock() muxClients.Lock()

View File

@ -99,6 +99,10 @@ func cronSaveClients() {
} }
muxClients.RUnlock() muxClients.RUnlock()
for k, _ := range clts {
clts[k].Active = false
}
b, err := json.Marshal(clts) b, err := json.Marshal(clts)
logOnError(err, "cronSaveClients : Marshal") logOnError(err, "cronSaveClients : Marshal")

16
main.go
View File

@ -86,6 +86,14 @@ func main() {
initDB() initDB()
} }
MQCWMsgQueue = make(chan ChatWarsMessage, MQCWMsgQueueSize)
SQLMsgIdentifyQueue = make(chan int64, SQLMsgIdentifyQueueSize)
TGCmdQueue = make(chan TGCommand, TGCmdQueueSize)
MQTGCmdQueue = make(chan TGCommand, MQTGCmdQueueSize)
JobQueue = make(chan Job, JobQueueSize)
clients = make(map[int64]*ChirpClient)
callbacks = make(map[int64]map[int64][]int64)
initCache(*initdb) initCache(*initdb)
// Registering bot // Registering bot
@ -108,14 +116,6 @@ func main() {
cr = startCron() cr = startCron()
MQCWMsgQueue = make(chan ChatWarsMessage, MQCWMsgQueueSize)
SQLMsgIdentifyQueue = make(chan int64, SQLMsgIdentifyQueueSize)
TGCmdQueue = make(chan TGCommand, TGCmdQueueSize)
MQTGCmdQueue = make(chan TGCommand, MQTGCmdQueueSize)
JobQueue = make(chan Job, JobQueueSize)
clients = make(map[int64]*ChirpClient)
callbacks = make(map[int64]map[int64][]int64)
for w := 1; w <= MQGetMsgWorkers; w++ { for w := 1; w <= MQGetMsgWorkers; w++ {
go MQGetMsgWorker(w, MQCWMsgQueue) go MQGetMsgWorker(w, MQCWMsgQueue)
} }

View File

@ -207,20 +207,24 @@ func MQKeepAliveWorker() {
} }
clt.Mux.Unlock() clt.Mux.Unlock()
if clt.Active { if clt.Active {
c := TGCommand{ if clt.CWRole == `` {
Type: commandSendMsg, c := TGCommand{
ToUserID64: x.TGUserID64, Type: commandSendMsg,
Text: "Your client is connected.", ToUserID64: x.TGUserID64,
} Text: "Your client is connected.",
TGCmdQueue <- c }
c = TGCommand{ TGCmdQueue <- c
Type: commandSendMsg, c = TGCommand{
ToUserID64: cfg.Bot.Admin, Type: commandSendMsg,
Text: fmt.Sprintf("Client `%s` is connected.", x.Nickname), ToUserID64: cfg.Bot.Admin,
} Text: fmt.Sprintf("Client `%s` is connected.", x.Nickname),
TGCmdQueue <- c }
TGCmdQueue <- c
clientSendCWMsg(x.TGUserID64, `🏅Me`) clientSendCWMsg(x.TGUserID64, `🏅Me`)
} else {
// silent reconnection
}
} else { } else {
c := TGCommand{ c := TGCommand{
Type: commandSendMsg, Type: commandSendMsg,