190 lines
6.6 KiB
Go
190 lines
6.6 KiB
Go
package rabbitmq
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/batch"
|
|
mq "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/library/rabbitmq"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
|
|
)
|
|
|
|
type TrafficUsageHandler interface {
|
|
RetryDaily(context.Context, model.TrafficUsageDailyRetryMessage) error
|
|
RetryMonthly(context.Context, model.TrafficUsageMonthlyRetryMessage) error
|
|
}
|
|
|
|
type WorkOrderHandler interface {
|
|
AutoCloseResolved(context.Context, model.WorkOrderAutoCloseMessage) error
|
|
}
|
|
|
|
type BalanceRechargeHandler interface {
|
|
CheckRechargeTimeout(context.Context, string) error
|
|
}
|
|
|
|
type BatchHandler interface {
|
|
Handle(context.Context, batch.Message) error
|
|
}
|
|
|
|
type PackageCustomerSyncHandler interface {
|
|
SyncDeptPackages(context.Context, model.PackageCustomerSyncMessage) error
|
|
}
|
|
|
|
type TrafficUsageMonthlyStartHandler interface {
|
|
Produce(context.Context, model.TrafficUsageMonthlyStartMessage) error
|
|
}
|
|
|
|
type TrafficUsageDailyStartHandler interface {
|
|
Produce(context.Context, model.TrafficUsageDailyStartMessage) error
|
|
}
|
|
|
|
type TrafficUsageMonthlyHistoryStartHandler interface {
|
|
Produce(context.Context, model.TrafficUsageMonthlyHistoryStartMessage) error
|
|
}
|
|
|
|
type SimStatusQueueStartHandler interface {
|
|
Produce(context.Context, model.SimStatusQueueStartMessage) error
|
|
}
|
|
|
|
type SimStatusQueueTaskHandler interface {
|
|
EnqueueCard(context.Context, model.SimStatusQueueTaskMessage) error
|
|
}
|
|
|
|
type ImeiQueueStartHandler interface {
|
|
Produce(context.Context, model.ImeiQueueStartMessage) error
|
|
}
|
|
|
|
// DataReviewQueryHandler 执行"手动同步上游结果":只读补查既有 operationId。
|
|
type DataReviewQueryHandler interface {
|
|
QueryReviewBatch(ctx context.Context, batchNo string) (int, error)
|
|
}
|
|
|
|
type Consumers struct {
|
|
trafficUsage TrafficUsageHandler
|
|
workOrder WorkOrderHandler
|
|
balance BalanceRechargeHandler
|
|
balanceRechargeRetryDelay time.Duration
|
|
balanceRechargeMaxRetry int
|
|
batch BatchHandler
|
|
batchExecuteRetryDelay time.Duration
|
|
batchExecuteMaxRetries int
|
|
packageCustomerSync PackageCustomerSyncHandler
|
|
trafficUsageMonthlyStart TrafficUsageMonthlyStartHandler
|
|
trafficUsageDailyStart TrafficUsageDailyStartHandler
|
|
trafficUsageMonthlyHistoryStart TrafficUsageMonthlyHistoryStartHandler
|
|
simStatusQueueStart SimStatusQueueStartHandler
|
|
simStatusQueueTask SimStatusQueueTaskHandler
|
|
imeiQueueStart ImeiQueueStartHandler
|
|
dataReview DataReviewQueryHandler
|
|
}
|
|
|
|
// WithDataReviewQuery adds the manual "sync upstream result" consumer without
|
|
// changing any existing RabbitMQ consumer wiring.
|
|
func (c *Consumers) WithDataReviewQuery(handler DataReviewQueryHandler) *Consumers {
|
|
c.dataReview = handler
|
|
return c
|
|
}
|
|
|
|
// WithDailyUsageStart adds the previous-day usage producer without changing any
|
|
// existing RabbitMQ consumer wiring.
|
|
func (c *Consumers) WithDailyUsageStart(start TrafficUsageDailyStartHandler) *Consumers {
|
|
c.trafficUsageDailyStart = start
|
|
return c
|
|
}
|
|
|
|
// WithMonthlyHistoryStart adds the previous-month usage history producer without
|
|
// changing any existing RabbitMQ consumer wiring.
|
|
func (c *Consumers) WithMonthlyHistoryStart(start TrafficUsageMonthlyHistoryStartHandler) *Consumers {
|
|
c.trafficUsageMonthlyHistoryStart = start
|
|
return c
|
|
}
|
|
|
|
// WithSimStatusQueue adds the independent status queue handlers without
|
|
// changing any existing RabbitMQ consumer wiring.
|
|
func (c *Consumers) WithSimStatusQueue(start SimStatusQueueStartHandler, task SimStatusQueueTaskHandler) *Consumers {
|
|
c.simStatusQueueStart = start
|
|
c.simStatusQueueTask = task
|
|
return c
|
|
}
|
|
|
|
// WithImeiQueue adds the independent IMEI queue handler without changing any
|
|
// existing RabbitMQ consumer wiring.
|
|
func (c *Consumers) WithImeiQueue(start ImeiQueueStartHandler) *Consumers {
|
|
c.imeiQueueStart = start
|
|
return c
|
|
}
|
|
|
|
func New(trafficUsage TrafficUsageHandler, workOrder WorkOrderHandler, balance BalanceRechargeHandler, batchHandlers ...BatchHandler) *Consumers {
|
|
consumers := &Consumers{
|
|
trafficUsage: trafficUsage,
|
|
workOrder: workOrder,
|
|
balance: balance,
|
|
}
|
|
if len(batchHandlers) > 0 {
|
|
consumers.batch = batchHandlers[0]
|
|
}
|
|
return consumers
|
|
}
|
|
|
|
func NewWithPackageCustomerSync(trafficUsage TrafficUsageHandler, workOrder WorkOrderHandler, balance BalanceRechargeHandler, packageCustomerSync PackageCustomerSyncHandler, batchHandlers ...BatchHandler) *Consumers {
|
|
consumers := New(trafficUsage, workOrder, balance, batchHandlers...)
|
|
consumers.packageCustomerSync = packageCustomerSync
|
|
return consumers
|
|
}
|
|
|
|
func NewWithPackageCustomerSyncAndMonthlyUsage(trafficUsage TrafficUsageHandler, workOrder WorkOrderHandler, balance BalanceRechargeHandler, packageCustomerSync PackageCustomerSyncHandler, monthlyStart TrafficUsageMonthlyStartHandler, batchHandlers ...BatchHandler) *Consumers {
|
|
consumers := NewWithPackageCustomerSync(trafficUsage, workOrder, balance, packageCustomerSync, batchHandlers...)
|
|
consumers.trafficUsageMonthlyStart = monthlyStart
|
|
return consumers
|
|
}
|
|
|
|
func (c *Consumers) Start(ctx context.Context) error {
|
|
initializers := c.initializers(g.Cfg().MustGet(ctx, "workerDiagnostic.enabled", false).Bool())
|
|
for _, initialize := range initializers {
|
|
if err := initialize(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Consumers) initializers(workerDiagnosticEnabled bool) []func(context.Context) error {
|
|
initializers := []func(context.Context) error{
|
|
c.initTrafficUsageDailyRetry,
|
|
c.initTrafficUsageMonthlyRetry,
|
|
c.initWorkOrderAutoClose,
|
|
c.initBalanceRechargeTimeout,
|
|
c.initBatchExecute,
|
|
c.initChannelRefresh,
|
|
c.initDataReviewQuery,
|
|
}
|
|
if c.packageCustomerSync != nil {
|
|
initializers = append(initializers, c.initPackageCustomerSync)
|
|
}
|
|
if c.trafficUsageMonthlyStart != nil {
|
|
initializers = append(initializers, c.initTrafficUsageMonthlyStart)
|
|
}
|
|
if c.trafficUsageDailyStart != nil {
|
|
initializers = append(initializers, c.initTrafficUsageDailyStart)
|
|
}
|
|
if c.trafficUsageMonthlyHistoryStart != nil {
|
|
initializers = append(initializers, c.initTrafficUsageMonthlyHistoryStart)
|
|
}
|
|
if c.simStatusQueueStart != nil && c.simStatusQueueTask != nil {
|
|
initializers = append(initializers, c.initSimStatusQueueStart, c.initSimStatusQueueTask)
|
|
}
|
|
if c.imeiQueueStart != nil {
|
|
initializers = append(initializers, c.initImeiQueueStart)
|
|
}
|
|
if workerDiagnosticEnabled {
|
|
initializers = append(initializers, c.initWorkerDiagnostic)
|
|
}
|
|
return initializers
|
|
}
|
|
|
|
func (c *Consumers) Stop(ctx context.Context) error {
|
|
return mq.Close(ctx)
|
|
}
|