All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m26s
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package bootstrap
|
||
|
||
import (
|
||
"github.com/hibiken/asynq"
|
||
"github.com/redis/go-redis/v9"
|
||
"go.uber.org/zap"
|
||
"gorm.io/gorm"
|
||
|
||
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
||
"github.com/break/junhong_cmp_fiber/pkg/queue"
|
||
"github.com/break/junhong_cmp_fiber/pkg/storage"
|
||
)
|
||
|
||
// WorkerDependencies Worker 进程的基础依赖
|
||
type WorkerDependencies struct {
|
||
DB *gorm.DB
|
||
Redis *redis.Client
|
||
Logger *zap.Logger
|
||
AsynqClient *asynq.Client // Worker 特有:用于 Scheduler 提交任务
|
||
QueueClient *queue.Client // 统一业务任务客户端,用于 Worker 内产生后续任务
|
||
StorageService *storage.Service // 对象存储(可选)
|
||
GatewayClient *gateway.Client // Gateway 客户端(可选)
|
||
}
|
||
|
||
// WorkerBootstrapResult Worker Bootstrap 初始化结果
|
||
type WorkerBootstrapResult = queue.WorkerBootstrapResult
|
||
|
||
// WorkerStores 导出的 Worker Store 集合
|
||
type WorkerStores = queue.WorkerStores
|
||
|
||
// WorkerServices 导出的 Worker 服务集合
|
||
type WorkerServices = queue.WorkerServices
|
||
|
||
// BootstrapWorker 初始化 Worker 进程的所有组件
|
||
//
|
||
// 初始化顺序:
|
||
// 1. 初始化 Worker Store 层(数据访问)
|
||
// 2. 初始化 Worker Service 层(业务逻辑)
|
||
//
|
||
// 参数:
|
||
// - deps: Worker 基础依赖(DB, Redis, Logger, AsynqClient, StorageService, GatewayClient)
|
||
//
|
||
// 返回:
|
||
// - *WorkerBootstrapResult: 包含 Stores 和 Services
|
||
// - error: 初始化错误
|
||
func BootstrapWorker(deps *WorkerDependencies) (*WorkerBootstrapResult, error) {
|
||
// 1. 初始化 Worker Store 层
|
||
stores := initWorkerStores(deps)
|
||
|
||
// 2. 初始化 Worker Service 层
|
||
services := initWorkerServices(stores, deps)
|
||
|
||
return &WorkerBootstrapResult{
|
||
Stores: stores,
|
||
Services: services,
|
||
}, nil
|
||
}
|