From 1c44e5bc34bb28dc4472466f1915ee7eece7f14e Mon Sep 17 00:00:00 2001 From: shoopea Date: Thu, 16 Jan 2020 21:28:34 +0800 Subject: [PATCH] test clients backup --- client.go | 32 +++++++++++++++++++++++++++++--- cron.go | 4 ++++ main.go | 16 ++++++++-------- workers.go | 30 +++++++++++++++++------------- 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/client.go b/client.go index 1341aa3..bb0f4a3 100644 --- a/client.go +++ b/client.go @@ -6,8 +6,33 @@ import ( "log" "strings" "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) { muxClients.RLock() defer muxClients.RUnlock() @@ -119,9 +144,10 @@ func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) { muxClients.RUnlock() return c, true } else if createMissing { - c := new(ChirpClient) - c.TGUserID64 = id - c.Active = false + c := ChirpClient{ + TGUserID64: id, + Active: false, + } c.Mux.Lock() muxClients.RUnlock() muxClients.Lock() diff --git a/cron.go b/cron.go index 591e9e9..340b485 100644 --- a/cron.go +++ b/cron.go @@ -99,6 +99,10 @@ func cronSaveClients() { } muxClients.RUnlock() + for k, _ := range clts { + clts[k].Active = false + } + b, err := json.Marshal(clts) logOnError(err, "cronSaveClients : Marshal") diff --git a/main.go b/main.go index 294d24f..75090fc 100644 --- a/main.go +++ b/main.go @@ -86,6 +86,14 @@ func main() { 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) // Registering bot @@ -108,14 +116,6 @@ func main() { 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++ { go MQGetMsgWorker(w, MQCWMsgQueue) } diff --git a/workers.go b/workers.go index 72e17df..ece0151 100644 --- a/workers.go +++ b/workers.go @@ -207,20 +207,24 @@ func MQKeepAliveWorker() { } clt.Mux.Unlock() if clt.Active { - c := TGCommand{ - Type: commandSendMsg, - ToUserID64: x.TGUserID64, - 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 + if clt.CWRole == `` { + c := TGCommand{ + Type: commandSendMsg, + ToUserID64: x.TGUserID64, + 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.TGUserID64, `🏅Me`) + clientSendCWMsg(x.TGUserID64, `🏅Me`) + } else { + // silent reconnection + } } else { c := TGCommand{ Type: commandSendMsg,