refactor: extract magic numbers to named constants for mnd lint compliance

This commit is contained in:
ryan
2026-06-09 13:44:29 +08:00
parent b05d26c9c6
commit e06f76436e
24 changed files with 650 additions and 440 deletions
+10 -2
View File
@@ -15,13 +15,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package task 定义异步任务类型与调度常量
package task
// 异步任务类型标识
const (
CleanupUnusedUploadsTask = "upload:cleanup_unused"
SendEmailTask = "mail:send"
)
// 任务队列名称
const (
QueueDefault = "default"
)
@@ -32,7 +35,11 @@ const (
TaskTypeSendEmail = "send_email"
)
// defaultMaxRetry 任务默认最大重试次数
const defaultMaxRetry = 3
// TaskParam 任务参数定义
//nolint:revive // TaskParam 保留完整名称以避免与通用 Param 混淆
type TaskParam struct {
Name string `json:"Name"` // 参数键名
Label string `json:"Label"` // 显示名称
@@ -43,6 +50,7 @@ type TaskParam struct {
}
// TaskMeta 任务元数据
//nolint:revive // TaskMeta 保留完整名称以避免与通用 Meta 混淆
type TaskMeta struct {
Type string
AsynqTask string
@@ -63,7 +71,7 @@ var DispatchableTasks = []TaskMeta{
Name: "清理未使用上传",
Description: "清理超过1小时未使用的上传文件",
SupportsTime: false,
MaxRetry: 3,
MaxRetry: defaultMaxRetry,
Queue: QueueDefault,
Retryable: true,
},
@@ -73,7 +81,7 @@ var DispatchableTasks = []TaskMeta{
Name: "发送邮件",
Description: "异步发送系统邮件",
SupportsTime: false,
MaxRetry: 3,
MaxRetry: defaultMaxRetry,
Queue: QueueDefault,
Retryable: true,
Params: []TaskParam{
+7 -2
View File
@@ -28,6 +28,11 @@ import (
"github.com/hibiken/asynq"
)
const (
cleanupDedupWindow = 23 * time.Hour // 清理任务去重窗口
cleanupMaxRetry = 3 // 清理任务最大重试次数
)
var (
scheduler *asynq.Scheduler
schedulerOnce sync.Once
@@ -62,8 +67,8 @@ func StartScheduler() error {
if _, err = scheduler.Register(
config.Config.Scheduler.CleanupUnusedUploadsTaskCron,
asynq.NewTask(task.CleanupUnusedUploadsTask, nil),
asynq.Unique(23*time.Hour),
asynq.MaxRetry(3),
asynq.Unique(cleanupDedupWindow),
asynq.MaxRetry(cleanupMaxRetry),
); err != nil {
return
}
+4 -1
View File
@@ -26,6 +26,9 @@ import (
"github.com/hibiken/asynq"
)
// workerShutdownTimeout Worker 优雅关闭超时时间
const workerShutdownTimeout = 3 * time.Minute
func init() {
// 注册所有任务处理器
taskhandlers.Register()
@@ -37,7 +40,7 @@ func StartWorker() error {
task.RedisOpt,
asynq.Config{
Concurrency: config.Config.Worker.Concurrency,
ShutdownTimeout: 3 * time.Minute,
ShutdownTimeout: workerShutdownTimeout,
Queues: buildQueuesFromConfig(),
StrictPriority: config.Config.Worker.StrictPriority,
},