feat: B7&B8 Add scheduled tasks and a portion of notification services#34
Merged
feat: B7&B8 Add scheduled tasks and a portion of notification services#34
Conversation
|
好的,我现在需要仔细审查用户提供的代码,并按照他们的要求进行分析。首先,我得理解用户的需求,他们希望我作为一位资深的软件工程师和代码审查专家,全面审查代码,重点关注代码质量、可靠性、性能和安全性。 首先,我看看用户提供的代码变化。这些变化包括新增了一些文件,比如notification handler、scheduler service,还有对main.go、models/task.go等文件的修改。看起来主要是新增了一个定时任务通知的功能,用于在任务截止日期临近时发送通知。 接下来,我逐一审查各个部分:
接下来,我需要针对这些问题提出具体的建议,并给出修改示例。例如,修改命名、添加注释、改进错误处理、增加超时设置、优化数据库查询等。 最后,综合评分,考虑到代码整体功能完善,但存在一些结构和安全问题,给予75分。 现在,我需要将这些思考整理成一个结构化的审查报告,按照用户的要求,每个部分都要具体,提供可操作的建议和代码示例。 代码审查报告1. 总体分析新增的定时任务通知功能模块整体设计合理,实现了任务截止提醒的核心功能。代码结构清晰,新增了服务层和处理器,符合分层设计原则。主要优点包括模块化设计和清晰的职责划分。但存在一些命名不一致、注释缺失和错误处理不完善的问题,需要改进以提高代码质量和可维护性。 2. 代码质量与可维护性
修改示例: // models/task.go
const (
TaskStatusTodo TaskStatus = 1 // 待办
TaskStatusInProgress TaskStatus = 2 // 进行中
TaskStatusCompleted TaskStatus = 3 // 已完成
TaskStatusCancelled TaskStatus = 4 // 已取消
TaskStatusArchived TaskStatus = 5 // 已归档
)3. 可靠性与健壮性
修改示例: // services/scheduler_service.go
func (s *Scheduler) sendNotification(task models.Task) error {
// 重试逻辑
maxRetries := 3
for i := 0; i < maxRetries; i++ {
if err := sendAttempt(task); err != nil {
if i == maxRetries-1 {
return fmt.Errorf("通知发送失败,已重试%d次: %v", maxRetries, err)
}
time.Sleep(time.Duration(i+1) * time.Second)
continue
}
break
}
return nil
}4. 性能优化
修改示例: // services/scheduler_service.go
func (s *Scheduler) queryPendingTasks() ([]models.Task, error) {
var tasks []models.Task
now := time.Now().UTC()
deadlineEnd := now.Add(24 * time.Hour)
err := s.db.Where(
"status NOT IN (?) AND due_date > ? AND due_date < ? AND deadline_alert_sent = 0",
[]int{3, 5}, now, deadlineEnd,
).Find(&tasks).Error
return tasks, err
}5. 安全性
修改示例: // handlers/notification/notification.go
func (h *NotificationHandler) ReceiveTaskNotification(c *gin.Context) {
var req TaskNotificationReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "无效的通知参数",
})
return
}
// 添加权限验证
token, err := c.Request.Cookie("Authorization")
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"code": 401,
"message": "未授权",
})
return
}
// 解析JWT并验证
// (此处省略具体实现)
// if !isValidToken(token.Value) {
// c.JSON(http.StatusUnauthorized, gin.H{
// "code": 401,
// "message": "无效的令牌",
// })
// return
// }
h.printNotification(req)
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "通知接收成功",
})
}综合质量评分: 75/100代码整体功能完善,但需在命名、注释、错误处理和安全性方面进一步改进,以提高代码质量和系统的健壮性。 |
WiDayn
approved these changes
Nov 27, 2025
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a scheduled task that runs once every hour. Each time it runs, it scans the database task table to find all tasks whose deadlines are within the next 24 hours and that are neither completed nor archived. The scanned tasks are then notified through the notification service. After the notification, these tasks are marked as archived. The scheduled task will no longer notify this service when it runs next time.