2021-01-26 12:56:20 +00:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2022-03-25 12:24:21 +00:00
|
|
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-26 12:56:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProcessContext struct {
|
2022-10-04 12:02:41 +00:00
|
|
|
mu sync.RWMutex
|
2023-05-04 00:21:33 +00:00
|
|
|
wg sync.WaitGroup // used to wait for components to shutdown
|
2022-10-04 12:02:41 +00:00
|
|
|
ctx context.Context // cancelled when Stop is called
|
|
|
|
shutdown context.CancelFunc // shut down Dendrite
|
|
|
|
degraded map[string]struct{} // reasons why the process is degraded
|
2021-01-26 12:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcessContext() *ProcessContext {
|
|
|
|
ctx, shutdown := context.WithCancel(context.Background())
|
|
|
|
return &ProcessContext{
|
|
|
|
ctx: ctx,
|
|
|
|
shutdown: shutdown,
|
2023-05-04 00:21:33 +00:00
|
|
|
wg: sync.WaitGroup{},
|
2021-01-26 12:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ProcessContext) Context() context.Context {
|
|
|
|
return context.WithValue(b.ctx, "scope", "process") // nolint:staticcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ProcessContext) ComponentStarted() {
|
|
|
|
b.wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ProcessContext) ComponentFinished() {
|
|
|
|
b.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ProcessContext) ShutdownDendrite() {
|
|
|
|
b.shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ProcessContext) WaitForShutdown() <-chan struct{} {
|
|
|
|
return b.ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ProcessContext) WaitForComponentsToFinish() {
|
|
|
|
b.wg.Wait()
|
|
|
|
}
|
2022-03-25 12:24:21 +00:00
|
|
|
|
2022-10-04 12:02:41 +00:00
|
|
|
func (b *ProcessContext) Degraded(err error) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if _, ok := b.degraded[err.Error()]; !ok {
|
|
|
|
logrus.WithError(err).Warn("Dendrite has entered a degraded state")
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
b.degraded[err.Error()] = struct{}{}
|
2022-03-25 12:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:02:41 +00:00
|
|
|
func (b *ProcessContext) IsDegraded() (bool, []string) {
|
|
|
|
b.mu.RLock()
|
|
|
|
defer b.mu.RUnlock()
|
|
|
|
if len(b.degraded) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
reasons := make([]string, 0, len(b.degraded))
|
|
|
|
for reason := range b.degraded {
|
|
|
|
reasons = append(reasons, reason)
|
|
|
|
}
|
|
|
|
return true, reasons
|
2022-03-25 12:24:21 +00:00
|
|
|
}
|