2021-01-26 06:56:20 -06:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-25 07:24:21 -05:00
|
|
|
"fmt"
|
2021-01-26 06:56:20 -06:00
|
|
|
"sync"
|
2022-03-25 07:24:21 -05:00
|
|
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"go.uber.org/atomic"
|
2021-01-26 06:56:20 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProcessContext struct {
|
|
|
|
wg *sync.WaitGroup // used to wait for components to shutdown
|
|
|
|
ctx context.Context // cancelled when Stop is called
|
|
|
|
shutdown context.CancelFunc // shut down Dendrite
|
2022-03-25 07:24:21 -05:00
|
|
|
degraded atomic.Bool
|
2021-01-26 06:56:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcessContext() *ProcessContext {
|
|
|
|
ctx, shutdown := context.WithCancel(context.Background())
|
|
|
|
return &ProcessContext{
|
|
|
|
ctx: ctx,
|
|
|
|
shutdown: shutdown,
|
|
|
|
wg: &sync.WaitGroup{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 07:24:21 -05:00
|
|
|
|
|
|
|
func (b *ProcessContext) Degraded() {
|
2022-09-20 09:01:19 -05:00
|
|
|
if b.degraded.CompareAndSwap(false, true) {
|
2022-03-25 07:24:21 -05:00
|
|
|
logrus.Warn("Dendrite is running in a degraded state")
|
|
|
|
sentry.CaptureException(fmt.Errorf("Process is running in a degraded state"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ProcessContext) IsDegraded() bool {
|
|
|
|
return b.degraded.Load()
|
|
|
|
}
|