Skip to content

Commit

Permalink
feat: 允许自定义稿件发布状态的hash名称前缀 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Jul 23, 2024
1 parent 1fd3945 commit 74b3640
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}

Expand Down
10 changes: 5 additions & 5 deletions backend/mq/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion backend/service/routine/confirm_posted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
}

if created {
panic("请修改配置文件")
panic("请修改配置文件后重启")
}

// 启动服务
Expand Down

0 comments on commit 74b3640

Please sign in to comment.