Files
protocol/webhook/notifier.go
David Zhao f3bba12b3e Component support for logger (#436)
* Component support for logger

With the ability to dynamically update log levels per component

* support hierarchical config

* consolidate pion logger to use components
2023-08-03 11:59:04 -07:00

67 lines
1.6 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 {
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(_ context.Context, event *livekit.WebhookEvent) error {
for _, u := range n.urlNotifiers {
if err := u.QueueNotify(event); err != nil {
return err
}
}
return nil
}