Harden backup and restore operations
This commit is contained in:
+13
-1
@@ -25,10 +25,12 @@ type Queue struct {
|
||||
stopping bool
|
||||
handler Handler
|
||||
onChange OnChange
|
||||
done chan struct{}
|
||||
doneOnce sync.Once
|
||||
}
|
||||
|
||||
func New(handler Handler, onChange OnChange) *Queue {
|
||||
q := &Queue{handler: handler, onChange: onChange}
|
||||
q := &Queue{handler: handler, onChange: onChange, done: make(chan struct{})}
|
||||
q.cond = sync.NewCond(&q.mu)
|
||||
return q
|
||||
}
|
||||
@@ -71,6 +73,7 @@ func (q *Queue) Enqueue(run model.Run) error {
|
||||
}
|
||||
|
||||
func (q *Queue) Run(ctx context.Context) {
|
||||
defer q.doneOnce.Do(func() { close(q.done) })
|
||||
for {
|
||||
q.mu.Lock()
|
||||
for len(q.pending) == 0 && !q.stopping {
|
||||
@@ -104,6 +107,15 @@ func (q *Queue) Run(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) Wait(ctx context.Context) error {
|
||||
select {
|
||||
case <-q.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) Cancel(id string) bool {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user