diff --git a/eventSubscribe.go b/eventSubscribe.go index e0fa62f..3a4acf6 100644 --- a/eventSubscribe.go +++ b/eventSubscribe.go @@ -11,7 +11,9 @@ import ( func onSubscribe(in InWechatMsg) { openID := in.header.FromUserName //getUser fromCRM - log.Println(openID) + info := WechatUserInfo{} + info.getUserInfo(in.header.FromUserName, "zh-CN") + log.Println(openID + " information get above onSubscribe") } //WechatUserInfo response for wechat user info. @@ -19,7 +21,7 @@ type WechatUserInfo struct { Subscribe int32 `json:"subscribe"` OpenID string `json:"openid"` NickName string `json:"nickname"` - Sex int32 `json:"sex"` + Sex int32 `json:"sex"` //0 未知 1 男 2 女 Language string `json:"language"` City string `json:"city"` Province string `json:"province"` @@ -32,16 +34,16 @@ type WechatUserInfo struct { TagIDList []int32 `json:"tagid_list"` } -func getUserInfo(openID string, lang string) (result WechatUserInfo) { - url := result.getURL(openID, lang) +func (m *WechatUserInfo) getUserInfo(openID string, lang string) { + url := m.getURL(openID, lang) resp, err := http.Get(url) if err != nil { } b, err := ioutil.ReadAll(resp.Body) - err = json.Unmarshal(b, &result) - return + err = json.Unmarshal(b, m) + log.Println(m) } - +//lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 func (m *WechatUserInfo) getURL(openID, lang string) string { atk, _ := GetAccessToken() u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=%s", atk, openID, lang) diff --git a/sessionManager.go b/sessionManager.go index c60cf39..1ca540b 100644 --- a/sessionManager.go +++ b/sessionManager.go @@ -37,8 +37,8 @@ var AllInMessage chan InWechatMsg func (m *SessionManager) startSessionManager(in <-chan InWechatMsg) { log.Println("session manager start..") m.sessions = map[string]openIDSession{} - m.done = make(chan workDone, 2000) - for { //forever looping + m.done = make(chan workDone, 2000) //2000 different openID simultaneously. + for { //forever looping select { case msg := <-in: log.Printf("SessionMgr : incoming msg %s (%s)", msg.header.FromUserName, msg.header.MsgType) @@ -67,10 +67,10 @@ func (m *SessionManager) addJob(v InWechatMsg) (jobCount int) { s.count++ m.sessions[openID] = s - log.Printf("Incoming message in queue %d", s.count) - log.Printf("Total Session count = %d", len(m.sessions)) + log.Printf("SessionMgr/addJob: Incoming message in queue %d", s.count) + log.Printf("SessionMgr/addJob: Total Session count = %d", len(m.sessions)) if s.count <= 0 { - log.Fatal("new job added, but count <= 0") + log.Fatal("SessionMgr/Addjob: new job added, but count <= 0") } return s.count } @@ -88,22 +88,25 @@ func (m *SessionManager) createSession(openID string) openIDSession { func (m *SessionManager) clearJobDone(d workDone) { s, found := m.sessions[d.openID] + log.Println("SessionMgr/clearJobDone: start clearning jobs ... ") log.Println(s) + log.Println(d) if found { s.count -= d.consumed - if s.count == 0 { //no job to do + m.sessions[d.openID] = s //update + if s.count == 0 { //no job to do //remove from memory m.destroySession(d.openID) - log.Printf("destroy session %s", d.openID) + log.Printf("SessionMgr/clearJobDone: destroy session %s", d.openID) } else if s.count > 0 { go m.startJob(d.openID) //processing any newly coming jobs } else { log.Println(s) - log.Fatal("session job count cannot be negative, problem session") + log.Fatal("SessionMgr/clearJobDone: session job count cannot be negative, problem session") } } else { log.Println(d) - log.Fatal("When job done, we canot find proper session") + log.Fatal("SessionMgr/clearJobDone: When job done, we canot find proper session") } } @@ -117,12 +120,12 @@ func (m *SessionManager) destroySession(openID string) { close(s.jobs) //delete it from memory delete(m.sessions, openID) - log.Printf("total session count=%d", len(m.sessions)) + log.Printf("SessionMgr/destroySession: total session count=%d", len(m.sessions)) } -//worker thread +//worker thread, cannot change session info func (m *SessionManager) startJob(openID string) { - log.Println("start job worker...") + log.Println("SessionMgr/startJob: enter...") jobFinished := workDone{openID, 0} //process all jobs in the channel hasJob := true @@ -132,7 +135,8 @@ func (m *SessionManager) startJob(openID string) { m.checkOpenID(openID, v) s := m.sessions[openID] s.data.incomingMsg(v) //<=== main logic for processing each incoming message - m.sessions[openID] = s + log.Println("in worker thread, set s to session") + log.Println(s) jobFinished.consumed++ default: hasJob = false @@ -144,8 +148,8 @@ func (m *SessionManager) startJob(openID string) { func (m *SessionManager) checkOpenID(openID string, v InWechatMsg) { if v.header.FromUserName != openID { - log.Println("Error: Weird Message below ...") + log.Println("Error:[SessionMgr/checkOpenID] Weird Message below ...") log.Println(v) - log.Fatalf("Error: worker thread for %s, see different id=%s \n", openID, v.header.FromUserName) + log.Fatalf("Error:[[SessionMgr/checkOpenID]] worker thread for %s, see different id=%s \n", openID, v.header.FromUserName) } } diff --git a/sync_wechat_token.sh b/sync_wechat_token.sh new file mode 100755 index 0000000..c54fc14 --- /dev/null +++ b/sync_wechat_token.sh @@ -0,0 +1,8 @@ +#!/bin/bash +FILE=`find /tmp -path "/tmp/ssh-*" -user sp -type s -iname "agent.*" 2>/dev/null` +export SSH_AUTH_SOCK="$FILE" + +#download +SSH_AUTH_SOCK="$FILE" rsync --update root@biukop.com.au:/tmp/wechat_hitxy_token /tmp/wechat_hitxy_token +#upload +SSH_AUTH_SOCK="$FILE" rsync --update /tmp/wechat_hitxy_token root@biukop.com.au:/tmp/wechat_hitxy_token