package rabbitmq import ( "context" "errors" "github.com/gogf/gf/v2/frame/g" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts" mq "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/library/rabbitmq" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model" monthlyhistoryproducer "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/producer/monthlyusagehistory" ) func (c *Consumers) initTrafficUsageMonthlyHistoryStart(ctx context.Context) error { if !mq.Enabled(ctx) { g.Log().Info(ctx, "traffic usage monthly history start consumer skipped, RabbitMQ disabled") return nil } if err := mq.ConsumeJSON[model.TrafficUsageMonthlyHistoryStartMessage]( ctx, consts.QueueTrafficUsageMonthlyHistoryStart, "iotlcm-traffic-usage-monthly-history-start", c.handleTrafficUsageMonthlyHistoryStart, ); err != nil { return err } g.Log().Infof(ctx, "traffic usage monthly history start consumer started, queue:%s", consts.QueueTrafficUsageMonthlyHistoryStart) return nil } func (c *Consumers) handleTrafficUsageMonthlyHistoryStart(ctx context.Context, message model.TrafficUsageMonthlyHistoryStartMessage) error { if message.RunID == "" || message.QueryMonth == "" { g.Log().Errorf(ctx, "traffic usage monthly history start message dropped, invalid message:%+v", message) return nil } err := c.trafficUsageMonthlyHistoryStart.Produce(ctx, message) if errors.Is(err, monthlyhistoryproducer.ErrAlreadyRunning) { g.Log().Warningf(ctx, "traffic usage monthly history start skipped because previous run is active, runId:%s", message.RunID) return nil } // A run that already finished must not be resurrected by a redelivered // message. Ack it instead of retrying forever. if errors.Is(err, monthlyhistoryproducer.ErrRunFinished) { g.Log().Warningf(ctx, "traffic usage monthly history start skipped because run already finished, runId:%s", message.RunID) return nil } return err }