mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-04 07:06:36 +08:00
压缩历史至 95081aff
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
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 task
|
||||
|
||||
const (
|
||||
UpdateUserGamificationScoresTask = "user:gamification:update_scores_task"
|
||||
UpdateSingleUserGamificationScoreTask = "user:gamification:update_single_score_task"
|
||||
AutoRefundExpiredDisputesTask = "dispute:auto_refund_expired"
|
||||
AutoRefundSingleDisputeTask = "dispute:auto_refund_single"
|
||||
MerchantPaymentNotifyTask = "payment:merchant_notify"
|
||||
SyncOrdersToClickHouseTask = "order:sync_to_clickhouse"
|
||||
RefundExpiredRedEnvelopesTask = "redenvelope:refund_expired"
|
||||
CleanupUnusedUploadsTask = "upload:cleanup_unused"
|
||||
SettlePendingPaymentsTask = "order:settle_pending_payments"
|
||||
)
|
||||
|
||||
const (
|
||||
QueueWhitelistOnly = "whitelist_only"
|
||||
QueueWebhook = "webhook"
|
||||
QueueDefault = "default"
|
||||
)
|
||||
|
||||
// 管理员可下发的任务类型标识
|
||||
const (
|
||||
TaskTypeOrderSync = "order_sync"
|
||||
TaskTypeUserGamification = "user_gamification"
|
||||
TaskTypeDisputeRefund = "dispute_auto_refund"
|
||||
TaskTypeRedEnvelopeRefund = "redenvelope_auto_refund"
|
||||
TaskTypeCleanupUploads = "cleanup_unused_uploads"
|
||||
TaskTypeSettlePending = "settle_pending_payments"
|
||||
)
|
||||
|
||||
// TaskMeta 任务元数据
|
||||
type TaskMeta struct {
|
||||
Type string
|
||||
AsynqTask string
|
||||
Name string
|
||||
Description string
|
||||
SupportsTime bool
|
||||
MaxRetry int
|
||||
Queue string
|
||||
}
|
||||
|
||||
// DispatchableTasks 可下发的任务列表
|
||||
var DispatchableTasks = []TaskMeta{
|
||||
{
|
||||
Type: TaskTypeOrderSync,
|
||||
AsynqTask: SyncOrdersToClickHouseTask,
|
||||
Name: "订单同步",
|
||||
Description: "同步订单数据到 ClickHouse",
|
||||
SupportsTime: true,
|
||||
MaxRetry: 5,
|
||||
Queue: QueueDefault,
|
||||
},
|
||||
{
|
||||
Type: TaskTypeUserGamification,
|
||||
AsynqTask: UpdateSingleUserGamificationScoreTask,
|
||||
Name: "用户积分更新",
|
||||
Description: "更新用户的点数积分",
|
||||
SupportsTime: false,
|
||||
MaxRetry: 5,
|
||||
Queue: QueueWhitelistOnly,
|
||||
},
|
||||
{
|
||||
Type: TaskTypeDisputeRefund,
|
||||
AsynqTask: AutoRefundExpiredDisputesTask,
|
||||
Name: "争议自动退款",
|
||||
Description: "处理过期争议的自动退款",
|
||||
SupportsTime: false,
|
||||
MaxRetry: 5,
|
||||
Queue: QueueDefault,
|
||||
},
|
||||
{
|
||||
Type: TaskTypeRedEnvelopeRefund,
|
||||
AsynqTask: RefundExpiredRedEnvelopesTask,
|
||||
Name: "红包自动退款",
|
||||
Description: "处理过期红包的自动退款",
|
||||
SupportsTime: false,
|
||||
MaxRetry: 5,
|
||||
Queue: QueueDefault,
|
||||
},
|
||||
{
|
||||
Type: TaskTypeCleanupUploads,
|
||||
AsynqTask: CleanupUnusedUploadsTask,
|
||||
Name: "清理未使用上传",
|
||||
Description: "清理超过1小时未使用的上传文件",
|
||||
SupportsTime: false,
|
||||
MaxRetry: 3,
|
||||
Queue: QueueDefault,
|
||||
},
|
||||
{
|
||||
Type: TaskTypeSettlePending,
|
||||
AsynqTask: SettlePendingPaymentsTask,
|
||||
Name: "延迟到账结算",
|
||||
Description: "结算到期的延迟到账订单,将在途资金转入可用余额",
|
||||
SupportsTime: false,
|
||||
MaxRetry: 5,
|
||||
Queue: QueueDefault,
|
||||
},
|
||||
}
|
||||
|
||||
// GetTaskMeta 根据任务类型获取元数据
|
||||
func GetTaskMeta(taskType string) *TaskMeta {
|
||||
for _, t := range DispatchableTasks {
|
||||
if t.Type == taskType {
|
||||
return &t
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
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 scheduler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/linux-do/credit/internal/config"
|
||||
"github.com/linux-do/credit/internal/task"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
var (
|
||||
AsynqClient *asynq.Client
|
||||
scheduler *asynq.Scheduler
|
||||
schedulerOnce sync.Once
|
||||
)
|
||||
|
||||
func init() {
|
||||
AsynqClient = asynq.NewClient(task.RedisOpt)
|
||||
}
|
||||
|
||||
// StartScheduler 启动调度器
|
||||
func StartScheduler() error {
|
||||
var err error
|
||||
schedulerOnce.Do(func() {
|
||||
location, locErr := time.LoadLocation("Asia/Shanghai")
|
||||
if locErr != nil {
|
||||
err = fmt.Errorf("failed to load location: %w", locErr)
|
||||
return
|
||||
}
|
||||
scheduler = asynq.NewScheduler(
|
||||
task.RedisOpt,
|
||||
&asynq.SchedulerOpts{
|
||||
Location: location,
|
||||
},
|
||||
)
|
||||
|
||||
// 用户积分更新任务
|
||||
if _, err = scheduler.Register(
|
||||
config.Config.Scheduler.UpdateUserGamificationScoresTaskCron,
|
||||
asynq.NewTask(task.UpdateUserGamificationScoresTask, nil),
|
||||
asynq.Queue(task.QueueWhitelistOnly),
|
||||
asynq.MaxRetry(5),
|
||||
asynq.Unique(23*time.Hour),
|
||||
); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 争议自动退款任务
|
||||
if _, err = scheduler.Register(
|
||||
config.Config.Scheduler.AutoRefundExpiredDisputesTaskCron,
|
||||
asynq.NewTask(task.AutoRefundExpiredDisputesTask, nil),
|
||||
asynq.MaxRetry(5),
|
||||
asynq.Unique(23*time.Hour),
|
||||
); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 订单同步任务
|
||||
if _, err = scheduler.Register(
|
||||
config.Config.Scheduler.SyncOrdersToClickHouseTaskCron,
|
||||
asynq.NewTask(task.SyncOrdersToClickHouseTask, nil),
|
||||
asynq.MaxRetry(10),
|
||||
asynq.Unique(23*time.Hour),
|
||||
); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 红包过期退款任务
|
||||
if _, err = scheduler.Register(
|
||||
config.Config.Scheduler.RefundExpiredRedEnvelopesTaskCron,
|
||||
asynq.NewTask(task.RefundExpiredRedEnvelopesTask, nil),
|
||||
asynq.Unique(23*time.Hour),
|
||||
); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 清理未使用的上传文件任务
|
||||
if _, err = scheduler.Register(
|
||||
config.Config.Scheduler.CleanupUnusedUploadsTaskCron,
|
||||
asynq.NewTask(task.CleanupUnusedUploadsTask, nil),
|
||||
asynq.Unique(23*time.Hour),
|
||||
asynq.MaxRetry(3),
|
||||
); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 延迟到账结算任务
|
||||
if _, err = scheduler.Register(
|
||||
config.Config.Scheduler.SettlePendingPaymentsTaskCron,
|
||||
asynq.NewTask(task.SettlePendingPaymentsTask, nil),
|
||||
asynq.Unique(55*time.Minute),
|
||||
asynq.MaxRetry(3),
|
||||
); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 启动调度器
|
||||
err = scheduler.Run()
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
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 task
|
||||
|
||||
import (
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/linux-do/credit/internal/config"
|
||||
)
|
||||
|
||||
// RedisOpt asynq Redis 连接配置(兼容 Standalone/Sentinel/Cluster)
|
||||
var RedisOpt asynq.RedisConnOpt
|
||||
|
||||
func init() {
|
||||
RedisOpt = NewRedisConnOpt()
|
||||
}
|
||||
|
||||
// NewRedisConnOpt 根据配置返回对应的 asynq Redis 连接选项
|
||||
func NewRedisConnOpt() asynq.RedisConnOpt {
|
||||
cfg := config.Config.Redis
|
||||
addrs := cfg.Addrs
|
||||
|
||||
if cfg.ClusterMode {
|
||||
return asynq.RedisClusterClientOpt{
|
||||
Addrs: addrs,
|
||||
Username: cfg.Username,
|
||||
Password: cfg.Password,
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.MasterName != "" {
|
||||
return asynq.RedisFailoverClientOpt{
|
||||
MasterName: cfg.MasterName,
|
||||
SentinelAddrs: addrs,
|
||||
Username: cfg.Username,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
}
|
||||
}
|
||||
|
||||
addr := "localhost:6379"
|
||||
if len(addrs) > 0 {
|
||||
addr = addrs[0]
|
||||
}
|
||||
return asynq.RedisClientOpt{
|
||||
Addr: addr,
|
||||
Username: cfg.Username,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
PoolSize: cfg.PoolSize,
|
||||
}
|
||||
}
|
||||
|
||||
// PrefixedQueue 返回带前缀的队列名,用于 Cluster 模式隔离
|
||||
func PrefixedQueue(queue string) string {
|
||||
prefix := config.Config.Redis.KeyPrefix
|
||||
if prefix == "" {
|
||||
return queue
|
||||
}
|
||||
return prefix + queue
|
||||
}
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user