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" monthlyusageproducer "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/producer/monthlyusage" ) func (c *Consumers) initTrafficUsageMonthlyStart(ctx context.Context) error { if !mq.Enabled(ctx) { g.Log().Info(ctx, "traffic usage monthly start consumer skipped, RabbitMQ disabled") return nil } if err := mq.ConsumeJSON[model.TrafficUsageMonthlyStartMessage]( ctx, consts.QueueTrafficUsageMonthlyStart, "iotlcm-traffic-usage-monthly-start", c.handleTrafficUsageMonthlyStart, ); err != nil { return err } g.Log().Infof(ctx, "traffic usage monthly start consumer started, queue:%s", consts.QueueTrafficUsageMonthlyStart) return nil } func (c *Consumers) handleTrafficUsageMonthlyStart(ctx context.Context, message model.TrafficUsageMonthlyStartMessage) error { if message.RunID == "" || message.QueryMonth == "" { g.Log().Errorf(ctx, "traffic usage monthly start message dropped, invalid message:%+v", message) return nil } err := c.trafficUsageMonthlyStart.Produce(ctx, message) if errors.Is(err, monthlyusageproducer.ErrAlreadyRunning) { g.Log().Warningf(ctx, "traffic usage monthly 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, monthlyusageproducer.ErrRunFinished) { g.Log().Warningf(ctx, "traffic usage monthly start skipped because run already finished, runId:%s", message.RunID) return nil } return err }