main.go 14 KB

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