package imeiqueue import ( "context" "fmt" "time" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rabbitmq" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/uuid32" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" ) const fullSyncOperation = "imei_full_sync" type sImeiQueue struct{} var ( imeiNow = time.Now imeiRunID = uuid32.S imeiRabbitEnabled = rabbitmq.Enabled imeiPublish = rabbitmq.Publish ) func init() { service.RegisterImeiQueue(New()) } func New() *sImeiQueue { return &sImeiQueue{} } func (s *sImeiQueue) StartImeiRun(ctx context.Context) (string, error) { return s.StartImeiRunScoped(ctx, nil) } // StartImeiRunScoped starts an IMEI run restricted to scope.Channels. A nil // scope, or one whose channels normalize to empty, starts a full sync. // // The runId prefix keeps IMEI runs distinguishable from status runs in logs and // in the Redis key space (each chain has its own prefix anyway). func (s *sImeiQueue) StartImeiRunScoped(ctx context.Context, scope *model.SyncScope) (string, error) { if ctx == nil { ctx = context.Background() } normalized, err := model.NormalizeScope(scope) if err != nil { return "", err } if !imeiRabbitEnabled(ctx) { return "", gerror.New("imei queue start skipped: rabbitmq is disabled") } now := imeiNow() message := model.ImeiQueueStartMessage{ RunID: fmt.Sprintf("imei-%s-%s", now.Format("20060102150405"), imeiRunID()), Operation: fullSyncOperation, TriggeredAt: now.Format(time.RFC3339), Scope: normalized, } if err := imeiPublish(ctx, consts.QueueImeiQueueStart, message); err != nil { return "", gerror.Wrap(err, "publish imei queue start") } g.Log().Infof(ctx, "event=imei_queue_trigger status=published runId=%s scopeChannels=%d", message.RunID, normalized.ChannelCount()) return message.RunID, nil }