50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package workerdiagnostic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/util/guid"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/model"
|
|
)
|
|
|
|
type Publisher func(ctx context.Context, queue string, data any) error
|
|
|
|
type Service struct {
|
|
publisher Publisher
|
|
}
|
|
|
|
type Result struct {
|
|
TraceID string
|
|
Queue string
|
|
SentAt int64
|
|
}
|
|
|
|
func New(publisher Publisher) *Service {
|
|
return &Service{publisher: publisher}
|
|
}
|
|
|
|
func (s *Service) Publish(ctx context.Context, message string) (*Result, error) {
|
|
var (
|
|
traceID = fmt.Sprintf("worker-test-%s", guid.S())
|
|
sentAt = gtime.TimestampMilli()
|
|
payload = model.WorkerDiagnosticMessage{
|
|
TraceID: traceID,
|
|
SentAt: sentAt,
|
|
Message: message,
|
|
}
|
|
)
|
|
if err := s.publisher(ctx, consts.QueueWorkerDiagnostic, payload); err != nil {
|
|
return nil, gerror.Wrap(err, "publish worker diagnostic message failed")
|
|
}
|
|
return &Result{
|
|
TraceID: traceID,
|
|
Queue: consts.QueueWorkerDiagnostic,
|
|
SentAt: sentAt,
|
|
}, nil
|
|
}
|