From 74b3640913bbbba4c1f86475a3ffbc9875c5e2ce Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Tue, 23 Jul 2024 14:11:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=81=E8=AE=B8=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=A8=BF=E4=BB=B6=E5=8F=91=E5=B8=83=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=9A=84hash=E5=90=8D=E7=A7=B0=E5=89=8D=E7=BC=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/config/config.go | 1 + backend/mq/redis.go | 10 +++++----- backend/service/routine/confirm_posted.go | 2 +- main.go | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/backend/config/config.go b/backend/config/config.go index 7e4c192..02b5e17 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -37,6 +37,7 @@ func SetDefault() { viper.SetDefault("mq.redis.stream.publish_post", "campux_publish_post") viper.SetDefault("mq.redis.stream.new_post", "campux_new_post") viper.SetDefault("mq.redis.stream.post_cancel", "campux_post_cancel") + viper.SetDefault("mq.redis.hash.post_publish_status", "campux_post_publish_status") } diff --git a/backend/mq/redis.go b/backend/mq/redis.go index 2f1a5ca..a5f2929 100644 --- a/backend/mq/redis.go +++ b/backend/mq/redis.go @@ -47,7 +47,7 @@ func (r *RedisStreamMQ) PublishPost(postID int) error { return err } - // 创建散列表跟踪发布状态 HSET publish_post_status:post_id campuxbot_1234567 0 campuxbot_1234568 0 + // 创建散列表跟踪发布状态 HSET {{ viper.GetString("mq.redis.hash.post_publish_status") }}post_id campuxbot_1234567 0 campuxbot_1234568 0 var bots []int64 err = viper.UnmarshalKey("service.bots", &bots) @@ -57,7 +57,7 @@ func (r *RedisStreamMQ) PublishPost(postID int) error { } for _, bot := range bots { - err = r.Client.HSet(context.Background(), "publish_post_status:"+strconv.Itoa(postID), "campuxbot_"+strconv.FormatInt(bot, 10), 0).Err() + err = r.Client.HSet(context.Background(), viper.GetString("mq.redis.hash.post_publish_status")+strconv.Itoa(postID), "campuxbot_"+strconv.FormatInt(bot, 10), 0).Err() if err != nil { return err @@ -88,8 +88,8 @@ func (r *RedisStreamMQ) PostCancel(postID int) error { } func (r *RedisStreamMQ) CheckPostPublishStatus(postID int) (bool, error) { - // HGETALL publish_post_status:77 - status, err := r.Client.HGetAll(context.Background(), "publish_post_status:"+strconv.Itoa(postID)).Result() + // HGETALL {{ viper.GetString("mq.redis.hash.post_publish_status") }}77 + status, err := r.Client.HGetAll(context.Background(), viper.GetString("mq.redis.hash.post_publish_status")+strconv.Itoa(postID)).Result() if err != nil { return false, err @@ -106,6 +106,6 @@ func (r *RedisStreamMQ) CheckPostPublishStatus(postID int) (bool, error) { // 删除稿件发布跟踪hash表 func (r *RedisStreamMQ) DeletePostPublishStatus(postID int) error { - _, err := r.Client.Del(context.Background(), "publish_post_status:"+strconv.Itoa(postID)).Result() + _, err := r.Client.Del(context.Background(), viper.GetString("mq.redis.hash.post_publish_status")+strconv.Itoa(postID)).Result() return err } diff --git a/backend/service/routine/confirm_posted.go b/backend/service/routine/confirm_posted.go index a28bab7..772ed5b 100644 --- a/backend/service/routine/confirm_posted.go +++ b/backend/service/routine/confirm_posted.go @@ -10,7 +10,7 @@ import ( func ConfirmPosted(db database.MongoDBManager, msq mq.RedisStreamMQ) { // 取出状态为“队列中”的稿件 - // 检查消息队列中HGETALL publish_post_status:post_id 的所有值是否都是1 + // 检查消息队列中HGETALL {{ viper.GetString("mq.redis.hash.post_publish_status") }}post_id 的所有值是否都是1 // 如果是, 则更新稿件状态为“已发布” inQueuePosts, _, err := db.GetPosts(-1, database.POST_STATUS_IN_QUEUE, 1, 1, 100) if err != nil { diff --git a/main.go b/main.go index 9c5364d..a7a4f51 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ func main() { } if created { - panic("请修改配置文件") + panic("请修改配置文件后重启") } // 启动服务