main.go 17 KB

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