wangchuanjin %!s(int64=2) %!d(string=hai) anos
pai
achega
b93529fd4b
Modificáronse 2 ficheiros con 33 adicións e 14 borrados
  1. 29 13
      p/public.go
  2. 4 1
      p/timetask.go

+ 29 - 13
p/public.go

@@ -3,6 +3,7 @@ package p
 import (
 	"encoding/json"
 	"fmt"
+	"net/http"
 	"reflect"
 	"sort"
 	"strconv"
@@ -212,20 +213,17 @@ func GetInfoTitle(info *map[string]interface{}) string {
 }
 
 //加载数据到内存中
-func LoadBidding(mgo *MongodbSim, dbName, coll string, startTime int64, redisCache bool) (*[]map[string]interface{}, int64) {
+func LoadBidding(mgo *MongodbSim, dbName, coll string, startTime int64, redisCache bool, query map[string]interface{}) (*[]map[string]interface{}, int64) {
 	defer util.Catch()
 	endTime := time.Now().Unix()
-	query := map[string]interface{}{
-		"pici": map[string]interface{}{
-			"$gte": startTime,
-			"$lt":  endTime,
-		},
+	if query == nil || len(query) == 0 {
+		query = map[string]interface{}{
+			"pici": map[string]interface{}{
+				"$gte": startTime,
+				"$lt":  endTime,
+			},
+		}
 	}
-	// query = map[string]interface{}{
-	// 	"_id": map[string]interface{}{
-	// 		"$in": ToObjectIds([]string{"5b7bd6b9a5cb26b9b7449fd7", "5b7bd6b9a5cb26b9b7449fe4", "626c4b63923488e172579d81", "5b7bd6b9a5cb26b9b7449fde"}),
-	// 	},
-	// }
 	logger.Info("开始加载", coll, "数据", query)
 	var res []map[string]interface{}
 	sess := mgo.GetMgoConn()
@@ -703,9 +701,9 @@ func IdentityByEntUserId(base, main *Mysql, entUserId int64) *IdentityInfo {
 //格式化小时
 func HourFormat(hour int) string {
 	if hour < 10 {
-		return fmt.Sprintf("0%d:00")
+		return fmt.Sprintf("0%d:00", hour)
 	} else {
-		return fmt.Sprintf("%d:00")
+		return fmt.Sprintf("%d:00", hour)
 	}
 }
 
@@ -727,3 +725,21 @@ func TimesIsOver(times []string, hour int) (bool, time.Time, time.Time) {
 		return hour > lastHour, start, now
 	}
 }
+
+//监听超时
+func MonitorTimeOut(pool chan bool, timeoutWarn string, f func()) bool {
+	select {
+	case <-time.After(1 * time.Minute):
+		go func() {
+			f()
+			if timeoutWarn != "" {
+				if _, err := http.Get(timeoutWarn); err != nil {
+					logger.Error("发送告警邮件错误", err)
+				}
+			}
+		}()
+		return false
+	case pool <- true:
+	}
+	return true
+}

+ 4 - 1
p/timetask.go

@@ -138,11 +138,14 @@ func (t *timeTask) RunEveryHour(runRightNow bool, f func()) {
 	}
 	now := time.Now()
 	nextHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour()+1, 0, 0, 0, time.Local)
-	ticker := time.NewTicker(nextHour.Sub(now))
+	sub := nextHour.Sub(now)
+	logger.Info("start runEveryHour after", sub)
+	ticker := time.NewTicker(sub)
 	for {
 		select {
 		case <-ticker.C:
 			go f()
+			logger.Info("start runEveryHour after", time.Hour)
 			ticker.Reset(time.Hour)
 		}
 	}