123 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package asynq
 | ||
| 
 | ||
| import (
 | ||
| 	"context"
 | ||
| 
 | ||
| 	"github.com/hibiken/asynq"
 | ||
| 	"go.uber.org/zap"
 | ||
| 
 | ||
| 	"tyapi-server/internal/application/api"
 | ||
| 	"tyapi-server/internal/application/article"
 | ||
| 	finance_services "tyapi-server/internal/domains/finance/services"
 | ||
| 	product_services "tyapi-server/internal/domains/product/services"
 | ||
| 	"tyapi-server/internal/infrastructure/task/handlers"
 | ||
| 	"tyapi-server/internal/infrastructure/task/repositories"
 | ||
| 	"tyapi-server/internal/infrastructure/task/types"
 | ||
| )
 | ||
| 
 | ||
| // AsynqWorker Asynq Worker实现
 | ||
| type AsynqWorker struct {
 | ||
| 	server         *asynq.Server
 | ||
| 	mux            *asynq.ServeMux
 | ||
| 	logger         *zap.Logger
 | ||
| 	articleHandler *handlers.ArticleTaskHandler
 | ||
| 	apiHandler     *handlers.ApiTaskHandler
 | ||
| }
 | ||
| 
 | ||
| // NewAsynqWorker 创建Asynq Worker
 | ||
| func NewAsynqWorker(
 | ||
| 	redisAddr string,
 | ||
| 	logger *zap.Logger,
 | ||
| 	articleApplicationService article.ArticleApplicationService,
 | ||
| 	apiApplicationService api.ApiApplicationService,
 | ||
| 	walletService finance_services.WalletAggregateService,
 | ||
| 	subscriptionService *product_services.ProductSubscriptionService,
 | ||
| 	asyncTaskRepo repositories.AsyncTaskRepository,
 | ||
| ) *AsynqWorker {
 | ||
| 	server := asynq.NewServer(
 | ||
| 		asynq.RedisClientOpt{Addr: redisAddr},
 | ||
| 		asynq.Config{
 | ||
| 			Concurrency: 6, // 降低总并发数
 | ||
| 			Queues: map[string]int{
 | ||
| 				"default": 2,  // 2个goroutine
 | ||
| 				"api":     3,  // 3个goroutine (扣款任务)
 | ||
| 				"article": 1,  // 1个goroutine
 | ||
| 			},
 | ||
| 		},
 | ||
| 	)
 | ||
| 
 | ||
| 	// 创建任务处理器
 | ||
| 	articleHandler := handlers.NewArticleTaskHandler(logger, articleApplicationService, asyncTaskRepo)
 | ||
| 	apiHandler := handlers.NewApiTaskHandler(logger, apiApplicationService, walletService, subscriptionService, asyncTaskRepo)
 | ||
| 
 | ||
| 	// 创建ServeMux
 | ||
| 	mux := asynq.NewServeMux()
 | ||
| 
 | ||
| 	return &AsynqWorker{
 | ||
| 		server:         server,
 | ||
| 		mux:            mux,
 | ||
| 		logger:         logger,
 | ||
| 		articleHandler: articleHandler,
 | ||
| 		apiHandler:     apiHandler,
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| // RegisterHandler 注册任务处理器
 | ||
| func (w *AsynqWorker) RegisterHandler(taskType types.TaskType, handler func(context.Context, *asynq.Task) error) {
 | ||
| 	// 简化实现,避免API兼容性问题
 | ||
| 	w.logger.Info("注册任务处理器", zap.String("task_type", string(taskType)))
 | ||
| }
 | ||
| 
 | ||
| // Start 启动Worker
 | ||
| func (w *AsynqWorker) Start() error {
 | ||
| 	w.logger.Info("启动Asynq Worker")
 | ||
| 
 | ||
| 	// 注册所有任务处理器
 | ||
| 	w.registerAllHandlers()
 | ||
| 
 | ||
| 	// 启动Worker服务器
 | ||
| 	go func() {
 | ||
| 		if err := w.server.Run(w.mux); err != nil {
 | ||
| 			w.logger.Error("Worker运行失败", zap.Error(err))
 | ||
| 		}
 | ||
| 	}()
 | ||
| 
 | ||
| 	w.logger.Info("Asynq Worker启动成功")
 | ||
| 	return nil
 | ||
| }
 | ||
| 
 | ||
| // Stop 停止Worker
 | ||
| func (w *AsynqWorker) Stop() {
 | ||
| 	w.logger.Info("停止Asynq Worker")
 | ||
| 	w.server.Stop()
 | ||
| }
 | ||
| 
 | ||
| // Shutdown 优雅关闭Worker
 | ||
| func (w *AsynqWorker) Shutdown() {
 | ||
| 	w.logger.Info("优雅关闭Asynq Worker")
 | ||
| 	w.server.Shutdown()
 | ||
| }
 | ||
| 
 | ||
| // registerAllHandlers 注册所有任务处理器
 | ||
| func (w *AsynqWorker) registerAllHandlers() {
 | ||
| 	// 注册文章任务处理器
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeArticlePublish), w.articleHandler.HandleArticlePublish)
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeArticleCancel), w.articleHandler.HandleArticleCancel)
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeArticleModify), w.articleHandler.HandleArticleModify)
 | ||
| 
 | ||
| 	// 注册API任务处理器
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeApiCall), w.apiHandler.HandleApiCall)
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeApiLog), w.apiHandler.HandleApiLog)
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeDeduction), w.apiHandler.HandleDeduction)
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeCompensation), w.apiHandler.HandleCompensation)
 | ||
| 	w.mux.HandleFunc(string(types.TaskTypeUsageStats), w.apiHandler.HandleUsageStats)
 | ||
| 
 | ||
| 	w.logger.Info("所有任务处理器注册完成",
 | ||
| 		zap.String("article_publish", string(types.TaskTypeArticlePublish)),
 | ||
| 		zap.String("article_cancel", string(types.TaskTypeArticleCancel)),
 | ||
| 		zap.String("article_modify", string(types.TaskTypeArticleModify)),
 | ||
| 		zap.String("api_call", string(types.TaskTypeApiCall)),
 | ||
| 		zap.String("api_log", string(types.TaskTypeApiLog)),
 | ||
| 	)
 | ||
| }
 |