feat: 完善IM后台Kafka与组织架构管理
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/imtenant"
|
||||
"hotgo/internal/library/queue"
|
||||
"time"
|
||||
|
||||
@@ -60,7 +61,7 @@ func PublishAdminCommand(ctx context.Context, in AdminCommandInput) (res *AdminC
|
||||
return nil, gerror.Wrap(err, "序列化IM命令Envelope失败")
|
||||
}
|
||||
|
||||
db := g.DB()
|
||||
db := adminDB()
|
||||
err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
nowTime := gtime.Now()
|
||||
if _, err := tx.Exec(`INSERT INTO im_admin_commands
|
||||
@@ -101,7 +102,7 @@ func PublishAdminCommand(ctx context.Context, in AdminCommandInput) (res *AdminC
|
||||
}
|
||||
|
||||
func DispatchOutboxByEvent(ctx context.Context, eventID string) error {
|
||||
row, err := g.DB().GetOne(ctx, `SELECT * FROM im_admin_outbox WHERE event_id=? AND status=? LIMIT 1`,
|
||||
row, err := adminDB().GetOne(ctx, `SELECT * FROM im_admin_outbox WHERE event_id=? AND status=? LIMIT 1`,
|
||||
eventID, consts.ImAdminOutboxStatusPending)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "读取IM命令Outbox失败")
|
||||
@@ -146,7 +147,7 @@ func PublishConfigEvent(ctx context.Context, in ConfigEventInput) error {
|
||||
return gerror.Wrap(err, "序列化IM配置事件Envelope失败")
|
||||
}
|
||||
nowTime := gtime.Now()
|
||||
_, err = g.DB().Exec(ctx, `INSERT INTO im_admin_outbox
|
||||
_, err = adminDB().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,
|
||||
@@ -198,7 +199,7 @@ func PublishEvent(ctx context.Context, in EventInput) (eventID string, err error
|
||||
return "", gerror.Wrap(err, "序列化IM Kafka事件Envelope失败")
|
||||
}
|
||||
nowTime := gtime.Now()
|
||||
_, err = g.DB().Exec(ctx, `INSERT INTO im_admin_outbox
|
||||
_, err = adminDB().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,
|
||||
@@ -216,7 +217,7 @@ func DispatchPendingOutbox(ctx context.Context, limit int) (sent int, err error)
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
rows, err := g.DB().GetAll(ctx, `SELECT * FROM im_admin_outbox
|
||||
rows, err := adminDB().GetAll(ctx, `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)
|
||||
if err != nil {
|
||||
@@ -250,14 +251,14 @@ func dispatchOutboxRecord(ctx context.Context, row gdb.Record) error {
|
||||
return err
|
||||
}
|
||||
now := gtime.Now()
|
||||
_, err := g.DB().Exec(ctx, `UPDATE im_admin_outbox
|
||||
_, err := adminDB().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().Exec(ctx, `UPDATE im_admin_commands
|
||||
_, err = adminDB().Exec(ctx, `UPDATE im_admin_commands
|
||||
SET status=?,sent_at=?,updated_at=? WHERE command_id=?`,
|
||||
consts.ImAdminCommandStatusRunning, now, now, commandID)
|
||||
if err != nil {
|
||||
@@ -269,7 +270,7 @@ 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().Exec(ctx, `UPDATE im_admin_outbox
|
||||
_, err := adminDB().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 {
|
||||
@@ -316,9 +317,13 @@ func ApplyCommandResult(ctx context.Context, result CommandResult) error {
|
||||
}
|
||||
sql += ` WHERE ` + whereColumn + `=?`
|
||||
args = append(args, whereValue)
|
||||
_, err := g.DB().Exec(ctx, sql, args...)
|
||||
_, err := adminDB().Exec(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "更新IM后台命令结果失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func adminDB() gdb.DB {
|
||||
return imtenant.RegistryDB()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user