aboutsummaryrefslogtreecommitdiff
path: root/setup/process
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2021-01-26 12:56:20 +0000
committerGitHub <noreply@github.com>2021-01-26 12:56:20 +0000
commit9f443317bc578e1897c7eab9b4911f952f39fdbc (patch)
tree1c758596b56fcf9042c688d9f0204d731dbc216e /setup/process
parent64fb6de6d4f0860cc2b7503cfc36eb743552395b (diff)
Graceful shutdowns (#1734)
* Initial graceful stop * Fix dendritejs * Use process context for outbound federation requests in destination queues * Reduce logging * Fix log level
Diffstat (limited to 'setup/process')
-rw-r--r--setup/process/process.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/setup/process/process.go b/setup/process/process.go
new file mode 100644
index 00000000..d55751d7
--- /dev/null
+++ b/setup/process/process.go
@@ -0,0 +1,45 @@
+package process
+
+import (
+ "context"
+ "sync"
+)
+
+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
+}
+
+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()
+}