(ctx context.Context)
| 80 | } |
| 81 | |
| 82 | func (ps *PubSub) subscribeTask(ctx context.Context) error { |
| 83 | if ps.subURL == "" { |
| 84 | return nil |
| 85 | } |
| 86 | logger := log.FromContext(ctx) |
| 87 | subscription, err := pubsub.OpenSubscription(ctx, ps.subURL) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | logger.Info("Subscribed") |
| 92 | defer func() { |
| 93 | if err := subscription.Shutdown(ctx); err != nil { |
| 94 | logger.WithError(err).Warn("Failed to close Go Cloud subscription") |
| 95 | } else { |
| 96 | logger.Info("Unsubscribed") |
| 97 | } |
| 98 | }() |
| 99 | for { |
| 100 | msg, err := subscription.Receive(ctx) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | msg.Ack() |
| 105 | m := msg.Metadata["content-type"] |
| 106 | var evt events.Event |
| 107 | switch m { |
| 108 | case "application/protobuf": |
| 109 | var e ttnpb.Event |
| 110 | if err = proto.Unmarshal(msg.Body, &e); err != nil { |
| 111 | logger.WithError(err).Warn("Failed to unmarshal event from binary") |
| 112 | continue |
| 113 | } |
| 114 | if evt, err = events.FromProto(&e); err != nil { |
| 115 | logger.WithError(err).Warn("Failed to unmarshal event from protobuf") |
| 116 | continue |
| 117 | } |
| 118 | case "application/json": |
| 119 | if evt, err = events.UnmarshalJSON(msg.Body); err != nil { |
| 120 | logger.WithError(err).Warn("Failed to unmarshal event from JSON") |
| 121 | continue |
| 122 | } |
| 123 | default: |
| 124 | logger.WithField("content_type", m).Warn("Received event with unknown content type") |
| 125 | continue |
| 126 | } |
| 127 | ps.PubSub.Publish(evt) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Subscribe to events from Go Cloud. |
| 132 | func (ps *PubSub) Subscribe( |
nothing calls this directly
no test coverage detected