Commit 7951f0ce authored by 吴贤德's avatar 吴贤德

中心服务:站点改造相关逻辑,播放日志表结构维护

parent 75479433
......@@ -12,6 +12,7 @@ type Config struct {
Ini map[string]string //Ini配置
Sys cmap.ConcurrentMap //Sys配置
Sps cmap.ConcurrentMap //Sp配置
Sites cmap.ConcurrentMap //Site配置
}
var config *Config
......@@ -24,6 +25,7 @@ func GetInstance() *Config {
initLogConfig(config.Ini["log_path"], config.Ini["log_filename"], 24*30*time.Hour, 1*time.Hour)
config.Sys = cmap.New()
config.Sps = cmap.New()
config.Sites = cmap.New()
})
return config
......@@ -76,3 +78,27 @@ func (this *Config) InitSpsConfig() error {
}
return err
}
func (this *Config) InitSitesConfig() error {
url := this.Ini["api_conf_sites"]
var data string = ""
var err error
data, err = Request.Get(url)
if err == nil {
jsonObj, err1 := simplejson.NewJson([]byte(data))
if err1 != nil {
err = err1
} else {
sitesConf, err2 := jsonObj.Map()
if err2 != nil {
err = err2
} else {
for k, v := range sitesConf {
//log.Infoln(k, v)
this.Sites.Set(k, v)
}
}
}
}
return err
}
\ No newline at end of file
package Controller
import (
"github.com/sirupsen/logrus"
"src/Common/Request"
"src/Config"
"time"
)
type TableCreater struct {
Config *Config.Config
}
func (this *TableCreater) Start() {
go func() {
START:
n:=3600*6
this.createTable()
time.Sleep(time.Duration(n) * time.Second)
goto START
}()
}
func (this *TableCreater) createTable() {
url := this.Config.Ini["api_tb_playlog_create"]
var data string = ""
var err error
data, err = Request.Get(url)
if err == nil {
logrus.Debugln("TableCreater OK :", data)
}else {
logrus.Debugln("TableCreater ERR :", err.Error())
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import (
cmap "github.com/orcaman/concurrent-map"
"src/Common/Unit"
"src/Config"
"src/Model/Pull"
"src/Model/Wasu"
"time"
)
......@@ -20,7 +21,8 @@ func (this *WasuChecker) Start() {
START:
s, ok := this.Config.Sys.Get("config_sp_check_seconds")
if ok {
this.check()
//this.check()
this.checkSite()
n := Unit.Time2Seconds(s.(string))+10
time.Sleep(time.Duration(n) * time.Second)
}
......@@ -44,3 +46,19 @@ func (this *WasuChecker) check() {
}
}
func (this *WasuChecker) checkSite() {
for _, tmp := range this.Config.Sites.Items() {
site := tmp.(map[string]interface{})
siteid:= site["id"].(string);
if !this.queue.Has(siteid) {
this.queue.Set(siteid, "running")
//spModel := new(Wasu.SpModel)
//spModel.Config = this.Config
//spModel.Spid = spid
siteModel := new(Pull.SiteModel)
siteModel.Config=this.Config
siteModel.Siteid=siteid
go siteModel.Start()
}
}
}
\ No newline at end of file
package Pull
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"src/Common/Request"
"src/Config"
"strings"
"unsafe"
)
type Media struct {
SiteId string `json:"site_id"`
Sps []*Sp `json:"sps"`
JsonUrl string `json:"jsonUrl"`
NewsId string `json:"newsId"`
NewsName string `json:"newsName"`
NodeId string `json:"nodeId"`
NodeName string `json:"nodeName"`
// Md5 string `json:"md5"`
// PlayUrl string `json:"playUrl"`
}
type Sp struct {
Spid string `json:"sp_id"`
PushMode string `json:"push_mode"` //是否自动转推:0手动,1自动
}
type MediaList struct {
CurrentPage int `json:"currentPage"`
PageNum int `json:"pageNum"`
PageSize int `json:"pageSize"`
Total int `json:"total"`
List []*Media `json:"list"`
}
type MediaListModel struct {
Config *Config.Config
Siteid string
}
func (this *MediaListModel) FetchList(date string, page int) *MediaList {
wasu_api_incrementList := ""
config,ok := this.Config.Sites.Get(this.Siteid)
if ok {
conf,ok:=config.(map[string]interface{})
if ok {
wasu_api_incrementList=conf["pull_api"].(string)
}
}
if wasu_api_incrementList == "" {
logrus.Error("PULL Site :",this.Siteid," 缺少配置项:pull_api")
}
mediaList := new(MediaList)
if ok {
url := fmt.Sprintf("%s?date=%s&page=%d", wasu_api_incrementList, date, page)
data, err := Request.Get(url)
if err == nil {
json.Unmarshal([]byte(data), mediaList)
}
}
return mediaList
}
func (this *MediaListModel) UpdateMediaList(mediaList *MediaList) bool {
sps := []*Sp{}
spids:=""
config,ok := this.Config.Sites.Get(this.Siteid)
if ok {
conf,ok:=config.(map[string]interface{})
if ok {
spids=conf["spids"].(string)
}
}
spids = ","+spids+","
for _, tmp := range this.Config.Sps.Items() {
sp := tmp.(map[string]interface{})
spid := sp["id"].(string);
idx := strings.Index(spids, ","+spid+",")
if idx > -1 {
sp1 := new(Sp)
sp1.Spid = spid
sp1.PushMode = sp["wasu_incrementList_push_mode"].(string);
sps=append(sps,sp1)
}
}
l := len(mediaList.List)
nOk := 0
for _, media := range mediaList.List {
media.SiteId=this.Siteid
media.Sps=sps
if this.UpdateMedia(media) {
nOk++
}
}
b := false
if nOk == l {
b = true
}
logrus.Debugln("UpdateMediaList : ok/total :", nOk, "/", l)
return b
}
func (this *MediaListModel) UpdateMedia(media *Media) bool {
center_api_media_update, ok := this.Config.Sys.Get("center_api_media_update")
b := false
if ok {
inData, _ := json.Marshal(media)
//fmt.Printf("****** media_info : %v",media)
data, err := Request.Post(center_api_media_update.(string), *(*string)(unsafe.Pointer(&inData)))
//data, err := Request.Post(center_api_media_update.(string), string(inData))
if err == nil {
if data == "true" {
b = true
}
}
}
return b
}
package Pull
import (
log "github.com/sirupsen/logrus"
"src/Common/DateTime"
"src/Common/Unit"
"src/Config"
"time"
)
type SiteModel struct {
Config *Config.Config
Siteid string
}
func (this *SiteModel) Start() {
START:
log.Infoln("启动Site拉取增量媒资检测:",this.Siteid)
config,ok := this.Config.Sites.Get(this.Siteid)
n:=3600
if ok {
conf,ok:=config.(map[string]interface{})
if ok {
wasu_check_seconds:=conf["check_seconds"].(string)
if wasu_check_seconds !="" {
n = Unit.Time2Seconds(wasu_check_seconds)
}
this.pull();
}
}
time.Sleep(time.Duration(n) * time.Second)
goto START
}
func (this *SiteModel) pull() {
log.Infoln("SiteModel.pull Starting...")
mediaModel := new(MediaListModel)
mediaModel.Config = this.Config
mediaModel.Siteid = this.Siteid
date := DateTime.Format("YYYYMMDD", time.Now())
//date="20190901"
//date="" //全量传空
mediaList := mediaModel.FetchList(date, 1)
ok := mediaModel.UpdateMediaList(mediaList)
if !ok {
log.Errorln("mediaModel.UpdateMediaList err: date=", date, "page=", 1)
}
if mediaList != nil {
pageNum := mediaList.PageNum
if pageNum >= 2 {
for page := 2; page <= pageNum; page++ {
mediaList := mediaModel.FetchList(date, page)
ok := mediaModel.UpdateMediaList(mediaList)
if !ok {
log.Errorln("mediaModel.UpdateMediaList err: date=", date, "page=", page)
}
}
}
}
}
[debug]
api_conf_sys = http://192.168.200.134:801/api/center/conf_sys #系统配置接口
api_conf_sps = http://192.168.200.134:801/api/center/conf_sps #合作方配置接口
api_conf_sys = http://192.168.200.134:801/api/center/conf_sys
api_conf_sps = http://192.168.200.134:801/api/center/conf_sps
api_conf_sites = http://192.168.200.134:801/api/center/conf_sites
api_tb_playlog_create = http://192.168.200.134:801/api/center/tb_playlog_create
log_path = ./
log_filename = center_service.log
[release]
api_conf_sys = http://api.hotel.wasu.tv/api/center/conf_sys #系统配置接口
api_conf_sps = http://api.hotel.wasu.tv/api/center/conf_sps #合作方配置接口
api_conf_sites = http://api.hotel.wasu.tv/api/center/conf_sites #站点配置接口
api_tb_playlog_create = http://api.hotel.wasu.tv/api/center/tb_playlog_create #创建播放日志表
log_path = ./
log_filename = center_service.log
\ No newline at end of file
......@@ -13,11 +13,17 @@ func main() {
config := Config.GetInstance()
err1 := config.InitSysConfig()
err2 := config.InitSpsConfig()
err3:=config.InitSitesConfig()
//初始化成功
if err1 == nil && err2 == nil {
if err1 == nil && err2 == nil && err3==nil{
configCHK := new(ConfigChecker)
configCHK.Start()
//创建播放日志表结果(6小时检查一次)
tbCreater := new(TableCreater)
tbCreater.Config = config
tbCreater.Start()
//华数媒资拉取
wasuCHK := new (WasuChecker)
wasuCHK.Config=config
......@@ -30,6 +36,7 @@ func main() {
} else {
log.Errorln("初始化系统配置错误:", err1)
log.Errorln("初始化合作方配置错误:", err2)
log.Errorln("初始化站点配置错误:", err3)
}
runtime.Goexit()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment