fix: use unprefixed im kafka tables
This commit is contained in:
@@ -67,39 +67,20 @@ func PublishAdminCommand(ctx context.Context, in AdminCommandInput) (res *AdminC
|
||||
|
||||
db := g.DB()
|
||||
err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
data := g.Map{
|
||||
"command_id": commandID,
|
||||
"tenant_id": in.TenantID,
|
||||
"tenant_code": in.TenantCode,
|
||||
"event_id": eventID,
|
||||
"event_type": in.EventType,
|
||||
"biz_type": in.BizType,
|
||||
"biz_id": in.BizID,
|
||||
"idempotency_key": in.IdempotencyKey,
|
||||
"status": consts.ImAdminCommandStatusPending,
|
||||
"payload": string(payloadBytes),
|
||||
"envelope": string(envelopeBytes),
|
||||
"created_by": in.CreatedBy,
|
||||
"created_at": gtime.Now(),
|
||||
"updated_at": gtime.Now(),
|
||||
}
|
||||
if _, err := tx.Model(adminCommandTable).Safe().Ctx(ctx).Data(data).Insert(); err != nil {
|
||||
nowTime := gtime.Now()
|
||||
if _, err := tx.Exec(`INSERT INTO im_admin_commands
|
||||
(command_id,tenant_id,tenant_code,event_id,event_type,biz_type,biz_id,idempotency_key,status,payload,envelope,created_by,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,CAST(? AS JSON),CAST(? AS JSON),?,?,?)`,
|
||||
commandID, in.TenantID, in.TenantCode, eventID, in.EventType, in.BizType, in.BizID,
|
||||
in.IdempotencyKey, consts.ImAdminCommandStatusPending, string(payloadBytes), string(envelopeBytes),
|
||||
in.CreatedBy, nowTime, nowTime); err != nil {
|
||||
return gerror.Wrap(err, "创建IM后台命令失败")
|
||||
}
|
||||
outbox := g.Map{
|
||||
"event_id": eventID,
|
||||
"command_id": commandID,
|
||||
"topic": consts.QueueImAdminCommandTopic,
|
||||
"event_type": in.EventType,
|
||||
"biz_type": in.BizType,
|
||||
"biz_id": in.BizID,
|
||||
"key_value": in.BizID,
|
||||
"envelope": string(envelopeBytes),
|
||||
"status": consts.ImAdminOutboxStatusPending,
|
||||
"created_at": gtime.Now(),
|
||||
"updated_at": gtime.Now(),
|
||||
}
|
||||
if _, err := tx.Model(adminOutboxTable).Safe().Ctx(ctx).Data(outbox).Insert(); err != nil {
|
||||
if _, err := tx.Exec(`INSERT INTO im_admin_outbox
|
||||
(event_id,command_id,topic,event_type,biz_type,biz_id,key_value,envelope,status,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,CAST(? AS JSON),?,?,?)`,
|
||||
eventID, commandID, consts.QueueImAdminCommandTopic, in.EventType, in.BizType, in.BizID,
|
||||
in.BizID, string(envelopeBytes), consts.ImAdminOutboxStatusPending, nowTime, nowTime); err != nil {
|
||||
return gerror.Wrap(err, "创建IM后台命令Outbox失败")
|
||||
}
|
||||
return nil
|
||||
@@ -126,10 +107,8 @@ func PublishAdminCommand(ctx context.Context, in AdminCommandInput) (res *AdminC
|
||||
|
||||
func DispatchOutboxByEvent(ctx context.Context, eventID string) error {
|
||||
var row gdb.Record
|
||||
if err := g.DB().Model(adminOutboxTable).Safe().Ctx(ctx).
|
||||
Where("event_id", eventID).
|
||||
Where("status", consts.ImAdminOutboxStatusPending).
|
||||
Scan(&row); err != nil {
|
||||
if err := g.DB().Raw(`SELECT * FROM im_admin_outbox WHERE event_id=? AND status=? LIMIT 1`,
|
||||
eventID, consts.ImAdminOutboxStatusPending).Scan(&row); err != nil {
|
||||
return gerror.Wrap(err, "读取IM命令Outbox失败")
|
||||
}
|
||||
if row.IsEmpty() {
|
||||
@@ -171,19 +150,12 @@ func PublishConfigEvent(ctx context.Context, in ConfigEventInput) error {
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "序列化IM配置事件Envelope失败")
|
||||
}
|
||||
_, err = g.DB().Model(adminOutboxTable).Safe().Ctx(ctx).Data(g.Map{
|
||||
"event_id": eventID,
|
||||
"command_id": "",
|
||||
"topic": consts.QueueImConfigEventTopic,
|
||||
"event_type": in.EventType,
|
||||
"biz_type": "config",
|
||||
"biz_id": in.BizID,
|
||||
"key_value": in.TenantCode,
|
||||
"envelope": string(envelopeBytes),
|
||||
"status": consts.ImAdminOutboxStatusPending,
|
||||
"created_at": gtime.Now(),
|
||||
"updated_at": gtime.Now(),
|
||||
}).Insert()
|
||||
nowTime := gtime.Now()
|
||||
_, err = g.DB().Exec(ctx, `INSERT INTO im_admin_outbox
|
||||
(event_id,command_id,topic,event_type,biz_type,biz_id,key_value,envelope,status,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,CAST(? AS JSON),?,?,?)`,
|
||||
eventID, "", consts.QueueImConfigEventTopic, in.EventType, "config", in.BizID, in.TenantCode,
|
||||
string(envelopeBytes), consts.ImAdminOutboxStatusPending, nowTime, nowTime)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "创建IM配置事件Outbox失败")
|
||||
}
|
||||
@@ -230,19 +202,12 @@ func PublishEvent(ctx context.Context, in EventInput) (eventID string, err error
|
||||
if err != nil {
|
||||
return "", gerror.Wrap(err, "序列化IM Kafka事件Envelope失败")
|
||||
}
|
||||
_, err = g.DB().Model(adminOutboxTable).Safe().Ctx(ctx).Data(g.Map{
|
||||
"event_id": eventID,
|
||||
"command_id": "",
|
||||
"topic": in.Topic,
|
||||
"event_type": in.EventType,
|
||||
"biz_type": in.BizType,
|
||||
"biz_id": in.BizID,
|
||||
"key_value": in.BizID,
|
||||
"envelope": string(envelopeBytes),
|
||||
"status": consts.ImAdminOutboxStatusPending,
|
||||
"created_at": gtime.Now(),
|
||||
"updated_at": gtime.Now(),
|
||||
}).Insert()
|
||||
nowTime := gtime.Now()
|
||||
_, err = g.DB().Exec(ctx, `INSERT INTO im_admin_outbox
|
||||
(event_id,command_id,topic,event_type,biz_type,biz_id,key_value,envelope,status,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,CAST(? AS JSON),?,?,?)`,
|
||||
eventID, "", in.Topic, in.EventType, in.BizType, in.BizID, in.BizID,
|
||||
string(envelopeBytes), consts.ImAdminOutboxStatusPending, nowTime, nowTime)
|
||||
if err != nil {
|
||||
return "", gerror.Wrap(err, "创建IM Kafka事件Outbox失败")
|
||||
}
|
||||
@@ -257,12 +222,9 @@ func DispatchPendingOutbox(ctx context.Context, limit int) (sent int, err error)
|
||||
limit = 100
|
||||
}
|
||||
var rows []gdb.Record
|
||||
err = g.DB().Model(adminOutboxTable).Safe().Ctx(ctx).
|
||||
WhereIn("status", []string{consts.ImAdminOutboxStatusPending, consts.ImAdminOutboxStatusFailed}).
|
||||
Where("next_retry_at IS NULL OR next_retry_at <= NOW()").
|
||||
OrderAsc("id").
|
||||
Limit(limit).
|
||||
Scan(&rows)
|
||||
err = g.DB().Raw(`SELECT * FROM im_admin_outbox
|
||||
WHERE status IN (?,?) AND (next_retry_at IS NULL OR next_retry_at <= NOW())
|
||||
ORDER BY id ASC LIMIT ?`, consts.ImAdminOutboxStatusPending, consts.ImAdminOutboxStatusFailed, limit).Scan(&rows)
|
||||
if err != nil {
|
||||
return 0, gerror.Wrap(err, "读取待投递IM命令Outbox失败")
|
||||
}
|
||||
@@ -294,27 +256,16 @@ func dispatchOutboxRecord(ctx context.Context, row gdb.Record) error {
|
||||
return err
|
||||
}
|
||||
now := gtime.Now()
|
||||
_, err := g.DB().Model(adminOutboxTable).Safe().Ctx(ctx).
|
||||
Where("event_id", eventID).
|
||||
Data(g.Map{
|
||||
"status": consts.ImAdminOutboxStatusSent,
|
||||
"attempts": gdb.Raw("attempts + 1"),
|
||||
"sent_at": now,
|
||||
"last_reason": "",
|
||||
"last_error": "",
|
||||
"updated_at": now,
|
||||
}).Update()
|
||||
_, err := g.DB().Exec(ctx, `UPDATE im_admin_outbox
|
||||
SET status=?,attempts=attempts+1,sent_at=?,last_reason='',last_error='',updated_at=?
|
||||
WHERE event_id=?`, consts.ImAdminOutboxStatusSent, now, now, eventID)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "更新IM命令Outbox投递状态失败")
|
||||
}
|
||||
if commandID != "" {
|
||||
_, err = g.DB().Model(adminCommandTable).Safe().Ctx(ctx).
|
||||
Where("command_id", commandID).
|
||||
Data(g.Map{
|
||||
"status": consts.ImAdminCommandStatusRunning,
|
||||
"sent_at": now,
|
||||
"updated_at": now,
|
||||
}).Update()
|
||||
_, err = g.DB().Exec(ctx, `UPDATE im_admin_commands
|
||||
SET status=?,sent_at=?,updated_at=? WHERE command_id=?`,
|
||||
consts.ImAdminCommandStatusRunning, now, now, commandID)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "更新IM后台命令投递状态失败")
|
||||
}
|
||||
@@ -324,16 +275,9 @@ func dispatchOutboxRecord(ctx context.Context, row gdb.Record) error {
|
||||
|
||||
func markOutboxFailed(ctx context.Context, eventID string, reason string, message string) error {
|
||||
now := gtime.Now()
|
||||
_, err := g.DB().Model(adminOutboxTable).Safe().Ctx(ctx).
|
||||
Where("event_id", eventID).
|
||||
Data(g.Map{
|
||||
"status": consts.ImAdminOutboxStatusFailed,
|
||||
"attempts": gdb.Raw("attempts + 1"),
|
||||
"next_retry_at": now.Add(30 * time.Second),
|
||||
"last_reason": reason,
|
||||
"last_error": message,
|
||||
"updated_at": now,
|
||||
}).Update()
|
||||
_, err := g.DB().Exec(ctx, `UPDATE im_admin_outbox
|
||||
SET status=?,attempts=attempts+1,next_retry_at=?,last_reason=?,last_error=?,updated_at=?
|
||||
WHERE event_id=?`, consts.ImAdminOutboxStatusFailed, now.Add(30*time.Second), reason, message, now, eventID)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "更新IM命令Outbox失败状态失败")
|
||||
}
|
||||
@@ -364,13 +308,21 @@ func ApplyCommandResult(ctx context.Context, result CommandResult) error {
|
||||
if len(result.Result) > 0 {
|
||||
data["result"] = string(result.Result)
|
||||
}
|
||||
mod := g.DB().Model(adminCommandTable).Safe().Ctx(ctx)
|
||||
whereColumn := "event_id"
|
||||
whereValue := result.CommandEventID
|
||||
if result.CommandID != "" {
|
||||
mod = mod.Where("command_id", result.CommandID)
|
||||
} else {
|
||||
mod = mod.Where("event_id", result.CommandEventID)
|
||||
whereColumn = "command_id"
|
||||
whereValue = result.CommandID
|
||||
}
|
||||
_, err := mod.Data(data).Update()
|
||||
sql := `UPDATE im_admin_commands SET status=?,reason=?,message=?,completed_at=?,updated_at=?`
|
||||
args := []any{data["status"], data["reason"], data["message"], data["completed_at"], data["updated_at"]}
|
||||
if resultValue, ok := data["result"]; ok {
|
||||
sql += `,result=CAST(? AS JSON)`
|
||||
args = append(args, resultValue)
|
||||
}
|
||||
sql += ` WHERE ` + whereColumn + `=?`
|
||||
args = append(args, whereValue)
|
||||
_, err := g.DB().Exec(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "更新IM后台命令结果失败")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user