21020: Make arv-copy search from environment
[arvados.git] / lib / controller / trash.go
index d1a54b42387c9f74294674e4791cbbcd7d7391a3..662ea26751f501178a13243a8abe92caf4476adb 100644 (file)
@@ -25,7 +25,7 @@ func (h *Handler) periodicWorker(workerName string, interval time.Duration, lock
                return
        }
        defer locker.Unlock()
-       for time.Sleep(interval); ctx.Err() == nil; time.Sleep(interval) {
+       for ctxSleep(ctx, interval); ctx.Err() == nil; ctxSleep(ctx, interval) {
                if !locker.Check() {
                        // context canceled
                        return
@@ -85,3 +85,12 @@ DELETE FROM logs
                return nil
        })
 }
+
+// Sleep for the given duration, but return early if ctx cancels
+// before that.
+func ctxSleep(ctx context.Context, d time.Duration) {
+       select {
+       case <-ctx.Done():
+       case <-time.After(d):
+       }
+}