main.go 11 KB

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