压缩历史至 95081aff

This commit is contained in:
ryan
2026-06-08 20:34:27 +08:00
commit 8a782525de
435 changed files with 71146 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
/*
Copyright 2025-2026 linux.do
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package worker
import (
"context"
"time"
"github.com/linux-do/credit/internal/logger"
"go.opentelemetry.io/otel/trace"
"github.com/hibiken/asynq"
"github.com/linux-do/credit/internal/otel_trace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
// taskLoggingMiddleware 记录任务日志中间件
func taskLoggingMiddleware(h asynq.Handler) asynq.Handler {
return asynq.HandlerFunc(func(ctx context.Context, t *asynq.Task) error {
// 初始化 Trace
ctx, span := otel_trace.Start(ctx, "TaskProcess_"+t.Type(), trace.WithSpanKind(trace.SpanKindConsumer))
defer span.End()
// 添加任务信息到 Span
span.SetAttributes(
attribute.String("task.type", t.Type()),
attribute.Int("task.payload_size", len(t.Payload())),
attribute.String("task.id", t.ResultWriter().TaskID()),
)
// 开始计时
start := time.Now()
// 处理任务
err := h.ProcessTask(ctx, t)
// 计算耗时
latency := time.Since(start)
if err != nil {
// 处理出错,记录错误日志
logger.ErrorF(
ctx,
"[TaskMiddleware] 任务处理失败 Type: %s\nStartTime: %s\nLatency: %d ms\nError: %v",
t.Type(),
start.Format(time.RFC3339),
latency.Milliseconds(),
err,
)
// 设置 Span 错误状态
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return err
}
// 处理成功,记录成功日志
logger.InfoF(
ctx,
"[TaskMiddleware] 任务处理完成 Type: %s\nStartTime: %s\nEndTime: %s\nLatency: %d ms",
t.Type(),
start.Format(time.RFC3339),
time.Now().Format(time.RFC3339),
latency.Milliseconds(),
)
return nil
})
}
+113
View File
@@ -0,0 +1,113 @@
/*
Copyright 2025 linux.do
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package worker
import (
"log"
"math/rand"
"strings"
"time"
"github.com/hibiken/asynq"
"github.com/linux-do/credit/internal/apps/dispute"
"github.com/linux-do/credit/internal/apps/order"
"github.com/linux-do/credit/internal/apps/payment"
"github.com/linux-do/credit/internal/apps/redenvelope"
"github.com/linux-do/credit/internal/apps/upload"
"github.com/linux-do/credit/internal/apps/user"
"github.com/linux-do/credit/internal/config"
"github.com/linux-do/credit/internal/task"
)
// StartWorker 启动任务处理服务器
func StartWorker() error {
asynqServer := asynq.NewServer(
task.RedisOpt,
asynq.Config{
Concurrency: config.Config.Worker.Concurrency,
ShutdownTimeout: 3 * time.Minute,
Queues: buildQueuesFromConfig(),
StrictPriority: config.Config.Worker.StrictPriority,
RetryDelayFunc: func(n int, err error, t *asynq.Task) time.Duration {
log.Printf("[RetryDelayFunc] 任务类型: %s, 重试次数: %d, 错误: %v", t.Type(), n, err)
// 针对积分更新任务使用更长的重试间隔 + 随机抖动
if t.Type() == task.UpdateSingleUserGamificationScoreTask {
var baseDelay time.Duration
if err != nil && strings.Contains(err.Error(), "429") {
baseDelay = 120 * time.Second
} else {
baseDelay = 60 * time.Second
}
// 指数退避:n=0 时 delay=baseDelay,之后翻倍
delay := baseDelay * time.Duration(1<<uint(n))
if delay > 30*time.Minute {
delay = 30 * time.Minute
}
// delay ~ 2*delay
if delay > 0 {
jitter := time.Duration(rand.Int63n(int64(delay)))
delay += jitter
}
return delay
}
return asynq.DefaultRetryDelayFunc(n, err, t)
},
},
)
// 注册任务处理器
mux := asynq.NewServeMux()
mux.Use(taskLoggingMiddleware)
mux.HandleFunc(task.UpdateUserGamificationScoresTask, user.HandleUpdateUserGamificationScores)
mux.HandleFunc(task.UpdateSingleUserGamificationScoreTask, user.HandleUpdateSingleUserGamificationScore)
mux.HandleFunc(task.AutoRefundExpiredDisputesTask, dispute.HandleAutoRefundExpiredDisputes)
mux.HandleFunc(task.AutoRefundSingleDisputeTask, dispute.HandleAutoRefundSingleDispute)
mux.HandleFunc(task.MerchantPaymentNotifyTask, payment.HandleMerchantPaymentNotify)
mux.HandleFunc(task.SyncOrdersToClickHouseTask, order.HandleSyncOrdersToClickHouse)
mux.HandleFunc(task.RefundExpiredRedEnvelopesTask, redenvelope.HandleRefundExpiredRedEnvelopes)
mux.HandleFunc(task.CleanupUnusedUploadsTask, upload.HandleCleanupUnusedUploads)
mux.HandleFunc(task.SettlePendingPaymentsTask, order.HandleSettlePendingPayments)
// 启动服务器
return asynqServer.Run(mux)
}
// buildQueuesFromConfig 从配置构建队列映射
func buildQueuesFromConfig() map[string]int {
queues := make(map[string]int)
// 从配置读取队列
if len(config.Config.Worker.Queues) > 0 {
for _, q := range config.Config.Worker.Queues {
if q.Name != "" && q.Priority > 0 {
queues[q.Name] = q.Priority
}
}
}
// 如果配置为空,使用默认队列
if len(queues) == 0 {
queues = map[string]int{
task.QueueWebhook: 10,
task.QueueWhitelistOnly: 5,
task.QueueDefault: 1,
}
}
return queues
}