Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
fix: proper shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
elijaharita committed Sep 19, 2023
1 parent 8547780 commit b547e01
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cmd/motion/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -217,6 +218,11 @@ func main() {
logger.Errorw("Failed to start Singularity blob store", "err", err)
return err
}
defer func() {
if err := singularityStore.Shutdown(context.Background()); err != nil {
logger.Errorw("Failed to shut down Singularity blob store", "err", err)
}
}()
store = singularityStore
} else {
store = blob.NewLocalStore(storeDir)
Expand Down
30 changes: 22 additions & 8 deletions integration/singularity/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/data-preservation-programs/singularity/client/swagger/http/deal_schedule"
Expand All @@ -35,7 +36,7 @@ type SingularityStore struct {
sourceName string
toPack chan uint64
closing chan struct{}
closed chan struct{}
closed sync.WaitGroup
}

func NewStore(o ...Option) (*SingularityStore, error) {
Expand All @@ -50,7 +51,6 @@ func NewStore(o ...Option) (*SingularityStore, error) {
sourceName: "source",
toPack: make(chan uint64, 16),
closing: make(chan struct{}),
closed: make(chan struct{}),
}, nil
}

Expand Down Expand Up @@ -247,14 +247,16 @@ func (l *SingularityStore) Start(ctx context.Context) error {
}

func (l *SingularityStore) runPreparationJobs() {
l.closed.Add(1)

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
defer func() {
cancel()
l.closed.Done()
}()

go func() {
defer func() {
close(l.closed)
}()
for {
select {
case <-ctx.Done():
Expand All @@ -281,15 +283,24 @@ func (l *SingularityStore) runPreparationJobs() {
}
}
}()
<-l.closing
}

func (l *SingularityStore) Shutdown(ctx context.Context) error {
close(l.closing)

done := make(chan struct{})
go func() {
l.closed.Wait()
close(done)
}()

select {
case <-ctx.Done():
case <-l.closed:
case <-done:
}

logger.Infof("Singularity store shut down")

return nil
}

Expand Down Expand Up @@ -433,6 +444,9 @@ func (s *SingularityStore) Describe(ctx context.Context, id blob.ID) (*blob.Desc
}

func (s *SingularityStore) runCleanupWorker(ctx context.Context) {
s.closed.Add(1)
defer s.closed.Done()

// Run immediately once before starting ticker
s.runCleanupJob()

Expand Down

0 comments on commit b547e01

Please sign in to comment.