init: web_hgdjc_server
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package seopushmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var db *mongos.Collection
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
db = mongos.Shop.Coll(TN.SeoPushTask)
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
// 同一个页面的同一种动作只保留一条任务,重复发布时复用并重置状态
|
||||
Keys: bson.D{
|
||||
{Key: FN.SeoPushTask.Url, Value: 1},
|
||||
{Key: FN.SeoPushTask.Action, Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.SeoPushTask.GoogleStatus, Value: 1},
|
||||
{Key: FN.SeoPushTask.IndexNowStatus, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.SeoPushTask.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(SeoPushTask:Init) Create SeoPushTask Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// UpsertPendingSeoPushTask 登记一条待推送任务,已存在则重置为待推送状态
|
||||
func UpsertPendingSeoPushTask(ctx context.Context, task *SeoPushTask) *srvdef.SrvErr {
|
||||
filter := bson.M{
|
||||
FN.SeoPushTask.Url: task.Url,
|
||||
FN.SeoPushTask.Action: task.Action,
|
||||
}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
FN.SeoPushTask.VideoId: task.VideoId,
|
||||
FN.SeoPushTask.GoogleStatus: PushStatusPending,
|
||||
FN.SeoPushTask.GoogleRetry: 0,
|
||||
FN.SeoPushTask.GoogleErr: "",
|
||||
FN.SeoPushTask.IndexNowStatus: PushStatusPending,
|
||||
FN.SeoPushTask.IndexNowRetry: 0,
|
||||
FN.SeoPushTask.IndexNowErr: "",
|
||||
FN.SeoPushTask.UpdatedAt: task.UpdatedAt,
|
||||
},
|
||||
"$setOnInsert": bson.M{
|
||||
FN.SeoPushTask.CreatedAt: task.CreatedAt,
|
||||
},
|
||||
}
|
||||
if _, err := db.UpsertOne(ctx, filter, update); err != nil {
|
||||
log.Warn("seopushmod:UpsertPendingSeoPushTask db.UpsertOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManySeoPushTasks 查找一批推送任务
|
||||
func FindManySeoPushTasks(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) (SeoPushTaskList, *srvdef.SrvErr) {
|
||||
data := make(SeoPushTaskList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("seopushmod:FindManySeoPushTasks db.Find failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// UpdateOneSeoPushTask 更新一条推送任务
|
||||
func UpdateOneSeoPushTask(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
if _, err := db.UpdateOne(ctx, filter, update); err != nil {
|
||||
log.Warn("seopushmod:UpdateOneSeoPushTask db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelManySeoPushTask 删除一批推送任务
|
||||
func DelManySeoPushTask(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteMany(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("seopushmod:DelManySeoPushTask db.DeleteMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// CountSeoPushTasks 统计推送任务数量
|
||||
func CountSeoPushTasks(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("seopushmod:CountSeoPushTasks db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package seopushmod
|
||||
|
||||
type TagSeoPushTask struct {
|
||||
ID string
|
||||
Url string
|
||||
VideoId string
|
||||
Action string
|
||||
|
||||
GoogleStatus string
|
||||
GoogleRetry string
|
||||
GoogleErr string
|
||||
GoogleAt string
|
||||
|
||||
IndexNowStatus string
|
||||
IndexNowRetry string
|
||||
IndexNowErr string
|
||||
IndexNowAt string
|
||||
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
SeoPushTask TagSeoPushTask
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
SeoPushTask string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.SeoPushTask.ID = "_id"
|
||||
FN.SeoPushTask.Url = "url"
|
||||
FN.SeoPushTask.VideoId = "videoId"
|
||||
FN.SeoPushTask.Action = "action"
|
||||
|
||||
FN.SeoPushTask.GoogleStatus = "googleStatus"
|
||||
FN.SeoPushTask.GoogleRetry = "googleRetry"
|
||||
FN.SeoPushTask.GoogleErr = "googleErr"
|
||||
FN.SeoPushTask.GoogleAt = "googleAt"
|
||||
|
||||
FN.SeoPushTask.IndexNowStatus = "indexNowStatus"
|
||||
FN.SeoPushTask.IndexNowRetry = "indexNowRetry"
|
||||
FN.SeoPushTask.IndexNowErr = "indexNowErr"
|
||||
FN.SeoPushTask.IndexNowAt = "indexNowAt"
|
||||
|
||||
FN.SeoPushTask.CreatedAt = "createdAt"
|
||||
FN.SeoPushTask.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.SeoPushTask = "seo_push_task"
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package seopushmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// SeoPushTask 搜索引擎推送任务(Google Indexing API / IndexNow)
|
||||
// 发布/更新/删除时即时推送一次,本表用于失败兜底重试。
|
||||
// @table:seo_push_task
|
||||
type SeoPushTask struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
Url string `json:"url" bson:"url"` // 页面完整地址
|
||||
VideoId int64 `json:"videoId" bson:"videoId"` // 关联资源ID
|
||||
Action string `json:"action" bson:"action"` // 推送动作 updated/deleted
|
||||
|
||||
GoogleStatus int64 `json:"googleStatus" bson:"googleStatus"` // Google 推送状态
|
||||
GoogleRetry int64 `json:"googleRetry" bson:"googleRetry"` // Google 已重试次数
|
||||
GoogleErr string `json:"googleErr" bson:"googleErr"` // Google 最近一次错误
|
||||
GoogleAt time.Time `json:"googleAt" bson:"googleAt"` // Google 最近一次推送时间
|
||||
|
||||
IndexNowStatus int64 `json:"indexNowStatus" bson:"indexNowStatus"` // IndexNow 推送状态
|
||||
IndexNowRetry int64 `json:"indexNowRetry" bson:"indexNowRetry"` // IndexNow 已重试次数
|
||||
IndexNowErr string `json:"indexNowErr" bson:"indexNowErr"` // IndexNow 最近一次错误
|
||||
IndexNowAt time.Time `json:"indexNowAt" bson:"indexNowAt"` // IndexNow 最近一次推送时间
|
||||
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 更新时间
|
||||
}
|
||||
|
||||
type SeoPushTaskList []*SeoPushTask
|
||||
|
||||
// 推送动作
|
||||
const (
|
||||
ActionUpdated = "updated" // 发布或更新,对应 Google URL_UPDATED
|
||||
ActionDeleted = "deleted" // 删除,对应 Google URL_DELETED
|
||||
)
|
||||
|
||||
// 推送状态
|
||||
const (
|
||||
PushStatusPending = int64(0) // 待推送
|
||||
PushStatusSuccess = int64(1) // 推送成功
|
||||
)
|
||||
|
||||
// MaxPushRetry 单个通道最大重试次数,达到上限后不再被补推任务捞取
|
||||
const MaxPushRetry = int64(5)
|
||||
Reference in New Issue
Block a user