flush.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package task
  2. import (
  3. "fmt"
  4. "luaweb/front"
  5. mgdb "qfw/util/mongodb"
  6. "time"
  7. "gopkg.in/mgo.v2/bson"
  8. )
  9. var timer *time.Ticker
  10. func init() {
  11. timer = time.NewTicker(1 * time.Second)
  12. }
  13. func TimeTask() {
  14. for t := range timer.C {
  15. hour := t.Hour()
  16. second := t.Second()
  17. minutes := t.Minute()
  18. if hour == 0 && minutes == 0 && second == 0 {
  19. FlushAuthor()
  20. }
  21. }
  22. }
  23. func FlushAuthor() {
  24. query := bson.M{
  25. "flush": bson.M{"$exists": false},
  26. "next": bson.M{"$exists": true},
  27. "l_uploadtime": bson.M{
  28. "$lte": time.Now().AddDate(0, 0, -30).Unix(),
  29. },
  30. "$where": "this.createuseremail != this.next",
  31. }
  32. rets := *mgdb.Find("luaconfig", query, bson.M{}, bson.M{}, false, -1, -1)
  33. for _, v := range rets {
  34. next, ok := v["next"].(string)
  35. if ok {
  36. one := *mgdb.FindOne("user", bson.M{"s_email": next})
  37. if len(one) > 0 {
  38. update := bson.M{
  39. "$set": bson.M{
  40. "createuser": one["s_name"],
  41. "createuserid": one["_id"].(bson.ObjectId).Hex(),
  42. "createuseremail": next,
  43. "flush": "ok",
  44. },
  45. }
  46. front.Wlog("转移爬虫给", v["code"].(string), fmt.Sprint(one["s_name"]), next, "系统定时任务", nil)
  47. mgdb.Update("luaconfig", bson.M{"_id": v["_id"]}, update, false, false)
  48. }
  49. }
  50. }
  51. }