main.go 13 KB

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