main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 mainT() {
  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 main() {
  85. sid = "5cde1a74a5cb26b9b782dd1f"
  86. eid = "5dee234de9d1f601e4c4b813"
  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. //P_QL.taskQuery()
  111. time.Sleep(99999 * time.Hour)
  112. }
  113. func mainS() {
  114. id := "5987e5e85d11e1c745d36c4c"
  115. mapinfo := map[string]interface{}{}
  116. mapinfo["id"] = id
  117. mapinfo["stype"] = "updateMoney"
  118. mapinfo["budget"] = "12345"
  119. mapinfo["ip"] = "127.0.0.1"
  120. mapinfo["port"] = Sysconfig["udpport"]
  121. if Sysconfig["loadStart"] != nil {
  122. loadStart := util.Int64All(Sysconfig["loadStart"])
  123. if loadStart > -1 {
  124. P_QL.loadData(loadStart)
  125. }
  126. }
  127. P_QL.loadSite()
  128. P_QL.currentType = mapinfo["stype"].(string)
  129. P_QL.pici = time.Now().Unix()
  130. P_QL.taskUpdateMoney(mapinfo)
  131. P_QL.Brun = true
  132. time.Sleep(20 * time.Second)
  133. }
  134. //udp调用信号
  135. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  136. switch act {
  137. case mu.OP_TYPE_DATA: //上个节点的数据
  138. var mapInfo map[string]interface{}
  139. err := json.Unmarshal(data, &mapInfo)
  140. log.Println("err:", err, "mapInfo:", mapInfo)
  141. if err != nil {
  142. _ = udpclient.WriteUdp([]byte("err:"+err.Error()), mu.OP_NOOP, ra)
  143. } else if mapInfo != nil {
  144. key, _ := mapInfo["key"].(string)
  145. if key == "" {
  146. key = "udpok"
  147. }
  148. go udpclient.WriteUdp([]byte(key), mu.OP_NOOP, ra)
  149. SingleThread <- true
  150. tasktype, _ := mapInfo["stype"].(string)
  151. switch tasktype {
  152. case "ql": //全量合并
  153. go func() {
  154. defer func() {
  155. <-SingleThread
  156. }()
  157. P_QL.currentType = tasktype
  158. P_QL.pici = time.Now().Unix()
  159. P_QL.taskQl(mapInfo)
  160. }()
  161. case "project": //增量合并,未抽取到项目名称或项目编号的不合并 bidding中mergestatus 1已合并 2字段问题不合并 3历史待合并
  162. //合同、验收公告在6个月内查询不到可扩展到两年
  163. go func() {
  164. defer func() {
  165. <-SingleThread
  166. }()
  167. P_QL.currentType = tasktype
  168. P_QL.pici = time.Now().Unix()
  169. P_QL.taskZl(mapInfo)
  170. }()
  171. case "updateInfo": //招标字段变更
  172. go func() {
  173. defer func() {
  174. <-SingleThread
  175. }()
  176. P_QL.currentType = tasktype
  177. P_QL.pici = time.Now().Unix()
  178. P_QL.taskUpdateInfo(mapInfo)
  179. }()
  180. case "updateMoney": //修改金额
  181. go func() {
  182. defer func() {
  183. <-SingleThread
  184. }()
  185. P_QL.currentType = tasktype
  186. P_QL.pici = time.Now().Unix()
  187. P_QL.taskUpdateMoney(mapInfo)
  188. }()
  189. case "updateMoneyMgo": //修改金额
  190. go func() {
  191. defer func() {
  192. <-SingleThread
  193. }()
  194. P_QL.currentType = tasktype
  195. P_QL.pici = time.Now().Unix()
  196. P_QL.taskQuery()
  197. }()
  198. case "history": //历史数据合并,暂时不写
  199. go func() {
  200. defer func() {
  201. <-SingleThread
  202. }()
  203. }()
  204. default:
  205. <-SingleThread
  206. }
  207. }
  208. case mu.OP_NOOP: //下个节点回应
  209. ok := string(data)
  210. if ok != "" {
  211. udptaskmap.Delete(ok)
  212. log.Println("ok:", ok)
  213. }
  214. }
  215. }