main.go 13 KB

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