main.go 13 KB

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