main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/elastic"
  5. "app.yhyue.com/data_processing/common_utils/log"
  6. "app.yhyue.com/data_processing/common_utils/mongodb"
  7. "app.yhyue.com/data_processing/common_utils/mysqldb"
  8. "app.yhyue.com/data_processing/common_utils/udp"
  9. "encoding/json"
  10. "esindex/config"
  11. "esindex/oss"
  12. "fmt"
  13. "github.com/robfig/cron"
  14. "go.uber.org/zap"
  15. "io/ioutil"
  16. "net"
  17. "net/http"
  18. "sync"
  19. "time"
  20. )
  21. var (
  22. MgoB *mongodb.MongodbSim
  23. MgoP *mongodb.MongodbSim
  24. MgoQ *mongodb.MongodbSim
  25. MgoS *mongodb.MongodbSim
  26. Mysql *mysqldb.Mysql
  27. Es, Es1 *elastic.Elastic
  28. UdpClient udp.UdpClient
  29. UdpTaskMap = &sync.Map{}
  30. JyUdpAddr *net.UDPAddr
  31. EsBulkSize = 100 // es批量保存大小
  32. updateBiddingPool = make(chan []map[string]interface{}, 5000) //更新bingding数据
  33. updateBiddingSp = make(chan bool, 5)
  34. saveEsPool = make(chan map[string]interface{}, 5000) //保存binding数据到es
  35. saveEsSp = make(chan bool, 5)
  36. saveProjectEsPool = make(chan map[string]interface{}, 5000) //保存project数据到es
  37. saveProjectSp = make(chan bool, 5)
  38. saveEsAllPool = make(chan map[string]interface{}, 5000) //存储单机版es,爬虫采集判重使用
  39. saveEsAllSp = make(chan bool, 5)
  40. saveErrBidPool = make(chan map[string]interface{}, 5000)
  41. saveBidSp = make(chan bool, 5)
  42. //detailLength = 50000 // es保存detail长度
  43. fileLength = 200000 // es保存附件文本长度,大于20万 时做一个日志记录
  44. //pscopeLength = 32766 // projectscope长度
  45. specialNames = make(map[string]bool, 0) //存储181 凭安提供的特企,爬虫采购单位
  46. )
  47. func init() {
  48. config.Init("./common.toml")
  49. InitLog()
  50. InitMgo()
  51. InitEs()
  52. InitField()
  53. InitEsBiddingField()
  54. oss.InitOss()
  55. verifyESFields() //检测es 字段类型
  56. JyUdpAddr = &net.UDPAddr{
  57. IP: net.ParseIP(config.Conf.Udp.JyAddr),
  58. Port: util.IntAll(config.Conf.Udp.JyPort),
  59. }
  60. log.Info("init success")
  61. }
  62. func main() {
  63. go checkMapJob() //udp 发送邮件
  64. go task_index() //定时同步更新winner_enterprise、buyer_enterprise ES索引;这个功能很少变动,几乎不需要维护
  65. go UpdateBidding() //更新bidding表数据
  66. go SaveEsMethod()
  67. go SaveAllEsMethod()
  68. go SaveProjectEs()
  69. go SaveBidErr()
  70. UdpClient = udp.UdpClient{Local: config.Conf.Udp.LocPort, BufSize: 1024}
  71. UdpClient.Listen(processUdpMsg)
  72. log.Info("Udp服务监听", zap.String("port:", config.Conf.Udp.LocPort))
  73. ch := make(chan bool, 1)
  74. <-ch
  75. }
  76. var pool = make(chan bool, 20)
  77. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  78. switch act {
  79. case udp.OP_TYPE_DATA:
  80. var mapInfo map[string]interface{}
  81. err := json.Unmarshal(data, &mapInfo)
  82. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  83. if err != nil {
  84. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  85. } else if mapInfo != nil {
  86. key, _ := mapInfo["key"].(string)
  87. if key == "" {
  88. key = "udpok"
  89. }
  90. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  91. tasktype, _ := mapInfo["stype"].(string)
  92. switch tasktype {
  93. case "index-by-id": //单个索引
  94. pool <- true
  95. go func() {
  96. defer func() {
  97. <-pool
  98. }()
  99. biddingTaskById(mapInfo)
  100. }()
  101. case "bidding":
  102. pool <- true
  103. go func() {
  104. defer func() {
  105. <-pool
  106. }()
  107. biddingTask(mapInfo)
  108. }()
  109. case "biddingall": //补充存量数据
  110. pool <- true
  111. go func() {
  112. defer func() {
  113. <-pool
  114. }()
  115. biddingAllTask(mapInfo)
  116. }()
  117. case "bidding_all_data": //根据biddingall配置文件,存量迁移数据
  118. pool <- true
  119. go func() {
  120. defer func() {
  121. <-pool
  122. }()
  123. biddingAllDataTask()
  124. }()
  125. case "bidding_history":
  126. pool <- true
  127. go func() {
  128. defer func() {
  129. <-pool
  130. }()
  131. biddingTask(mapInfo)
  132. }()
  133. case "project":
  134. pool <- true
  135. go func() {
  136. defer func() {
  137. <-pool
  138. }()
  139. projectTask(data, mapInfo)
  140. }()
  141. case "project_all_data": //存量 projectset 数据
  142. pool <- true
  143. go func() {
  144. defer func() {
  145. <-pool
  146. }()
  147. projectAllData()
  148. }()
  149. case "biddingdata": //es 单机版,采集判重
  150. pool <- true
  151. go func() {
  152. defer func() {
  153. <-pool
  154. }()
  155. biddingDataTask(data, mapInfo)
  156. }()
  157. case "biddingdelbyextracttype": //根据bidding表extracttype=-1,删除es中重复数据
  158. pool <- true
  159. go func() {
  160. defer func() {
  161. <-pool
  162. }()
  163. biddingDelByExtracttype(data, mapInfo)
  164. }()
  165. case "buyer_once": // 采购单位昨天增量数据
  166. pool <- true
  167. go func() {
  168. defer func() {
  169. <-pool
  170. }()
  171. buyerOnce()
  172. }()
  173. default:
  174. pool <- true
  175. go func() {
  176. defer func() {
  177. <-pool
  178. }()
  179. log.Info("err", zap.Any("mapInfo", mapInfo))
  180. }()
  181. }
  182. }
  183. case udp.OP_NOOP: //下个节点回应
  184. ok := string(data)
  185. if ok != "" {
  186. log.Info("udp re", zap.String("data:", ok))
  187. UdpTaskMap.Delete(ok)
  188. }
  189. }
  190. }
  191. func task_index() {
  192. c := cron.New()
  193. _ = c.AddFunc("0 0 0 * * ?", func() { task_winneres() }) //每天凌晨执行一次winner生索引
  194. _ = c.AddFunc("0 0 1 * * ?", func() { task_buyeres() }) //每天1点执行一次buyer生索引
  195. c.Start()
  196. }
  197. func task_winneres() {
  198. log.Info("定时任务,winneres")
  199. winnerEsTaskOnce()
  200. }
  201. func task_buyeres() {
  202. log.Info("定时任务,buyeres")
  203. buyerOnce()
  204. }
  205. type UdpNode struct {
  206. data []byte
  207. addr *net.UDPAddr
  208. timestamp int64
  209. retry int
  210. }
  211. //UpdateBidding 更新bidding表数据
  212. func UpdateBidding() {
  213. arru := make([][]map[string]interface{}, 200)
  214. indexu := 0
  215. for {
  216. select {
  217. case v := <-updateBiddingPool:
  218. arru[indexu] = v
  219. indexu++
  220. if indexu == 200 {
  221. updateBiddingSp <- true
  222. go func(arru [][]map[string]interface{}) {
  223. defer func() {
  224. <-updateBiddingSp
  225. }()
  226. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  227. }(arru)
  228. arru = make([][]map[string]interface{}, 200)
  229. indexu = 0
  230. }
  231. case <-time.After(1000 * time.Millisecond):
  232. if indexu > 0 {
  233. updateBiddingSp <- true
  234. go func(arru [][]map[string]interface{}) {
  235. defer func() {
  236. <-updateBiddingSp
  237. }()
  238. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  239. }(arru[:indexu])
  240. arru = make([][]map[string]interface{}, 200)
  241. indexu = 0
  242. }
  243. }
  244. }
  245. }
  246. //SaveBidErr 记录错误信息,暂时记录 附件过长的
  247. func SaveBidErr() {
  248. arru := make([]map[string]interface{}, 200)
  249. indexu := 0
  250. for {
  251. select {
  252. case v := <-saveErrBidPool:
  253. arru[indexu] = v
  254. indexu++
  255. if indexu == 200 {
  256. saveBidSp <- true
  257. go func(arru []map[string]interface{}) {
  258. defer func() {
  259. <-saveBidSp
  260. }()
  261. MgoB.SaveBulk("bidding_es_err_record", arru...)
  262. }(arru)
  263. arru = make([]map[string]interface{}, 200)
  264. indexu = 0
  265. }
  266. case <-time.After(1000 * time.Millisecond):
  267. if indexu > 0 {
  268. saveBidSp <- true
  269. go func(arru []map[string]interface{}) {
  270. defer func() {
  271. <-saveBidSp
  272. }()
  273. MgoB.SaveBulk("bidding_es_err_record", arru...)
  274. }(arru[:indexu])
  275. arru = make([]map[string]interface{}, 200)
  276. indexu = 0
  277. }
  278. }
  279. }
  280. }
  281. //SaveEsMethod 保存到es
  282. func SaveEsMethod() {
  283. arru := make([]map[string]interface{}, EsBulkSize)
  284. indexu := 0
  285. for {
  286. select {
  287. case v := <-saveEsPool:
  288. arru[indexu] = v
  289. indexu++
  290. if indexu == EsBulkSize {
  291. saveEsSp <- true
  292. go func(arru []map[string]interface{}) {
  293. defer func() {
  294. <-saveEsSp
  295. }()
  296. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  297. if config.Conf.DB.Es.IndexTmp != "" {
  298. Es.BulkSave(config.Conf.DB.Es.IndexTmp, arru)
  299. }
  300. }(arru)
  301. arru = make([]map[string]interface{}, EsBulkSize)
  302. indexu = 0
  303. }
  304. case <-time.After(1000 * time.Millisecond):
  305. if indexu > 0 {
  306. saveEsSp <- true
  307. go func(arru []map[string]interface{}) {
  308. defer func() {
  309. <-saveEsSp
  310. }()
  311. Es.BulkSave(config.Conf.DB.Es.IndexB, arru)
  312. if config.Conf.DB.Es.IndexTmp != "" {
  313. Es.BulkSave(config.Conf.DB.Es.IndexTmp, arru)
  314. }
  315. }(arru[:indexu])
  316. arru = make([]map[string]interface{}, EsBulkSize)
  317. indexu = 0
  318. }
  319. }
  320. }
  321. }
  322. func SaveAllEsMethod() {
  323. arru := make([]map[string]interface{}, EsBulkSize)
  324. indexu := 0
  325. for {
  326. select {
  327. case v := <-saveEsAllPool:
  328. arru[indexu] = v
  329. indexu++
  330. if indexu == EsBulkSize {
  331. saveEsAllSp <- true
  332. go func(arru []map[string]interface{}) {
  333. defer func() {
  334. <-saveEsAllSp
  335. }()
  336. Es1.BulkSave("biddingall", arru)
  337. }(arru)
  338. arru = make([]map[string]interface{}, EsBulkSize)
  339. indexu = 0
  340. }
  341. case <-time.After(1000 * time.Millisecond):
  342. if indexu > 0 {
  343. saveEsAllSp <- true
  344. go func(arru []map[string]interface{}) {
  345. defer func() {
  346. <-saveEsAllSp
  347. }()
  348. Es1.BulkSave("biddingall", arru)
  349. }(arru[:indexu])
  350. arru = make([]map[string]interface{}, EsBulkSize)
  351. indexu = 0
  352. }
  353. }
  354. }
  355. }
  356. func SaveProjectEs() {
  357. arru := make([]map[string]interface{}, EsBulkSize)
  358. indexu := 0
  359. for {
  360. select {
  361. case v := <-saveProjectEsPool:
  362. arru[indexu] = v
  363. indexu++
  364. if indexu == EsBulkSize {
  365. saveProjectSp <- true
  366. go func(arru []map[string]interface{}) {
  367. defer func() {
  368. <-saveProjectSp
  369. }()
  370. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  371. }(arru)
  372. arru = make([]map[string]interface{}, EsBulkSize)
  373. indexu = 0
  374. }
  375. case <-time.After(1000 * time.Millisecond):
  376. if indexu > 0 {
  377. saveProjectSp <- true
  378. go func(arru []map[string]interface{}) {
  379. defer func() {
  380. <-saveProjectSp
  381. }()
  382. Es.BulkSave(config.Conf.DB.Es.IndexP, arru)
  383. }(arru[:indexu])
  384. arru = make([]map[string]interface{}, EsBulkSize)
  385. indexu = 0
  386. }
  387. }
  388. }
  389. }
  390. func checkMapJob() {
  391. if config.Conf.Mail.Send {
  392. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  393. for {
  394. UdpTaskMap.Range(func(k, v interface{}) bool {
  395. now := time.Now().Unix()
  396. node, _ := v.(*UdpNode)
  397. if now-node.timestamp > 120 {
  398. node.retry++
  399. if node.retry > 5 {
  400. UdpTaskMap.Delete(k)
  401. res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", config.Conf.Mail.Api, config.Conf.Mail.To, "field-sync-send-fail", k.(string)))
  402. if err == nil {
  403. defer res.Body.Close()
  404. read, err := ioutil.ReadAll(res.Body)
  405. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  406. }
  407. } else {
  408. log.Info("udp重发", zap.Any("k:", k))
  409. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  410. }
  411. } else if now-node.timestamp > 10 {
  412. log.Info("udp任务超时中..", zap.Any("k:", k))
  413. }
  414. return true
  415. })
  416. time.Sleep(60 * time.Second)
  417. }
  418. }
  419. }
  420. func task() {
  421. sess := MgoB.GetMgoConn()
  422. defer MgoB.DestoryMongoConn(sess)
  423. ch := make(chan bool, 10)
  424. wg := &sync.WaitGroup{}
  425. query := sess.DB("qfw").C("result_replace_repair_log").Find(nil).Iter()
  426. count := 0
  427. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  428. if count%1000 == 0 {
  429. util.Debug("current ---", count)
  430. }
  431. ch <- true
  432. wg.Add(1)
  433. go func(tmp map[string]interface{}) {
  434. defer func() {
  435. <-ch
  436. wg.Done()
  437. }()
  438. if id := util.ObjToString(tmp["replace_id"]); mongodb.IsObjectIdHex(id) {
  439. biddingTaskById(map[string]interface{}{"infoid": id, "stype": "bidding"})
  440. }
  441. }(tmp)
  442. tmp = make(map[string]interface{})
  443. }
  444. wg.Wait()
  445. util.Debug("over ---", count)
  446. }