main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. mu "mfw/util"
  6. "net"
  7. "os"
  8. "os/signal"
  9. "qfw/util"
  10. "qfw/util/elastic"
  11. "syscall"
  12. "time"
  13. )
  14. var (
  15. udpclient mu.UdpClient //udp对象
  16. SingleThread = make(chan bool, 1)
  17. SingleClear = 0
  18. toaddr = []*net.UDPAddr{} //下节点对象
  19. ChSign = make(chan os.Signal)
  20. Es *elastic.Elastic
  21. Index string
  22. Itype string
  23. sid, eid string //测试使用
  24. )
  25. func init() {
  26. signal.Notify(ChSign)
  27. go DealSign()
  28. nextNode := util.ObjArrToMapArr(Sysconfig["nextNode"].([]interface{}))
  29. for _, m := range nextNode {
  30. toaddr = append(toaddr, &net.UDPAddr{
  31. IP: net.ParseIP(m["addr"].(string)),
  32. Port: util.IntAll(m["port"]),
  33. })
  34. }
  35. es := Sysconfig["es"].(map[string]interface{})
  36. Es = &elastic.Elastic{
  37. S_esurl: util.ObjToString(es["addr"]),
  38. I_size: util.IntAllDef(es["pool"], 10),
  39. }
  40. Index = util.ObjToString(es["index"])
  41. Itype = util.ObjToString(es["itype"])
  42. Es.InitElasticSize()
  43. }
  44. var queryClose = make(chan bool)
  45. var queryCloseOver = make(chan bool)
  46. func DealSign() {
  47. for {
  48. select {
  49. case sign := <-ChSign:
  50. //log.Println("receive:", sign)
  51. if v, ok := sign.(syscall.Signal); ok && v == os.Interrupt {
  52. log.Println("receice signal..,start close iter")
  53. if P_QL.Brun {
  54. queryClose <- true
  55. select {
  56. case <-queryCloseOver:
  57. case <-time.After(30 * time.Second):
  58. }
  59. }
  60. util.ReadConfig(&Sysconfig)
  61. log.Println("signal deal over")
  62. }
  63. }
  64. }
  65. }
  66. func main() {
  67. //udp跑增量 id段 project
  68. //udp跑全量 qlT
  69. //udp跑历史数据 信息id1,id2/或id段 ls
  70. //udp强制合并 信息id1,id2,id3 [项目id] 不存在时新建 qzhb
  71. //udp强制拆分 项目id,信息id1,id2 qzcf
  72. //udp重新合并 信息id1,id2,id3 cxhb
  73. if Sysconfig["loadStart"] != nil {
  74. loadStart := util.Int64All(Sysconfig["loadStart"])
  75. if loadStart > -1 {
  76. P_QL.loadData(loadStart)
  77. }
  78. }
  79. P_QL.loadSite()
  80. go checkMapJob()
  81. time.Sleep(99999 * time.Hour)
  82. }
  83. //测试组人员使用
  84. func mainT() {
  85. sid = "56388138af53745d9a000001"
  86. eid = "5b671f32a5cb26b9b76ddbb6"
  87. //flag.StringVar(&sid, "sid", "", "开始id")
  88. //flag.StringVar(&eid, "eid", "", "结束id")
  89. //flag.Parse()
  90. mapinfo := map[string]interface{}{}
  91. if sid == "" || eid == "" {
  92. log.Println("sid, eid参数不能为空")
  93. os.Exit(0)
  94. }
  95. mapinfo["gtid"] = sid
  96. mapinfo["lteid"] = eid
  97. mapinfo["stype"] = "ql"
  98. mapinfo["ip"] = "127.0.0.1"
  99. mapinfo["port"] = Sysconfig["udpport"]
  100. if Sysconfig["loadStart"] != nil {
  101. loadStart := util.Int64All(Sysconfig["loadStart"])
  102. if loadStart > -1 {
  103. P_QL.loadData(loadStart)
  104. }
  105. }
  106. P_QL.loadSite()
  107. P_QL.currentType = mapinfo["stype"].(string)
  108. P_QL.pici = time.Now().Unix()
  109. P_QL.taskQl(mapinfo)
  110. time.Sleep(20 * time.Second)
  111. }
  112. func mainS() {
  113. id := "5987e5e85d11e1c745d36c4c"
  114. mapinfo := map[string]interface{}{}
  115. mapinfo["id"] = id
  116. mapinfo["stype"] = "updateMoney"
  117. mapinfo["budget"] = "12345"
  118. mapinfo["ip"] = "127.0.0.1"
  119. mapinfo["port"] = Sysconfig["udpport"]
  120. if Sysconfig["loadStart"] != nil {
  121. loadStart := util.Int64All(Sysconfig["loadStart"])
  122. if loadStart > -1 {
  123. P_QL.loadData(loadStart)
  124. }
  125. }
  126. P_QL.loadSite()
  127. P_QL.currentType = mapinfo["stype"].(string)
  128. P_QL.pici = time.Now().Unix()
  129. P_QL.taskUpdateMoney(mapinfo)
  130. P_QL.Brun = true
  131. time.Sleep(20 * time.Second)
  132. }
  133. //udp调用信号
  134. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  135. switch act {
  136. case mu.OP_TYPE_DATA: //上个节点的数据
  137. var mapInfo map[string]interface{}
  138. err := json.Unmarshal(data, &mapInfo)
  139. log.Println("err:", err, "mapInfo:", mapInfo)
  140. if err != nil {
  141. _ = udpclient.WriteUdp([]byte("err:"+err.Error()), mu.OP_NOOP, ra)
  142. } else if mapInfo != nil {
  143. key, _ := mapInfo["key"].(string)
  144. if key == "" {
  145. key = "udpok"
  146. }
  147. go udpclient.WriteUdp([]byte(key), mu.OP_NOOP, ra)
  148. SingleThread <- true
  149. tasktype, _ := mapInfo["stype"].(string)
  150. switch tasktype {
  151. case "ql": //全量合并
  152. go func() {
  153. defer func() {
  154. <-SingleThread
  155. }()
  156. P_QL.currentType = tasktype
  157. P_QL.pici = time.Now().Unix()
  158. P_QL.taskQl(mapInfo)
  159. }()
  160. case "project": //增量合并,未抽取到项目名称或项目编号的不合并 bidding中mergestatus 1已合并 2字段问题不合并 3历史待合并
  161. //合同、验收公告在6个月内查询不到可扩展到两年
  162. go func() {
  163. defer func() {
  164. <-SingleThread
  165. }()
  166. P_QL.currentType = tasktype
  167. P_QL.pici = time.Now().Unix()
  168. P_QL.taskZl(mapInfo)
  169. }()
  170. case "updateInfo": //招标字段变更
  171. go func() {
  172. defer func() {
  173. <-SingleThread
  174. }()
  175. P_QL.currentType = tasktype
  176. P_QL.pici = time.Now().Unix()
  177. P_QL.taskUpdateInfo(mapInfo)
  178. }()
  179. case "updateMoney": //修改金额
  180. go func() {
  181. defer func() {
  182. <-SingleThread
  183. }()
  184. P_QL.currentType = tasktype
  185. P_QL.pici = time.Now().Unix()
  186. P_QL.taskUpdateMoney(mapInfo)
  187. }()
  188. case "history": //历史数据合并,暂时不写
  189. go func() {
  190. defer func() {
  191. <-SingleThread
  192. }()
  193. }()
  194. default:
  195. <-SingleThread
  196. }
  197. }
  198. case mu.OP_NOOP: //下个节点回应
  199. ok := string(data)
  200. if ok != "" {
  201. udptaskmap.Delete(ok)
  202. log.Println("ok:", ok)
  203. }
  204. }
  205. }