Skip to content

Commit bb3790b

Browse files
authored
feat: notifier behind nats (#5185)
* feat: notifier behind nats * chore: wire * chore: run test case again after updating common lib by taking merge * fix: update common lib and wire * fix : add argo cd assets * chore: update branch * chore: merge main * chore: update common lib
1 parent b006b34 commit bb3790b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+818
-34
lines changed

client/events/EventClient.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ import (
3737
)
3838

3939
type EventClientConfig struct {
40-
DestinationURL string `env:"EVENT_URL" envDefault:"http://localhost:3000/notify"`
40+
DestinationURL string `env:"EVENT_URL" envDefault:"http://localhost:3000/notify"`
41+
NotificationMedium NotificationMedium `env:"NOTIFICATION_MEDIUM" envDefault:"rest"`
4142
}
43+
type NotificationMedium string
44+
45+
const PUB_SUB NotificationMedium = "nats"
4246

4347
func GetEventClientConfig() (*EventClientConfig, error) {
4448
cfg := &EventClientConfig{}
@@ -238,6 +242,16 @@ func (impl *EventRESTClientImpl) WriteNotificationEvent(event Event) (bool, erro
238242
}
239243
return true, err
240244
}
245+
func (impl *EventRESTClientImpl) sendEventsOnNats(body []byte) error {
246+
247+
err := impl.pubsubClient.Publish(pubsub.NOTIFICATION_EVENT_TOPIC, string(body))
248+
if err != nil {
249+
impl.logger.Errorw("err while publishing msg for testing topic", "msg", body, "err", err)
250+
return err
251+
}
252+
return nil
253+
254+
}
241255

242256
// do not call this method if notification module is not installed
243257
func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
@@ -247,6 +261,14 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
247261
impl.logger.Errorw("error while marshaling event request ", "err", err)
248262
return false, err
249263
}
264+
if impl.config.NotificationMedium == PUB_SUB {
265+
err = impl.sendEventsOnNats(body)
266+
if err != nil {
267+
impl.logger.Errorw("error while publishing event ", "err", err)
268+
return false, err
269+
}
270+
return true, nil
271+
}
250272
var reqBody = []byte(body)
251273
req, err := http.NewRequest(http.MethodPost, impl.config.DestinationURL, bytes.NewBuffer(reqBody))
252274
if err != nil {

client/events/event_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
pubsub_lib "github.com/devtron-labs/common-lib/pubsub-lib"
6+
"github.com/devtron-labs/devtron/internal/sql/repository"
7+
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
8+
"github.com/devtron-labs/devtron/internal/util"
9+
"github.com/devtron-labs/devtron/pkg/sql"
10+
"testing"
11+
)
12+
13+
func TestSendEventsOnNats(t *testing.T) {
14+
logger, err := util.NewSugardLogger()
15+
//nats, err := pubsub_lib.NewNatsClient(logger)
16+
//mockPubsubClient := NewPubSubClientServiceImpl(logger)
17+
mockPubsubClient, err := pubsub_lib.NewPubSubClientServiceImpl(logger)
18+
client := util.NewHttpClient()
19+
config := sql.Config{}
20+
db, err := sql.NewDbConnection(&config, logger)
21+
trans := sql.NewTransactionUtilImpl(db)
22+
impl := &EventRESTClientImpl{
23+
logger: logger,
24+
pubsubClient: mockPubsubClient,
25+
client: client,
26+
config: &EventClientConfig{DestinationURL: "localhost:3000/notify", NotificationMedium: PUB_SUB},
27+
ciPipelineRepository: pipelineConfig.NewCiPipelineRepositoryImpl(db, logger, trans),
28+
pipelineRepository: pipelineConfig.NewPipelineRepositoryImpl(db, logger),
29+
attributesRepository: repository.NewAttributesRepositoryImpl(db),
30+
}
31+
//xpectedTopic := "NOTIFICATION_EVENT_TOPIC"
32+
expectedMsg := "'{\"eventTypeId\":1,\"pipelineId\":123,\"payload\":{\"key\":\"value\"},\"eventTime\":\"2024-05-09T12:00:00Z\",\"appId\":456,\"envId\":789,\"teamId\":101}'"
33+
34+
err = impl.sendEventsOnNats([]byte(expectedMsg))
35+
fmt.Println(err)
36+
37+
}

env_gen.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
| NATS_MSG_MAX_AGE | 86400 | |
178178
| NATS_MSG_PROCESSING_BATCH_SIZE | 1 | |
179179
| NATS_SERVER_HOST | nats://devtron-nats.devtroncd:4222 | |
180+
| NOTIFICATION_MEDIUM | rest | |
180181
| ORCH_HOST | http://devtroncd-orchestrator-service-prod.devtroncd/webhook/msg/nats | |
181182
| ORCH_TOKEN | | |
182183
| OTEL_COLLECTOR_URL | | |

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/davecgh/go-spew v1.1.1
1919
github.com/deckarep/golang-set v1.8.0
2020
github.com/devtron-labs/authenticator v0.4.35-0.20240405091826-a91813c53470
21-
github.com/devtron-labs/common-lib v0.0.18-0.20240524141543-f4ed1281e694
21+
github.com/devtron-labs/common-lib v0.0.19-0.20240607054959-82c79c23b046
2222
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534
2323
github.com/evanphx/json-patch v5.6.0+incompatible
2424
github.com/gammazero/workerpool v1.1.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4 h1:YcpmyvADG
207207
github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
208208
github.com/devtron-labs/authenticator v0.4.35-0.20240405091826-a91813c53470 h1:AUTYcDnL6w6Ux+264VldYaOUQAP6pDZ5Tq8wCKJyiEg=
209209
github.com/devtron-labs/authenticator v0.4.35-0.20240405091826-a91813c53470/go.mod h1:JQxTCMmQisrpjzETJr0tzVadV+wW23rHEZAY7JVyK3s=
210-
github.com/devtron-labs/common-lib v0.0.18-0.20240524141543-f4ed1281e694 h1:lUcMarRvAKzsLpmuYwFgOsKLJQpHsJuvbKG+we/dI58=
211-
github.com/devtron-labs/common-lib v0.0.18-0.20240524141543-f4ed1281e694/go.mod h1:deAcJ5IjUjM6ozZQLJEgPWDUA0mKa632LBsKx8uM9TE=
210+
github.com/devtron-labs/common-lib v0.0.19-0.20240607054959-82c79c23b046 h1:hOyqkgILg+eDttLV6X7OAAo9PKEHzInUmBTVy/EY/iI=
211+
github.com/devtron-labs/common-lib v0.0.19-0.20240607054959-82c79c23b046/go.mod h1:deAcJ5IjUjM6ozZQLJEgPWDUA0mKa632LBsKx8uM9TE=
212212
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534 h1:TElPRU69QedW7DIQiiQxtjwSQ6cK0fCTAMGvSLhP0ac=
213213
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534/go.mod h1:ypUknVph8Ph4dxSlrFoouf7wLedQxHku2LQwgRrdgS4=
214214
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

vendor/github.com/devtron-labs/common-lib/blob-storage/AwsS3Blob.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/AzureBlob.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/Bean.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/BlobStorageService.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/BlobUtils.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)