SpChecker.go 1.21 KB
Newer Older
吴贤德's avatar
吴贤德 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package Controller

import (
	cmap "github.com/orcaman/concurrent-map"
	log "github.com/sirupsen/logrus"
	"src/Common/DateTime"
	"src/Common/Unit"
	"src/Config"
	"src/Model/Sp"
	"strconv"
	"time"
)

type SpChecker struct {
	Config *Config.Config
	queue cmap.ConcurrentMap
}

func (this *SpChecker) Start() {
	this.queue =cmap.New()
	//定时遍历SP,检查SP是否启动
	go func() {
	START:
		s, ok := this.Config.Sys.Get("config_sp_check_seconds")
		if ok {
			this.check()
			n := Unit.Time2Seconds(s.(string))+10
			time.Sleep(time.Duration(n) * time.Second)
		}
		goto START
	}()
}

func (this *SpChecker) check() {
	for _, tmp := range this.Config.Sps.Items() {
		sp := tmp.(map[string]interface{})
		spid:= sp["id"].(string);
		push_type,_:=strconv.Atoi(sp["push_type"].(string)) //0不推送,1推送
		if push_type == 1 {
			if !this.queue.Has(spid) {
				this.queue.Set(spid, "running")
				spModel := new(Sp.SpModel)
				spModel.Config = this.Config
				spModel.Spid = spid
				st, _ := DateTime.Parse("YYYY-MM-DD hh:mm:ss", sp["push_offset"].(string))
				spModel.StartTime = DateTime.Format("YYYYMMDDhhmmss", st)
				go spModel.Start()
			}
		}else{
			log.Infof("this sp do not need to push media , spinfo : %v",sp)
		}
	}
}