claim.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package front
  2. import (
  3. "fmt"
  4. qu "qfw/util"
  5. "strings"
  6. "sync"
  7. "time"
  8. . "util"
  9. )
  10. var (
  11. //爬虫认领
  12. ClaimMaxNum = 10 //已认领爬虫上限
  13. ClaimLock = &sync.Mutex{}
  14. ClaimPriorityLimit = 350 //区分外包和内部人员认领爬虫的优先级界限
  15. ClaimPriorityOut = 200
  16. ClaimPriorityIn = 700
  17. ClaimQueryFields = map[string]interface{}{
  18. "site": 1,
  19. "code": 1,
  20. "channel": 1,
  21. "priority": 1,
  22. "spiderimportant": 1,
  23. "modifyuser": 1,
  24. "claimtime": 1,
  25. "claimtype": 1,
  26. "recovertime": 1,
  27. //"grade": 1,
  28. }
  29. )
  30. //type ClaimLog struct {
  31. // Site string `bson:"site"`
  32. // Code string `bson:"code"`
  33. // Channel string `bson:"channel"`
  34. // ModifyUser string `bson:"modifyuser"`
  35. // Priority int `bson:"priority"`
  36. // Comeintime int64 `bson:"comeintime"`
  37. // Important bool `bson:"important"`
  38. // ClaimTime int64 `bson:"claimtime"`
  39. // RecoverTime int64 `bson:"recovertime"`
  40. // ReturnTime int64 `bson:"returntime"`
  41. // Stype string `bson:"stype"` //认领、回收、归还
  42. // ReturnReason string `bson:"returnreason"` //归还原因
  43. // ClaimRecoverType int `bson:"claimrecovertype"` //认领或回收类型 1:主动;0:被动(爬虫分配)
  44. //}
  45. const CLAIMTYPEUNCLAIMED, CLAIMTYPECLAIMED, CLAIMTYPEHISTORY = 0, 1, 2 //未认领、已认领、历史爬虫
  46. func (f *Front) ClaimCode() {
  47. ClaimLock.Lock()
  48. defer ClaimLock.Unlock()
  49. modifyuser := f.GetSession("loginuser")
  50. identity := qu.IntAll(f.GetSession("identity"))
  51. //1、已认领爬虫个数
  52. q := map[string]interface{}{
  53. "modifyuser": modifyuser,
  54. "claimtype": CLAIMTYPECLAIMED,
  55. "state": map[string]interface{}{
  56. "$in": []int{0, 2}, //待完成、未通过
  57. },
  58. "platform": map[string]interface{}{
  59. "$in": []string{"golua平台", "chrome"},
  60. },
  61. }
  62. num := MgoEB.Count("luaconfig", q)
  63. if num >= ClaimMaxNum {
  64. f.ServeJson(map[string]interface{}{"msg": "已超爬虫认领上限!", "ok": false})
  65. return
  66. }
  67. //2、认领爬虫逻辑
  68. q = map[string]interface{}{
  69. "claimtype": CLAIMTYPEUNCLAIMED, //待认领
  70. "platform": map[string]interface{}{
  71. "$in": []string{"golua平台", "chrome"},
  72. },
  73. "state": Sp_state_0,
  74. }
  75. s := 1
  76. if identity == 1 { //内部人员
  77. s = -1
  78. q["priority"] = map[string]interface{}{
  79. "$gte": ClaimPriorityLimit,
  80. }
  81. } else { //外包指定优先级范围且简单的爬虫
  82. q["priority"] = map[string]interface{}{
  83. "$gt": 0,
  84. "$lt": ClaimPriorityLimit,
  85. }
  86. q["grade"] = 0
  87. }
  88. sort := map[string]interface{}{
  89. "priority": s,
  90. }
  91. qu.Debug(f.GetSession("loginuser"), "认领爬虫:", q)
  92. lua, _ := MgoEB.Find("luaconfig", q, sort, ClaimQueryFields, false, -1, -1)
  93. if len(*lua) > 0 {
  94. claimNum := 0 //本次认领爬虫个数
  95. claimSite := map[string]bool{} //本次认领的站点
  96. for _, l := range *lua {
  97. site := qu.ObjToString(l["site"])
  98. if claimSite[site] {
  99. continue
  100. }
  101. claimSite[site] = true
  102. q["site"] = site //查询指定站点爬虫
  103. //delete(q, "priority") //这里不限制优先级
  104. tmpLua, _ := MgoEB.Find("luaconfig", q, nil, ClaimQueryFields, false, -1, -1)
  105. claimNum += len(*tmpLua)
  106. //更新数据、新增日志
  107. UpdateCodeAndSaveLog(*tmpLua, f)
  108. if num+claimNum >= ClaimMaxNum {
  109. break
  110. }
  111. }
  112. f.ServeJson(map[string]interface{}{"msg": "成功认领爬虫" + fmt.Sprint(claimNum) + "个", "ok": true})
  113. return
  114. }
  115. f.ServeJson(map[string]interface{}{"msg": "暂无可认领爬虫!", "ok": false})
  116. }
  117. func (f *Front) ReturnCode() {
  118. identity := qu.IntAll(f.GetSession("identity"))
  119. if identity != 0 {
  120. f.ServeJson(map[string]interface{}{"msg": "无权限归还爬虫!", "ok": false})
  121. return
  122. }
  123. codes := f.GetString("codes")
  124. returnreason := f.GetString("returnreason")
  125. qu.Debug(codes)
  126. cs := strings.Split(codes, ",")
  127. q := map[string]interface{}{
  128. "code": map[string]interface{}{
  129. "$in": cs,
  130. },
  131. }
  132. luas, _ := MgoEB.Find("luaconfig", q, nil, ClaimQueryFields, false, -1, -1)
  133. if len(*luas) > 0 {
  134. update := [][]map[string]interface{}{}
  135. save := []map[string]interface{}{}
  136. for _, l := range *luas {
  137. now := time.Now().Unix()
  138. up := []map[string]interface{}{
  139. {"code": l["code"]},
  140. {"$set": map[string]interface{}{
  141. "claimtype": CLAIMTYPEUNCLAIMED,
  142. "claimtime": int64(0),
  143. "recovertime": int64(0),
  144. "priority": ClaimPriorityIn,
  145. "state": 0,
  146. "grade": 1, //外包归还爬虫后,爬虫难易度更新为困难
  147. }},
  148. }
  149. update = append(update, up)
  150. save = append(save, map[string]interface{}{
  151. "site": l["site"],
  152. "code": l["code"],
  153. "channel": l["channel"],
  154. "modifyuser": l["modifyuser"],
  155. "priority": l["priority"],
  156. "stype": "归还",
  157. "comeintime": now,
  158. "claimtime": l["claimtime"],
  159. "recovertime": l["recovertime"],
  160. "returntime": now,
  161. "important": l["spiderimportant"],
  162. "returnreason": returnreason,
  163. "claimrecovertype": 1,
  164. "source": "golua平台爬虫归还",
  165. })
  166. }
  167. //更新爬虫信息
  168. MgoEB.UpdateBulk("luaconfig", update...)
  169. //保存认领日志
  170. MgoEB.SaveBulk("lua_logs_claim", save...)
  171. f.ServeJson(map[string]interface{}{"msg": "爬虫归还成功!", "ok": true})
  172. return
  173. }
  174. f.ServeJson(map[string]interface{}{"msg": "爬虫归还失败!", "ok": false})
  175. }
  176. func UpdateCodeAndSaveLog(lua []map[string]interface{}, f *Front) {
  177. update := [][]map[string]interface{}{}
  178. save := []map[string]interface{}{}
  179. for _, l := range lua {
  180. loginuser := f.GetSession("loginuser")
  181. userid := f.GetSession("userid")
  182. priority := qu.IntAll(l["priority"])
  183. spiderimportant, _ := l["spiderimportant"].(bool)
  184. recovertime := CreateRecovertime(spiderimportant, priority)
  185. now := time.Now().Unix()
  186. up := []map[string]interface{}{
  187. {"code": l["code"]},
  188. {"$set": map[string]interface{}{
  189. "claimtype": CLAIMTYPECLAIMED,
  190. "createuseremail": f.GetSession("email"),
  191. "createuser": loginuser,
  192. "createuserid": userid,
  193. "modifyuser": loginuser,
  194. "modifyuserid": userid,
  195. "claimtime": now,
  196. "state": Sp_state_0, //通用平台退回爬虫状态为:无法标注12
  197. "recovertime": recovertime,
  198. }},
  199. }
  200. update = append(update, up)
  201. save = append(save, map[string]interface{}{
  202. "site": l["site"],
  203. "code": l["code"],
  204. "channel": l["channel"],
  205. "modifyuser": loginuser,
  206. "priority": priority,
  207. "stype": "认领",
  208. "comeintime": now,
  209. "claimtime": now,
  210. "recovertime": recovertime,
  211. "returntime": int64(0),
  212. "important": spiderimportant,
  213. "returnreason": "",
  214. "claimrecovertype": 1,
  215. "source": "golua平台爬虫认领",
  216. })
  217. }
  218. //更新爬虫信息
  219. MgoEB.UpdateBulk("luaconfig", update...)
  220. //保存认领日志
  221. MgoEB.SaveBulk("lua_logs_claim", save...)
  222. }
  223. // CreateRecovertime 生成归还时间
  224. func CreateRecovertime(important bool, priority int) int64 {
  225. if important || priority >= ClaimPriorityLimit {
  226. return time.Now().Add(24 * time.Hour).Unix()
  227. } else {
  228. return time.Now().Add(24 * 5 * time.Hour).Unix()
  229. }
  230. }
  231. // UpdateLuaClaimtype 更新爬虫认领状态
  232. func UpdateLuaClaimtype(code string, claimtype int) {
  233. MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{
  234. "claimtype": claimtype,
  235. "claimtime": int64(0),
  236. "recovertime": int64(0),
  237. }}, false, false)
  238. }