main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. time.Sleep(99999 * time.Hour)
  121. }
  122. //udp调用信号
  123. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  124. switch act {
  125. case mu.OP_TYPE_DATA: //上个节点的数据
  126. var mapInfo map[string]interface{}
  127. err := json.Unmarshal(data, &mapInfo)
  128. log.Println("err:", err, "mapInfo:", mapInfo)
  129. if err != nil {
  130. _ = udpclient.WriteUdp([]byte("err:"+err.Error()), mu.OP_NOOP, ra)
  131. } else if mapInfo != nil {
  132. key, _ := mapInfo["key"].(string)
  133. if key == "" {
  134. key = "udpok"
  135. }
  136. go udpclient.WriteUdp([]byte(key), mu.OP_NOOP, ra)
  137. SingleThread <- true
  138. tasktype, _ := mapInfo["stype"].(string)
  139. switch tasktype {
  140. case "ql": //全量合并
  141. go func() {
  142. defer func() {
  143. <-SingleThread
  144. }()
  145. P_QL.currentType = tasktype
  146. P_QL.pici = time.Now().Unix()
  147. P_QL.taskQl(mapInfo)
  148. }()
  149. case "project": //增量合并,未抽取到项目名称或项目编号的不合并 bidding中mergestatus 1已合并 2字段问题不合并 3历史待合并
  150. //合同、验收公告在6个月内查询不到可扩展到两年
  151. go func() {
  152. defer func() {
  153. <-SingleThread
  154. }()
  155. P_QL.currentType = tasktype
  156. P_QL.pici = time.Now().Unix()
  157. P_QL.taskZl(mapInfo)
  158. }()
  159. case "updateInfo": //招标字段变更
  160. go func() {
  161. defer func() {
  162. <-SingleThread
  163. }()
  164. P_QL.currentType = tasktype
  165. P_QL.pici = time.Now().Unix()
  166. P_QL.taskUpdateInfo(mapInfo)
  167. }()
  168. case "updatePro": //修改项目外围字段(只修改外围字段值)
  169. go func() {
  170. defer func() {
  171. <-SingleThread
  172. }()
  173. P_QL.currentType = tasktype
  174. P_QL.pici = time.Now().Unix()
  175. P_QL.taskUpdatePro(mapInfo)
  176. }()
  177. case "deleteInfo": // 删除招标公告
  178. go func() {
  179. defer func() {
  180. <-SingleThread
  181. }()
  182. P_QL.currentType = tasktype
  183. P_QL.pici = time.Now().Unix()
  184. P_QL.delInfoPro(mapInfo)
  185. }()
  186. case "spider": // 爬虫代码code、isflow
  187. go func() {
  188. defer func() {
  189. <-SingleThread
  190. }()
  191. go P_QL.loadSpiderCode()
  192. }()
  193. case "history": //历史数据合并,暂时不写
  194. go func() {
  195. defer func() {
  196. <-SingleThread
  197. }()
  198. }()
  199. default:
  200. <-SingleThread
  201. }
  202. }
  203. case mu.OP_NOOP: //下个节点回应
  204. ok := string(data)
  205. if ok != "" {
  206. udptaskmap.Delete(ok)
  207. log.Println("ok:", ok)
  208. }
  209. }
  210. }