Files
protocol/webhook/notifier.go
Raja Subramanian 9b6e5d703f Webhook analytics event. (#979)
* Webhook analytics event.

NOTE: Need a frostbyte73/core PR to merge and tag a release
PR pending: https://github.com/frostbyte73/core/pull/5

* sync submit

* generated protobuf

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2025-02-13 10:21:17 +05:30

74 lines
1.9 KiB
Go

// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webhook
import (
"context"
"sync"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
)
type QueuedNotifier interface {
RegisterProcessedHook(f func(ctx context.Context, whi *livekit.WebhookInfo))
QueueNotify(ctx context.Context, event *livekit.WebhookEvent) error
}
type DefaultNotifier struct {
urlNotifiers []*URLNotifier
}
func NewDefaultNotifier(apiKey, apiSecret string, urls []string) QueuedNotifier {
n := &DefaultNotifier{}
for _, url := range urls {
u := NewURLNotifier(URLNotifierParams{
URL: url,
Logger: logger.GetLogger().WithComponent("webhook"),
APIKey: apiKey,
APISecret: apiSecret,
})
n.urlNotifiers = append(n.urlNotifiers, u)
}
return n
}
func (n *DefaultNotifier) Stop(force bool) {
wg := sync.WaitGroup{}
for _, u := range n.urlNotifiers {
wg.Add(1)
go func(u *URLNotifier) {
defer wg.Done()
u.Stop(force)
}(u)
}
wg.Wait()
}
func (n *DefaultNotifier) QueueNotify(ctx context.Context, event *livekit.WebhookEvent) error {
for _, u := range n.urlNotifiers {
if err := u.QueueNotify(ctx, event); err != nil {
return err
}
}
return nil
}
func (n *DefaultNotifier) RegisterProcessedHook(hook func(ctx context.Context, whi *livekit.WebhookInfo)) {
for _, u := range n.urlNotifiers {
u.RegisterProcessedHook(hook)
}
}