main.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. package main
  2. import (
  3. "cluster"
  4. "config"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/cron"
  8. "gopkg.in/mgo.v2/bson"
  9. "html/template"
  10. "log"
  11. "math"
  12. mu "mfw/util"
  13. "net"
  14. "net/http"
  15. qu "qfw/util"
  16. "qfw/util/mongodb"
  17. "regexp"
  18. "strings"
  19. "sync"
  20. "time"
  21. )
  22. var udpclient mu.UdpClient //udp对象
  23. var sys sync.RWMutex
  24. var DeleteNode = time.NewTimer(time.Minute * 50)
  25. func main() {
  26. udpclient = mu.UdpClient{Local: config.Sysconfig["udpip"].(string) + ":" + config.Sysconfig["udpport"].(string), BufSize: 1024}
  27. udpclient.Listen(processUdpMsg)
  28. log.Printf("Udp listening port: %s:%s\n", config.Sysconfig["udpip"], config.Sysconfig["udpport"])
  29. if config.Sysconfig["broadcast"].(bool) { //重启的话通知分布式节点
  30. ips := qu.ObjToString(config.Sysconfig["broadcast_ips"])
  31. ipsArr := strings.Split(ips, ";")
  32. for _, v := range ipsArr {
  33. udpclient.WriteUdp([]byte{}, mu.OP_TYPE_DATA, &net.UDPAddr{
  34. IP: net.ParseIP(v),
  35. Port: qu.IntAll(config.Sysconfig["broadcast_port"]),
  36. })
  37. }
  38. }
  39. mux := http.NewServeMux()
  40. mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
  41. http.Redirect(writer, request, "/login", http.StatusFound)
  42. })
  43. mux.HandleFunc("/favicon.ico", func(writer http.ResponseWriter, request *http.Request) {
  44. writer.WriteHeader(http.StatusOK)
  45. })
  46. mux.HandleFunc("/login", func(writer http.ResponseWriter, request *http.Request) {
  47. tp, err := template.ParseFiles("src/web/login.html")
  48. if err != nil {
  49. log.Println(err)
  50. writer.Write([]byte( "页面找不到了~"))
  51. return
  52. }
  53. if request.Method == "GET" {
  54. tp.Execute(writer, "")
  55. return
  56. }
  57. if request.Method == "POST" {
  58. err := request.ParseForm()
  59. if err != nil {
  60. http.Error(writer, err.Error(), http.StatusBadRequest)
  61. return
  62. }
  63. email := request.Form.Get("username")
  64. pwd := request.Form.Get("pwd")
  65. if email == "ocr_task" && pwd == "ocr_task_pwd" {
  66. http.SetCookie(writer, &http.Cookie{Name: "username", Value: qu.GetMd5String("email")})
  67. http.Redirect(writer, request, "/query", http.StatusFound)
  68. } else {
  69. writer.WriteHeader(http.StatusUnauthorized)
  70. tp.Execute(writer, "密码错误")
  71. }
  72. return
  73. }
  74. })
  75. res := http.FileServer(http.Dir("src/res"))
  76. mux.Handle("/res/", http.StripPrefix("/res/", res))
  77. mux.HandleFunc("/query", func(writer http.ResponseWriter, request *http.Request) {
  78. tplogin, err := template.ParseFiles("src/web/login.html")
  79. if err != nil {
  80. log.Println(err)
  81. writer.Write([]byte( "页面找不到了~"))
  82. return
  83. }
  84. cookie, _ := request.Cookie("username")
  85. if cookie == nil {
  86. http.RedirectHandler("/login", http.StatusUnauthorized)
  87. tplogin.Execute(writer, "请先登录")
  88. return
  89. }
  90. if cookie.Value != "0c83f57c786a0b4a39efab23731c7ebc" {
  91. http.RedirectHandler("/login", http.StatusUnauthorized)
  92. tplogin.Execute(writer, "密钥错误")
  93. return
  94. }
  95. tp, err := template.ParseFiles("src/web/index.html", "src/public/header.html")
  96. if err != nil {
  97. log.Println(err)
  98. writer.Write([]byte( "页面找不到了~"))
  99. return
  100. }
  101. task := queryOcrTask()
  102. tp.Execute(writer, task)
  103. })
  104. mux.HandleFunc("/reload", func(writer http.ResponseWriter, request *http.Request) {
  105. tplogin, err := template.ParseFiles("src/web/login.html")
  106. if err != nil {
  107. log.Println(err)
  108. writer.Write([]byte( "页面找不到了~"))
  109. return
  110. }
  111. cookie, _ := request.Cookie("username")
  112. if cookie == nil {
  113. http.RedirectHandler("/login", http.StatusUnauthorized)
  114. tplogin.Execute(writer, "请先登录")
  115. return
  116. }
  117. tpReload, err := template.ParseFiles("src/web/reload.html")
  118. if err != nil {
  119. log.Println(err)
  120. writer.Write([]byte( "页面找不到了~"))
  121. return
  122. }
  123. if request.Method == "POST" {
  124. err = request.ParseForm()
  125. if err != nil {
  126. tpReload.Execute(writer, err)
  127. return
  128. }
  129. data := request.PostForm["ips"]
  130. if len(data) <= 0 {
  131. tpReload.Execute(writer, "参数无效")
  132. return
  133. }
  134. var tmpstr string
  135. for _, v := range data {
  136. if !regexp.MustCompile("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}").MatchString(v) {
  137. tmpstr += v + "ip格式不正确</br>"
  138. continue
  139. }
  140. result := reload(v)
  141. tmpstr += v + "---->" + result + "</br>"
  142. log.Println(v, "重新部署完成", )
  143. }
  144. tpReload.Execute(writer, template.HTML(
  145. "</br><span style=\"color: red\">"+tmpstr+" 重新部署结束</span></br>"+
  146. "</br>"+
  147. "<form action=\"/reload\" method=\"post\">"+
  148. "<input id=\"ips1\" name=\"ips\" placeholder=\"请输入实例ip\" required/></br>"+
  149. "<input type=\"submit\" value=\"提交\" />"+
  150. "</form>"))
  151. return
  152. }
  153. tpReload.Execute(writer, template.HTML("<form action=\"/reload\" method=\"post\">"+
  154. "<input id=\"ips1\" name=\"ips\" placeholder=\"请输入实例ip\" required/></br>"+
  155. "<input type=\"submit\" value=\"提交\" />"+
  156. "</form>"))
  157. })
  158. c := cron.New()
  159. spec := qu.ObjToString(config.Sysconfig["cornstr"])
  160. c.AddFunc(spec, func() {
  161. d := time.Now()
  162. nowday := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  163. taskArr := mongodb.Find("ocr_task", bson.M{}, `{_id:1}`, nil, false, -1, -1)
  164. taskNum := len(*taskArr)
  165. log.Println("当前任务数量:", taskNum)
  166. //if taskNum <= 0 {
  167. //计算释放,发送udp
  168. ccnum := compute()
  169. log.Println("ccnum:",ccnum,", len(config.CID):", len(config.CID),config.CID)
  170. if ccnum <= 0 {
  171. if len(config.CID) == 0 {
  172. log.Println("当前实例为空,无需释放", config.CID, )
  173. return
  174. }
  175. if ccnum == 0 && len(config.CID) >0{
  176. for i,_ := range config.CID{
  177. tmpIid := config.CID[i]
  178. go func(tmpIid string) {
  179. config.CID = config.CID[i+1:]
  180. mongodb.Del("ocr_ecs", bson.M{"InstanceId": tmpIid})
  181. log.Println("5分钟后释放实例", tmpIid)
  182. udpclient.WriteUdp([]byte("5分钟后释放实例"), mu.OP_DELETE_DOWNLOADERCODES, &net.UDPAddr{
  183. IP: net.ParseIP(tmpIid),
  184. Port: qu.IntAll(config.Sysconfig["broadcast_port"]),
  185. })
  186. time.Sleep(time.Minute * 5)
  187. cluster.DeleteInstance(tmpIid)
  188. log.Println("5分钟后释放实例完成", tmpIid)
  189. }(tmpIid)
  190. }
  191. }else {
  192. for i:=0; i<ccnum*(-1); ccnum++ {
  193. tmpIid := config.CID[i]
  194. go func(tmpIid string) {
  195. config.CID = config.CID[i+1:]
  196. mongodb.Del("ocr_ecs", bson.M{"InstanceId": tmpIid})
  197. log.Println("5分钟后释放实例", tmpIid)
  198. udpclient.WriteUdp([]byte("5分钟后释放实例"), mu.OP_DELETE_DOWNLOADERCODES, &net.UDPAddr{
  199. IP: net.ParseIP(tmpIid),
  200. Port: qu.IntAll(config.Sysconfig["broadcast_port"]),
  201. })
  202. time.Sleep(time.Minute * 5)
  203. cluster.DeleteInstance(tmpIid)
  204. log.Println("5分钟后释放实例完成", tmpIid)
  205. }(tmpIid)
  206. }
  207. }
  208. return
  209. }
  210. //}
  211. if len(config.CID) >= qu.IntAll(config.Sysconfig["pernum"]) {
  212. log.Println("实例申请上限,当前实例:", config.CID)
  213. return
  214. }
  215. //if taskNum > 1 {
  216. cluster.DescribeInstances() //查询多台实例的详细信息
  217. ocrescs := mongodb.Find("ocr_ecs", bson.M{}, nil, bson.M{"AutoReleaseTime": 1}, false, -1, -1)
  218. if ocrescs != nil || len(*ocrescs) > 0 {
  219. for _, v := range (*ocrescs) {
  220. if qu.ObjToString(v["AutoReleaseTime"]) != "" {
  221. utc, err := time.ParseInLocation("2006-01-02T15:04Z", qu.ObjToString(v["AutoReleaseTime"]), time.Local)
  222. if err != nil {
  223. log.Println("解析时间异常", err)
  224. continue
  225. }
  226. if utc.Before(nowday) {
  227. log.Println("删除过时实例", mongodb.Del("ocr_ecs", bson.M{"_id": v["_id"].(bson.ObjectId)}))
  228. }
  229. }
  230. }
  231. }
  232. log.Println("申请实例")
  233. now := time.Now()
  234. hours := time.Date(now.Year(), now.Month(), now.Day(), 20, 0, 0, 0, now.Location()).Sub(now).Hours()
  235. cluster.RunInstances("ocr_task_arr", "8", "false", ccnum, int(math.Round(hours)))
  236. log.Println("实例申请成功")
  237. DynamicTask() //动态任务
  238. log.Println("申请实例结束")
  239. //}
  240. })
  241. c.Start()
  242. log.Println("Http listening port: ", qu.ObjToString(config.Sysconfig["http_port"]))
  243. if err := http.ListenAndServe(":"+qu.ObjToString(config.Sysconfig["http_port"]), mux); err != nil {
  244. fmt.Println("start http server faild:", err)
  245. }
  246. }
  247. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  248. defer qu.Catch()
  249. switch act {
  250. case mu.OP_TYPE_DATA: //保存服务
  251. go udpclient.WriteUdp([]byte("数据接收成功:"+string(data)), mu.OP_NOOP, ra)
  252. tmp := make(map[string]interface{})
  253. err := json.Unmarshal(data, &tmp)
  254. if err != nil {
  255. go udpclient.WriteUdp([]byte("数据接收成功:"+string(data)+",data解析失败,"+err.Error()), mu.OP_NOOP, ra)
  256. return
  257. }
  258. tmp["start"] = tmp[qu.ObjToString(config.SidField)]
  259. tmp["import_time"] = time.Now().Unix()
  260. bytes, _ := json.Marshal(tmp)
  261. b := mongodb.Save("ocr_task", string(bytes))
  262. mongodb.Save("ocr_task_bak", string(bytes))
  263. log.Println("保存id:", b)
  264. case mu.OP_NOOP: //其他节点回应消息打印
  265. log.Println("节点接收成功", string(data), ra.String())
  266. case mu.OP_GET_DOWNLOADERCODE: //分发任务
  267. if `{"permission":"get_ocr_task"}` != string(data) {
  268. log.Println("没有权限:", string(data), ra)
  269. go udpclient.WriteUdp([]byte("没有权限"), mu.OP_NOOP, ra)
  270. return
  271. }
  272. sys.Lock()
  273. datas := mongodb.Find("ocr_task", nil, `{"_id":1}`, nil, false, -1, -1)
  274. if len(*datas) == 0 {
  275. go udpclient.WriteUdp([]byte("没有新数据"), mu.OP_TYPE_DATA, ra)
  276. sys.Unlock()
  277. return
  278. }
  279. tmp := (*datas)[0]
  280. ObjectId := tmp["_id"]
  281. sid := qu.ObjToString(tmp[qu.ObjToString(config.SidField)])
  282. eid := qu.ObjToString(tmp[qu.ObjToString(config.EidField)])
  283. rdata := mongodb.FindOneByField(config.MgoC, bson.M{"_id": bson.M{
  284. "$gt": bson.ObjectIdHex(sid),
  285. }}, `{"_id":1,"`+config.MgoFileFiled+`":1}`)
  286. //log.Println(rdata)
  287. if len((*rdata)) == 0 {
  288. go udpclient.WriteUdp([]byte("没有获取到最新数据"), mu.OP_TYPE_DATA, ra)
  289. sys.Unlock()
  290. return
  291. }
  292. newId := (*rdata)["_id"]
  293. if newId.(bson.ObjectId).Hex() >= eid {
  294. go udpclient.WriteUdp([]byte(`{"id":"`+qu.ObjToString(tmp["start"])+`","permission":"ocr_task","is_start":"true"}`), mu.OP_TYPE_DATA, ra) //起始位置
  295. go udpclient.WriteUdp([]byte(`{"id":"`+newId.(bson.ObjectId).Hex()+`","permission":"ocr_task"}`), mu.OP_TYPE_DATA, ra) //分发任务
  296. totmp := make(map[string]string)
  297. totmp["sid"] = qu.ObjToString(tmp[qu.ObjToString("start")])
  298. totmp["eid"] = qu.ObjToString(tmp[qu.ObjToString(config.EidField)])
  299. tobyte, _ := json.Marshal(totmp)
  300. go udpclient.WriteUdp(tobyte, mu.OP_TYPE_DATA, &net.UDPAddr{
  301. IP: net.ParseIP(qu.ObjToString(config.Sysconfig["toudpip"])),
  302. Port: qu.IntAll(config.Sysconfig["toudpport"]),
  303. })
  304. log.Println("ocr_task处理完成,发送下个节点", string(tobyte))
  305. mongodb.Del("ocr_task", bson.M{"_id": ObjectId.(bson.ObjectId)})
  306. tmp["end_time"] = time.Now().Unix()
  307. mongodb.Update("ocr_task_bak", bson.M{"_id": ObjectId.(bson.ObjectId)},map[string]interface{}{"$set": tmp}, true, false)
  308. sys.Unlock()
  309. return
  310. }
  311. go udpclient.WriteUdp([]byte(`{"id":"`+newId.(bson.ObjectId).Hex()+`","permission":"ocr_task"}`), mu.OP_TYPE_DATA, ra) //分发任务
  312. //log.Println(newId.(bson.ObjectId).Hex())
  313. tmp[config.SidField] = newId.(bson.ObjectId).Hex()
  314. mongodb.Update("ocr_task",
  315. bson.M{"_id": ObjectId.(bson.ObjectId)}, tmp, false, false)
  316. sys.Unlock()
  317. }
  318. }
  319. func queryOcrTask() map[string]int {
  320. data := make(map[string]int)
  321. taskArr := mongodb.Find("ocr_task", bson.M{}, `{_id:1}`, nil, false, -1, -1)
  322. taskNum := len(*taskArr)
  323. if taskNum == 0 {
  324. return data
  325. }
  326. data["taskNum"] = taskNum
  327. sumNum := 0
  328. nowSumNum := 0
  329. for i, v := range *taskArr {
  330. sid := bson.ObjectIdHex(qu.ObjToString(v["start"]))
  331. eid := bson.ObjectIdHex(qu.ObjToString(v[qu.ObjToString(config.EidField)]))
  332. sumNum += mongodb.Count("bidding", bson.M{"_id": bson.M{
  333. "$gte": sid,
  334. "$lte": eid,
  335. }})
  336. if i == 0 {
  337. nowSumNum = sumNum
  338. }
  339. }
  340. data["sumNum"] = sumNum
  341. data["nowSumNum"] = nowSumNum
  342. tmpsid := bson.ObjectIdHex(qu.ObjToString((*taskArr)[0]["start"]))
  343. over := (*taskArr)[0][qu.ObjToString(config.SidField)]
  344. overNum := mongodb.Count("bidding", bson.M{"_id": bson.M{
  345. "$gte": tmpsid,
  346. "$lt": bson.ObjectIdHex(qu.ObjToString(over)),
  347. }})
  348. data["overNum"] = overNum
  349. undoneNum := sumNum - overNum
  350. data["undoneNum"] = undoneNum
  351. nowUnDoneNum := nowSumNum - overNum
  352. data["nowUnDoneNum"] = nowUnDoneNum
  353. return data
  354. }
  355. func DynamicTask() {
  356. time.Sleep(time.Second * 30)
  357. cluster.DescribeInstances() //查询多台实例的详细信息
  358. escObject := mongodb.Find("ocr_ecs", bson.M{"TaskName": "ocr_task_arr", "OcrTaskStatus": "none"}, bson.M{"_id": -1}, nil, false, -1, -1)
  359. log.Println("实例未部署数量", len(*escObject))
  360. if escObject != nil || len(*escObject) > 0 {
  361. for i, v := range (*escObject) {
  362. if tmpip := qu.ObjToString(v["ip_nw"]); tmpip == "" {
  363. log.Println("没用获取到ip,实例ip异常", tmpip)
  364. DynamicTask()
  365. } else {
  366. if DoCMD(tmpip) {
  367. var tmpstr string
  368. isok2, udpstr := cluster.SshPgrep(tmpip, "pgrep udp2019")
  369. tmpstr += udpstr + ";&nbsp;&nbsp;"
  370. isok3, fil2textstr := cluster.SshPgrep(tmpip, "pgrep file2text")
  371. tmpstr += fil2textstr
  372. if isok2 && isok3 {
  373. (*escObject)[i]["OcrTaskStatus"] = "successful"
  374. mongodb.Update("ocr_ecs", bson.M{"_id": (*escObject)[i]["_id"]}, (*escObject)[i], true, false)
  375. config.CID = append(config.CID, qu.ObjToString(v["InstanceId"]))
  376. log.Println((*escObject)[i]["_id"], tmpip, "部署成功")
  377. } else {
  378. log.Println(tmpip, "部署异常,"+tmpstr)
  379. }
  380. } else {
  381. log.Println(tmpip, "部署失败")
  382. }
  383. }
  384. }
  385. } else {
  386. log.Println("动态任务创建失败")
  387. }
  388. }
  389. func DoCMD(ip string) bool {
  390. if ip == "" {
  391. return false
  392. }
  393. return cluster.RunSsh(ip)
  394. }
  395. func reload(ip string) string {
  396. tmp := mongodb.FindOne("ocr_ecs", bson.M{"ip_nw": ip})
  397. if tmp == nil || len(*tmp) <= 0 {
  398. return "ip不存在"
  399. }
  400. for i, v := range config.CID {
  401. if v == qu.ObjToString((*tmp)["InstanceId"]) {
  402. config.CID = append(config.CID[:i], config.CID[i+1:]...)
  403. break
  404. }
  405. }
  406. cluster.DeleteInstance(qu.ObjToString((*tmp)["InstanceId"])) //删除要重新部署的实例
  407. now := time.Now()
  408. hours := time.Date(now.Year(), now.Month(), now.Day(), 20, 0, 0, 0, now.Location()).Sub(now).Hours()
  409. cluster.RunInstances("ocr_task_arr", "8", "false", 1, int(math.Round(hours))) //创建新实例
  410. time.Sleep(time.Second * 30)
  411. cluster.DescribeInstances() //查询多台实例的详细信息
  412. escObject := mongodb.Find("ocr_ecs", bson.M{"TaskName": "ocr_task_arr", "OcrTaskStatus": "none"}, bson.M{"_id": -1}, nil, true, -1, -1)
  413. if escObject != nil || len(*escObject) > 0 {
  414. var tmpip string
  415. if tmpip = qu.ObjToString((*escObject)[0]["ip_nw"]); tmpip == "" {
  416. log.Println("没用获取到ip,实例ip异常", tmpip)
  417. time.Sleep(time.Minute)
  418. cluster.DescribeInstances() //查询多台实例的详细信息
  419. escObject = mongodb.Find("ocr_ecs", bson.M{"TaskName": "ocr_task_arr", "OcrTaskStatus": "none"}, bson.M{"_id": -1}, nil, true, -1, -1)
  420. tmpip = qu.ObjToString((*escObject)[0]["ip_nw"])
  421. }
  422. if DoCMD(tmpip) {
  423. var tmpstr string
  424. isok2, udpstr := cluster.SshPgrep(tmpip, "pgrep udp2019")
  425. tmpstr += udpstr + ";&nbsp;&nbsp;"
  426. isok3, fil2textstr := cluster.SshPgrep(tmpip, "pgrep file2text")
  427. tmpstr += fil2textstr
  428. if isok2 && isok3 {
  429. (*escObject)[0]["OcrTaskStatus"] = "successful"
  430. mongodb.Update("ocr_ecs", bson.M{"_id": (*escObject)[0]["_id"]}, (*escObject)[0], true, false)
  431. config.CID = append(config.CID, qu.ObjToString((*escObject)[0]["InstanceId"]))
  432. log.Println((*escObject)[0]["_id"], tmpip, "部署成功")
  433. return tmpip + "部署成功," + tmpstr
  434. } else {
  435. return tmpip + "部署异常," + tmpstr
  436. }
  437. } else {
  438. return tmpip + "部署失败"
  439. }
  440. }
  441. return "重新部署" + ip + "未知错误,查询多台实例的详细信息,没有查询到新创建实例"
  442. }
  443. func compute() int {
  444. nowtime := time.Now().Unix()
  445. taskArrase := mongodb.Find("ocr_task", bson.M{}, `{_id:1}`, nil, false, -1, -1)
  446. if taskArrase == nil || len(*taskArrase) == 0 {
  447. return 0
  448. }
  449. stmp := (*taskArrase)[0]
  450. etmp := (*taskArrase)[len(*taskArrase)-1]
  451. if stmp != nil && etmp != nil {
  452. stime := qu.Int64All(stmp["import_time"])
  453. if nowtime-stime <= qu.Int64All(config.Sysconfig["time_consuming_limit"]) {
  454. return 0
  455. }
  456. sid := bson.ObjectIdHex(qu.ObjToString(stmp["start"]))
  457. eid := bson.ObjectIdHex(qu.ObjToString(etmp[qu.ObjToString(config.EidField)]))
  458. sum := mongodb.Count("bidding", bson.M{"_id": bson.M{
  459. "$gte": sid,
  460. "$lt": eid,
  461. }})
  462. if sum <= qu.IntAll(config.Sysconfig["accumulated_task_limit"]) {
  463. return 0
  464. }
  465. gteid := bson.ObjectIdHex(qu.ObjToString(stmp[qu.ObjToString(config.SidField)]))
  466. overNum := mongodb.Count("bidding", bson.M{"_id": bson.M{
  467. "$gte": sid,
  468. "$lt": gteid,
  469. }})
  470. if overNum == 0 {
  471. return 0
  472. }
  473. if sum <= qu.IntAll(config.Sysconfig["accumulated_task_lowlimit"]) {
  474. return 0
  475. }
  476. mtmm := overNum / int(nowtime-stime) / (len(config.CID) + 3) //每台每秒
  477. cc := sum/qu.IntAll(config.Sysconfig["corntime_consuming"])/mtmm - len(config.CID) - 3
  478. log.Println("overNum:", overNum, ",hs:", int(nowtime-stime), ",mtms:", mtmm, ",sum:", sum, cc)
  479. if cc > qu.IntAll(config.Sysconfig["pernum"]) {
  480. return qu.IntAll(config.Sysconfig["pernum"])
  481. } else {
  482. return cc
  483. }
  484. }
  485. return 0
  486. }