fe25921f9a
add: store UpdateOperator
229 lines
6.9 KiB
Go
229 lines
6.9 KiB
Go
package intimate
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
// OperatorFlag 标志
|
|
type OperatorFlag int32
|
|
|
|
const (
|
|
// OperatorOK 等待被处理
|
|
OperatorOK OperatorFlag = 100
|
|
// OperatorExtractorOK 提取数据完成
|
|
OperatorExtractorOK OperatorFlag = 200
|
|
// OperatorWait 等待被处理
|
|
OperatorWait OperatorFlag = 1000
|
|
// OperatorError 错误标志
|
|
OperatorError OperatorFlag = 10000
|
|
)
|
|
|
|
type ISet interface {
|
|
Set(string, interface{})
|
|
}
|
|
|
|
// SourceStore 储存
|
|
type SourceStore struct {
|
|
table string
|
|
db *sql.DB
|
|
errorCount int
|
|
errorLimit int
|
|
}
|
|
|
|
// NewSourceStore 创建一个存储实例
|
|
func NewSourceStore(table string) *SourceStore {
|
|
db, err := sql.Open("mysql", InitConfig.Database.SourceURI)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &SourceStore{table: table, db: db}
|
|
}
|
|
|
|
func (store *SourceStore) errorAlarm(err error) {
|
|
if err != nil {
|
|
log.Println("store error: ", err)
|
|
// 报警. 如果数据插入有问题
|
|
store.errorCount++
|
|
if store.errorCount >= store.errorLimit {
|
|
// 数据库频繁操作初问题 报警, 减少没意义的请求
|
|
}
|
|
} else {
|
|
if store.errorCount > 0 {
|
|
store.errorCount--
|
|
}
|
|
}
|
|
}
|
|
|
|
// Insert 插入数据
|
|
func (store *SourceStore) Insert(isource IGetSource) {
|
|
_, err := store.db.Exec("insert into "+store.table+"(url, target_type, source, ext, operator, error_msg) values(?,?,?,?,?,?)", isource.GetUrl(), isource.GetTargetType(), isource.GetSource(), isource.GetExt(), isource.GetOperator(), isource.GetErrorMsg())
|
|
store.errorAlarm(err)
|
|
}
|
|
|
|
// Update 更新数据
|
|
func (store *SourceStore) Update(isource IUpdateSource) {
|
|
_, err := store.db.Exec("update "+store.table+" set ext = ?, pass_gob = ?, operator = ?, error_msg = ? where uid = ?", isource.GetExt(), isource.GetPassGob(), isource.GetOperator(), isource.GetErrorMsg(), isource.GetUid())
|
|
store.errorAlarm(err)
|
|
}
|
|
|
|
// UpdateOperator 更新数据操作标志位
|
|
func (store *SourceStore) UpdateOperator(isource IUpdateSource) {
|
|
_, err := store.db.Exec("update "+store.table+" set operator = ?, error_msg = ? where uid = ?", isource.GetOperator(), isource.GetErrorMsg(), isource.GetUid())
|
|
store.errorAlarm(err)
|
|
}
|
|
|
|
// UpdateError 更新错误数据
|
|
func (store *SourceStore) UpdateError(isource IUpdateSource, err error) {
|
|
isource.SetOperator(int32(OperatorError))
|
|
isource.SetErrorMsg(sql.NullString{String: err.Error(), Valid: true})
|
|
_, dberr := store.db.Exec("update "+store.table+" set operator = ?, error_msg = ? where uid = ?", isource.GetOperator(), isource.GetErrorMsg(), isource.GetUid())
|
|
store.errorAlarm(dberr)
|
|
}
|
|
|
|
// Restore 恢复Operator数据状态
|
|
func (store *SourceStore) Restore(isource IUpdateSource) {
|
|
_, err := store.db.Exec("update "+store.table+" set operator = ? where uid = ?", isource.GetLastOperator(), isource.GetUid())
|
|
store.errorAlarm(err)
|
|
}
|
|
|
|
// Pop 弹出一条未处理的数据
|
|
func (store *SourceStore) Pop(targetType string, operators ...int32) (IUpdateSource, error) {
|
|
|
|
tx, err := store.db.Begin()
|
|
if err != nil {
|
|
log.Println(err, targetType)
|
|
return nil, err
|
|
}
|
|
var args = []interface{}{targetType}
|
|
selectSQL := `select uid, url, target_type, source, ext, operator, update_time from ` + store.table + ` where target_type = ?`
|
|
if len(operators) == 0 {
|
|
selectSQL += " and operator = ?"
|
|
args = append(args, 0)
|
|
} else {
|
|
for _, operator := range operators {
|
|
selectSQL += " and operator = ?"
|
|
args = append(args, operator)
|
|
}
|
|
}
|
|
|
|
// log.Println(selectSQL + ` limit 1 for update`)
|
|
row := tx.QueryRow(selectSQL+` limit 1 for update`, args...)
|
|
|
|
defer func() {
|
|
err := tx.Commit()
|
|
if err != nil {
|
|
log.Println(err)
|
|
err = tx.Rollback()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
}()
|
|
|
|
s := &Source{}
|
|
// uid, url, target_type, source, ext, operator
|
|
err = row.Scan(&s.Uid, &s.Url, &s.TargetType, &s.Source, &s.Ext, &s.Operator, &s.UpdateTime)
|
|
if err != nil {
|
|
log.Println(err, targetType)
|
|
return nil, err
|
|
}
|
|
s.SetLastOperator(s.Operator)
|
|
_, err = tx.Exec("update "+store.table+" set operator = ? where uid = ?", OperatorWait, s.Uid)
|
|
return s, nil
|
|
}
|
|
|
|
// AnchorTable 主播表名称
|
|
const AnchorTable string = "anchor_info"
|
|
|
|
// CollectLogTable 采集日志表
|
|
const CollectLogTable string = "collect_log"
|
|
|
|
type ExtractorStore struct {
|
|
db *sql.DB
|
|
|
|
errorCount int
|
|
errorLimit int
|
|
}
|
|
|
|
func (store *ExtractorStore) errorAlarm(err error) {
|
|
if err != nil {
|
|
log.Panic("store error: ", err)
|
|
// 报警. 如果数据插入有问题
|
|
store.errorCount++
|
|
if store.errorCount >= store.errorLimit {
|
|
// 数据库频繁操作初问题 报警, 减少没意义的请求
|
|
}
|
|
} else {
|
|
if store.errorCount > 0 {
|
|
store.errorCount--
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewExtractorStore() *ExtractorStore {
|
|
db, err := sql.Open("mysql", InitConfig.Database.ExtractorURI)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &ExtractorStore{db: db}
|
|
}
|
|
|
|
/*
|
|
`uid` bigint,
|
|
`platform` varchar(255) NOT NULL,
|
|
`anchor_id` varchar(255) NOT NULL,
|
|
`anchor_name` varchar(255) NOT NULL,
|
|
`live_url` text,
|
|
`channel` varchar(128) DEFAULT NULL,
|
|
`show_type` varchar(255) DEFAULT NULL,
|
|
*/
|
|
|
|
// InsertAnchorInfo AnchorInfo表, 插入数据
|
|
func (store *ExtractorStore) InsertAnchorInfo(isource IGetAnchorInfo) (Uid int64, err error) {
|
|
// select uid from table where platform = ? and anchor_id = ?
|
|
selectSQL := "select uid from " + AnchorTable + " where platform = ? and anchor_id = ?"
|
|
tx, err := store.db.Begin()
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, err
|
|
}
|
|
log.Println(isource.GetPlatform(), isource.GetAnchorId())
|
|
row := tx.QueryRow(selectSQL+` limit 1 for update`, isource.GetPlatform(), isource.GetAnchorId())
|
|
|
|
var uid int64
|
|
if err = row.Scan(&uid); err == nil {
|
|
return uid, nil
|
|
}
|
|
|
|
result, err := tx.Exec("insert into "+AnchorTable+"(platform, anchor_id, anchor_name, live_url, channel, tags, ext) values(?,?,?,?,?,?,?);", isource.GetPlatform(), isource.GetAnchorId(), isource.GetAnchorName(), isource.GetLiveUrl(), isource.GetChannel(), isource.GetTags(), isource.GetExt())
|
|
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, nil
|
|
}
|
|
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
log.Println(err)
|
|
err = tx.Rollback()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
return 0, err
|
|
}
|
|
|
|
return result.LastInsertId()
|
|
}
|
|
|
|
// InsertCollectLog CollectLog表插入数据
|
|
func (store *ExtractorStore) InsertCollectLog(isource IGetCollectLog) error {
|
|
_, err := store.db.Exec("insert into "+CollectLogTable+"(uid, platform, anchor_id, is_live_streaming, is_error, followers, views, giver, gratuity, live_title, live_start_time, live_end_time, update_time, tags, ext, error_msg) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
|
isource.GetUid(), isource.GetPlatform(), isource.GetAnchorId(), isource.GetIsLiveStreaming(), isource.GetIsError(), isource.GetFollowers(), isource.GetViews(), isource.GetGiver(), isource.GetGratuity(), isource.GetLiveTitle(), isource.GetLiveStartTime(), isource.GetLiveEndTime(), isource.GetUpdateTime(), isource.GetTags(), isource.GetExt(), isource.GetErrorMsg(),
|
|
)
|
|
store.errorAlarm(err)
|
|
return err
|
|
}
|