mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-28 08:05:45 +00:00
33 lines
799 B
Go
33 lines
799 B
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/warmbly/warmbly/internal/infrastructure/kafka"
|
|
"github.com/warmbly/warmbly/internal/models"
|
|
)
|
|
|
|
func (s *WorkerService) Produce(jobEventType models.JobEventType, key string, body any) error {
|
|
if state, ok := body.(*models.JobEventSyncState); ok {
|
|
workerID, err := uuid.Parse(s.ID)
|
|
if err != nil || workerID == uuid.Nil {
|
|
return fmt.Errorf("sync state requires a valid reporting worker")
|
|
}
|
|
copy := *state
|
|
copy.WorkerID = &workerID
|
|
body = ©
|
|
}
|
|
ctx := context.Background()
|
|
payload, err := s.Codec.Serialize(ctx, kafka.TopicWorkerEvents, &models.JobEvent{
|
|
Type: jobEventType,
|
|
Body: body,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.Bus.Publish(ctx, kafka.TopicWorkerEvents, key, payload)
|
|
}
|