main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 mainT() {
  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. func main() {
  94. sid = "61d3b61345a326c6c35080f8"
  95. eid = "629496011cd2d8ec2137ed5b"
  96. //flag.StringVar(&sid, "sid", "", "开始id")
  97. //flag.StringVar(&eid, "eid", "", "结束id")
  98. //flag.Parse()
  99. mapinfo := map[string]interface{}{}
  100. if sid == "" || eid == "" {
  101. log.Println("sid, eid参数不能为空")
  102. os.Exit(0)
  103. }
  104. mapinfo["gtid"] = sid
  105. mapinfo["lteid"] = eid
  106. mapinfo["stype"] = "ql"
  107. mapinfo["ip"] = "127.0.0.1"
  108. mapinfo["port"] = Sysconfig["udpport"]
  109. if Sysconfig["loadStart"] != nil {
  110. loadStart := util.Int64All(Sysconfig["loadStart"])
  111. if loadStart > -1 {
  112. P_QL.loadData(loadStart)
  113. }
  114. }
  115. P_QL.loadSite()
  116. P_QL.currentType = mapinfo["stype"].(string)
  117. P_QL.pici = time.Now().Unix()
  118. P_QL.taskQl(mapinfo)
  119. time.Sleep(99999 * time.Hour)
  120. }
  121. //udp调用信号
  122. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  123. switch act {
  124. case mu.OP_TYPE_DATA: //上个节点的数据
  125. var mapInfo map[string]interface{}
  126. err := json.Unmarshal(data, &mapInfo)
  127. log.Println("err:", err, "mapInfo:", mapInfo)
  128. if err != nil {
  129. _ = udpclient.WriteUdp([]byte("err:"+err.Error()), mu.OP_NOOP, ra)
  130. } else if mapInfo != nil {
  131. key, _ := mapInfo["key"].(string)
  132. if key == "" {
  133. key = "udpok"
  134. }
  135. go udpclient.WriteUdp([]byte(key), mu.OP_NOOP, ra)
  136. SingleThread <- true
  137. tasktype, _ := mapInfo["stype"].(string)
  138. switch tasktype {
  139. case "ql": //全量合并
  140. go func() {
  141. defer func() {
  142. <-SingleThread
  143. }()
  144. P_QL.currentType = tasktype
  145. P_QL.pici = time.Now().Unix()
  146. P_QL.taskQl(mapInfo)
  147. }()
  148. case "project": //增量合并,未抽取到项目名称或项目编号的不合并 bidding中mergestatus 1已合并 2字段问题不合并 3历史待合并
  149. //合同、验收公告在6个月内查询不到可扩展到两年
  150. go func() {
  151. defer func() {
  152. <-SingleThread
  153. }()
  154. P_QL.currentType = tasktype
  155. P_QL.pici = time.Now().Unix()
  156. P_QL.taskZl(mapInfo)
  157. }()
  158. case "updateInfo": //招标字段变更
  159. go func() {
  160. defer func() {
  161. <-SingleThread
  162. }()
  163. P_QL.currentType = tasktype
  164. P_QL.pici = time.Now().Unix()
  165. P_QL.taskUpdateInfo(mapInfo)
  166. }()
  167. case "updatePro": //修改项目外围字段(只修改外围字段值)
  168. go func() {
  169. defer func() {
  170. <-SingleThread
  171. }()
  172. P_QL.currentType = tasktype
  173. P_QL.pici = time.Now().Unix()
  174. P_QL.taskUpdatePro(mapInfo)
  175. }()
  176. case "deleteInfo": // 删除招标公告
  177. go func() {
  178. defer func() {
  179. <-SingleThread
  180. }()
  181. P_QL.currentType = tasktype
  182. P_QL.pici = time.Now().Unix()
  183. P_QL.delInfoPro(mapInfo)
  184. }()
  185. case "spider": // 爬虫代码code、isflow
  186. go func() {
  187. defer func() {
  188. <-SingleThread
  189. }()
  190. go P_QL.loadSpiderCode()
  191. }()
  192. case "history": //历史数据合并,暂时不写
  193. go func() {
  194. defer func() {
  195. <-SingleThread
  196. }()
  197. }()
  198. default:
  199. <-SingleThread
  200. }
  201. }
  202. case mu.OP_NOOP: //下个节点回应
  203. ok := string(data)
  204. if ok != "" {
  205. udptaskmap.Delete(ok)
  206. log.Println("ok:", ok)
  207. }
  208. }
  209. }