|
| 1 | +// Copyright 2025 The Bucketeer Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package processor |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "go.uber.org/zap" |
| 24 | + "google.golang.org/protobuf/proto" |
| 25 | + |
| 26 | + "github.com/bucketeer-io/bucketeer/pkg/notification/sender/notifier" |
| 27 | + "github.com/bucketeer-io/bucketeer/pkg/pubsub/puller" |
| 28 | + "github.com/bucketeer-io/bucketeer/pkg/pubsub/puller/codes" |
| 29 | + "github.com/bucketeer-io/bucketeer/pkg/subscriber" |
| 30 | + domainevent "github.com/bucketeer-io/bucketeer/proto/event/domain" |
| 31 | + domaineventproto "github.com/bucketeer-io/bucketeer/proto/event/domain" |
| 32 | + notificationproto "github.com/bucketeer-io/bucketeer/proto/notification" |
| 33 | + senderproto "github.com/bucketeer-io/bucketeer/proto/notification/sender" |
| 34 | +) |
| 35 | + |
| 36 | +type DemoOrganizationCreationNotifierConfig struct { |
| 37 | + Notifier NotifierConfig `json:"notifier"` |
| 38 | +} |
| 39 | + |
| 40 | +type NotifierConfig struct { |
| 41 | + Slack SlackNotifierConfig `json:"slack"` |
| 42 | +} |
| 43 | + |
| 44 | +type SlackNotifierConfig struct { |
| 45 | + WebHookURL string `json:"webHookURL"` |
| 46 | +} |
| 47 | + |
| 48 | +type demoOrganizationCreationNotifier struct { |
| 49 | + slackNotifier notifier.Notifier |
| 50 | + demoOrganizationCreationNotifierConfig DemoOrganizationCreationNotifierConfig |
| 51 | + logger *zap.Logger |
| 52 | +} |
| 53 | + |
| 54 | +func NewDemoOrganizationCreationNotifier( |
| 55 | + config interface{}, |
| 56 | + webURL string, |
| 57 | + logger *zap.Logger, |
| 58 | +) subscriber.PubSubProcessor { |
| 59 | + jsonConfigMap, ok := config.(map[string]interface{}) |
| 60 | + if !ok { |
| 61 | + logger.Error("demoOrganizationCreationNotifier: invalid config type, expected map[string]interface{}") |
| 62 | + return nil |
| 63 | + } |
| 64 | + configBytes, err := json.Marshal(jsonConfigMap) |
| 65 | + if err != nil { |
| 66 | + logger.Error("demoOrganizationCreationNotifier: failed to marshal config", zap.Error(err)) |
| 67 | + return nil |
| 68 | + } |
| 69 | + var notifierConfig DemoOrganizationCreationNotifierConfig |
| 70 | + if err := json.Unmarshal(configBytes, ¬ifierConfig); err != nil { |
| 71 | + logger.Error("demoOrganizationCreationNotifier: failed to unmarshal config", zap.Error(err)) |
| 72 | + return nil |
| 73 | + } |
| 74 | + slackNotifier := notifier.NewSlackNotifier(webURL) |
| 75 | + |
| 76 | + return &demoOrganizationCreationNotifier{ |
| 77 | + slackNotifier: slackNotifier, |
| 78 | + demoOrganizationCreationNotifierConfig: notifierConfig, |
| 79 | + logger: logger, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func (d demoOrganizationCreationNotifier) Process(ctx context.Context, msgChan <-chan *puller.Message) error { |
| 84 | + for { |
| 85 | + select { |
| 86 | + case msg, ok := <-msgChan: |
| 87 | + if !ok { |
| 88 | + d.logger.Error("demoOrganizationCreationNotifier: message channel closed") |
| 89 | + return nil |
| 90 | + } |
| 91 | + subscriberReceivedCounter.WithLabelValues(subscriberDemoOrganizationEvent).Inc() |
| 92 | + d.handleMessage(msg) |
| 93 | + case <-ctx.Done(): |
| 94 | + d.logger.Debug("subscriber context done, stopped processing messages") |
| 95 | + return nil |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func (d demoOrganizationCreationNotifier) handleMessage(msg *puller.Message) { |
| 101 | + if id := msg.Attributes["id"]; id == "" { |
| 102 | + msg.Ack() |
| 103 | + subscriberHandledCounter.WithLabelValues(subscriberDemoOrganizationEvent, codes.MissingID.String()).Inc() |
| 104 | + return |
| 105 | + } |
| 106 | + domainEvent, err := d.unmarshalMessage(msg) |
| 107 | + if err != nil { |
| 108 | + d.logger.Error("Failed to unmarshal message", |
| 109 | + zap.Error(err), |
| 110 | + zap.String("msgID", msg.ID), |
| 111 | + zap.String("attributes", fmt.Sprintf("%+v", msg.Attributes)), |
| 112 | + ) |
| 113 | + subscriberHandledCounter.WithLabelValues(subscriberDemoOrganizationEvent, codes.BadMessage.String()).Inc() |
| 114 | + msg.Ack() |
| 115 | + return |
| 116 | + } |
| 117 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 118 | + defer cancel() |
| 119 | + |
| 120 | + if domainEvent.Type != domainevent.Event_DEMO_ORGANIZATION_CREATED { |
| 121 | + subscriberHandledCounter.WithLabelValues(subscriberDemoOrganizationEvent, codes.OK.String()).Inc() |
| 122 | + msg.Ack() |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + var organizationCreatedEvent domaineventproto.OrganizationCreatedEvent |
| 127 | + if err := domainEvent.Data.UnmarshalTo(&organizationCreatedEvent); err != nil { |
| 128 | + d.logger.Error("Failed to unmarshal OrganizationCreatedEvent", |
| 129 | + zap.String("event id", domainEvent.Id), |
| 130 | + zap.Error(err), |
| 131 | + ) |
| 132 | + subscriberHandledCounter.WithLabelValues( |
| 133 | + subscriberDemoOrganizationEvent, |
| 134 | + codes.NonRepeatableError.String(), |
| 135 | + ).Inc() |
| 136 | + msg.Ack() |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + recipient := ¬ificationproto.Recipient{ |
| 141 | + Type: notificationproto.Recipient_SlackChannel, |
| 142 | + Language: notificationproto.Recipient_ENGLISH, |
| 143 | + SlackChannelRecipient: ¬ificationproto.SlackChannelRecipient{ |
| 144 | + WebhookUrl: d.demoOrganizationCreationNotifierConfig.Notifier.Slack.WebHookURL, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + err = d.slackNotifier.Notify(ctx, &senderproto.Notification{ |
| 149 | + Type: senderproto.Notification_DemoOrganizationCreation, |
| 150 | + DemoOrganizationCreationNotification: &senderproto.DemoOrganizationCreationNotification{ |
| 151 | + OwnerEmail: organizationCreatedEvent.OwnerEmail, |
| 152 | + OrganizationId: organizationCreatedEvent.Id, |
| 153 | + OrganizationName: organizationCreatedEvent.Name, |
| 154 | + }, |
| 155 | + }, recipient, recipient.Language) |
| 156 | + if err != nil { |
| 157 | + d.logger.Error("Failed to send notification", |
| 158 | + zap.Error(err), |
| 159 | + zap.String("event id", domainEvent.Id), |
| 160 | + zap.String("webhookURL", d.demoOrganizationCreationNotifierConfig.Notifier.Slack.WebHookURL), |
| 161 | + zap.String("organizationId", organizationCreatedEvent.Id), |
| 162 | + ) |
| 163 | + subscriberHandledCounter.WithLabelValues( |
| 164 | + subscriberDemoOrganizationEvent, |
| 165 | + codes.NonRepeatableError.String(), |
| 166 | + ).Inc() |
| 167 | + msg.Ack() |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + subscriberHandledCounter.WithLabelValues( |
| 172 | + subscriberDemoOrganizationEvent, |
| 173 | + codes.OK.String(), |
| 174 | + ).Inc() |
| 175 | + msg.Ack() |
| 176 | +} |
| 177 | + |
| 178 | +func (d demoOrganizationCreationNotifier) unmarshalMessage(msg *puller.Message) (*domainevent.Event, error) { |
| 179 | + event := &domaineventproto.Event{} |
| 180 | + err := proto.Unmarshal(msg.Data, event) |
| 181 | + if err != nil { |
| 182 | + d.logger.Error("Failed to unmarshal message", zap.Error(err), zap.String("msgID", msg.ID)) |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + return event, nil |
| 186 | +} |
0 commit comments