83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package simstatusqueue
|
|
|
|
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 = "sim_status_queue_full_sync"
|
|
|
|
type sSimStatusQueue struct{}
|
|
|
|
func init() { service.RegisterSimStatusQueue(New()) }
|
|
|
|
func New() *sSimStatusQueue { return &sSimStatusQueue{} }
|
|
|
|
func (s *sSimStatusQueue) StartFullSync(ctx context.Context) error {
|
|
_, err := s.StartFullSyncRun(ctx)
|
|
return err
|
|
}
|
|
|
|
func (s *sSimStatusQueue) StartFullSyncRun(ctx context.Context) (string, error) {
|
|
return s.StartFullSyncRunScoped(ctx, nil)
|
|
}
|
|
|
|
// StartFullSyncRunScoped starts a status run restricted to scope.Channels. A nil
|
|
// scope, or one whose channels normalize to empty, starts a full sync.
|
|
func (s *sSimStatusQueue) StartFullSyncRunScoped(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 !rabbitmq.Enabled(ctx) {
|
|
return "", gerror.New("sim status queue start skipped: rabbitmq is disabled")
|
|
}
|
|
now := time.Now()
|
|
message := model.SimStatusQueueStartMessage{
|
|
RunID: fmt.Sprintf("sim-status-%s-%s", now.Format("20060102150405"), uuid32.S()),
|
|
Operation: fullSyncOperation,
|
|
TriggeredAt: now.Format(time.RFC3339),
|
|
Scope: normalized,
|
|
}
|
|
if err := rabbitmq.Publish(ctx, consts.QueueSimStatusQueueStart, message); err != nil {
|
|
return "", gerror.Wrap(err, "publish sim status queue start")
|
|
}
|
|
g.Log().Infof(ctx, "event=sim_status_queue_trigger status=published runId=%s", message.RunID)
|
|
return message.RunID, nil
|
|
}
|
|
|
|
func (s *sSimStatusQueue) EnqueueCard(ctx context.Context, message model.SimStatusQueueTaskMessage) error {
|
|
if message.RequestID == "" {
|
|
message.RequestID = uuid32.S()
|
|
}
|
|
if message.RunID == "" {
|
|
message.RunID = "sim-status-card-" + message.RequestID
|
|
}
|
|
if message.Operation == "" {
|
|
message.Operation = "sim_status_queue_task"
|
|
}
|
|
if message.CardID <= 0 || message.ICCID == "" || message.ChanKey == "" || message.TempKey == "" {
|
|
return gerror.New("invalid sim status queue card")
|
|
}
|
|
if !rabbitmq.Enabled(ctx) {
|
|
return gerror.New("sim status queue task skipped: rabbitmq is disabled")
|
|
}
|
|
if err := rabbitmq.Publish(ctx, consts.QueueSimStatusQueueTask, message); err != nil {
|
|
return gerror.Wrap(err, "publish sim status queue task")
|
|
}
|
|
return nil
|
|
}
|