-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mq | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/redis/go-redis/v9" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type RedisStreamMQ struct { | ||
Client *redis.Client | ||
PublishPostStream string | ||
} | ||
|
||
func NewRedisStreamMQ() *RedisStreamMQ { | ||
client := redis.NewClient(&redis.Options{ | ||
Addr: viper.GetString("mq.redis.addr"), | ||
Password: viper.GetString("mq.redis.password"), | ||
DB: viper.GetInt("mq.redis.db"), | ||
}) | ||
return &RedisStreamMQ{ | ||
Client: client, | ||
PublishPostStream: viper.GetString("mq.redis.stream.publish_post"), | ||
} | ||
} | ||
|
||
func (r *RedisStreamMQ) PublishPost(postID int) error { | ||
_, err := r.Client.XAdd(context.Background(), &redis.XAddArgs{ | ||
Stream: r.PublishPostStream, | ||
Values: map[string]interface{}{ | ||
"post_id": postID, | ||
}, | ||
}).Result() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package routine | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/RockChinQ/Campux/backend/database" | ||
"github.com/RockChinQ/Campux/backend/mq" | ||
"github.com/RockChinQ/Campux/backend/util" | ||
) | ||
|
||
func SchedulePublishing(db database.MongoDBManager, msq mq.RedisStreamMQ) { | ||
// 从数据库中查询出所有待发布的稿件 | ||
// 遍历稿件, 发布到消息队列 | ||
// 更新稿件状态 | ||
approvedPosts, err := db.GetPosts(-1, database.POST_STATUS_APPROVED, 1, 1, 10) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, post := range approvedPosts { | ||
err = msq.PublishPost(post.ID) | ||
if err != nil { | ||
fmt.Println(err) | ||
continue | ||
} | ||
|
||
// 加日志 | ||
err = db.AddPostLog(&database.PostLogPO{ | ||
PostID: post.ID, | ||
Op: -1, | ||
OldStat: database.POST_STATUS_APPROVED, | ||
NewStat: database.POST_STATUS_IN_QUEUE, | ||
Comment: "发布到消息队列", | ||
CreatedAt: util.GetCSTTime(), | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
continue | ||
} | ||
|
||
err = db.UpdatePostStatus(post.ID, database.POST_STATUS_IN_QUEUE) | ||
if err != nil { | ||
fmt.Println(err) | ||
continue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters