50 lines
1.7 KiB
Go
50 lines
1.7 KiB
Go
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"
|
|
dailyusageproducer "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/producer/dailyusage"
|
|
)
|
|
|
|
func (c *Consumers) initTrafficUsageDailyStart(ctx context.Context) error {
|
|
if !mq.Enabled(ctx) {
|
|
g.Log().Info(ctx, "traffic usage daily start consumer skipped, RabbitMQ disabled")
|
|
return nil
|
|
}
|
|
if err := mq.ConsumeJSON[model.TrafficUsageDailyStartMessage](
|
|
ctx,
|
|
consts.QueueTrafficUsageDailyStart,
|
|
"iotlcm-traffic-usage-daily-start",
|
|
c.handleTrafficUsageDailyStart,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
g.Log().Infof(ctx, "traffic usage daily start consumer started, queue:%s", consts.QueueTrafficUsageDailyStart)
|
|
return nil
|
|
}
|
|
|
|
func (c *Consumers) handleTrafficUsageDailyStart(ctx context.Context, message model.TrafficUsageDailyStartMessage) error {
|
|
if message.RunID == "" || message.QueryDate == "" {
|
|
g.Log().Errorf(ctx, "traffic usage daily start message dropped, invalid message:%+v", message)
|
|
return nil
|
|
}
|
|
err := c.trafficUsageDailyStart.Produce(ctx, message)
|
|
if errors.Is(err, dailyusageproducer.ErrAlreadyRunning) {
|
|
g.Log().Warningf(ctx, "traffic usage daily 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, dailyusageproducer.ErrRunFinished) {
|
|
g.Log().Warningf(ctx, "traffic usage daily start skipped because run already finished, runId:%s", message.RunID)
|
|
return nil
|
|
}
|
|
return err
|
|
}
|