main.go 5.4 KB

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