2020-07-07 10:39:24 +00:00
|
|
|
package intimate
|
2020-07-06 08:33:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-07-08 07:02:55 +00:00
|
|
|
"errors"
|
2020-07-06 09:58:24 +00:00
|
|
|
"log"
|
2020-07-06 08:33:35 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
2020-07-08 07:02:55 +00:00
|
|
|
// IGetSource 源接口结构
|
|
|
|
type IGetSource interface {
|
2020-07-07 10:14:14 +00:00
|
|
|
GetUrl() string //
|
|
|
|
GetTargetType() string //
|
|
|
|
GetSource() sql.NullString //
|
|
|
|
GetExt() interface{} //
|
|
|
|
GetUpdateTime() time.Time //
|
|
|
|
GetOperator() int32 //
|
|
|
|
GetErrorMsg() sql.NullString //
|
2020-07-08 07:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IUpdateSource interface {
|
|
|
|
IGetSource
|
2020-07-07 10:14:14 +00:00
|
|
|
|
2020-07-08 07:02:55 +00:00
|
|
|
SetExt(ext interface{}) //
|
|
|
|
SetUpdateTime(ut time.Time) //
|
|
|
|
SetOperator(operator int32) //
|
|
|
|
SetErrorMsg(emsg sql.NullString) //
|
2020-07-06 08:33:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 07:02:55 +00:00
|
|
|
// OperatorFlag 标志
|
|
|
|
type OperatorFlag int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
// OperatorWait 等待被处理
|
|
|
|
OperatorWait OperatorFlag = 1000
|
|
|
|
// OperatorError 错误标志
|
|
|
|
OperatorError OperatorFlag = 10000
|
|
|
|
)
|
|
|
|
|
2020-07-06 08:33:35 +00:00
|
|
|
// Store 储存
|
|
|
|
type Store struct {
|
2020-07-08 07:02:55 +00:00
|
|
|
table string
|
|
|
|
db *sql.DB
|
2020-07-06 08:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStore 创建一个存储实例
|
2020-07-08 07:02:55 +00:00
|
|
|
func NewStore(table string) *Store {
|
2020-07-06 08:33:35 +00:00
|
|
|
db, err := sql.Open("mysql", InitConfig.Database.URI)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-08 07:02:55 +00:00
|
|
|
return &Store{table: table, db: db}
|
2020-07-06 08:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save 储存数据
|
2020-07-08 07:02:55 +00:00
|
|
|
func (store *Store) Save(isource IGetSource) {
|
|
|
|
_, err := store.db.Exec("insert into `source_openrec`(url, target_type, source, ext, operator, error_msg) values(?,?,?,?,?,?)", isource.GetUrl(), isource.GetTargetType(), isource.GetSource(), isource.GetExt(), isource.GetOperator(), isource.GetErrorMsg())
|
2020-07-06 09:58:24 +00:00
|
|
|
if err != nil {
|
2020-07-07 04:04:05 +00:00
|
|
|
log.Fatalln(err)
|
2020-07-06 09:58:24 +00:00
|
|
|
}
|
2020-07-06 08:33:35 +00:00
|
|
|
}
|
2020-07-08 07:02:55 +00:00
|
|
|
|
|
|
|
// Pop 储存数据
|
|
|
|
func (store *Store) 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 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, args...)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
err = tx.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if row != nil {
|
|
|
|
s := &Source{}
|
|
|
|
// uid, url, target_type, source, ext, operator
|
|
|
|
err = row.Scan(&s.Uid, &s.Url, &s.TargetType, &s.Source, &s.Ext, &s.Operator)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err, targetType)
|
|
|
|
_, err = tx.Exec("update "+store.table+" set error_msg = ?, operator = ? where uid = ?", OperatorError, s.Uid)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec("update "+store.table+" set operator = ? where uid = ?", OperatorWait, s.Uid)
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("TaskQueue is nil")
|
|
|
|
}
|