diff --git a/main.go b/main.go index d20b696..c13cef1 100644 --- a/main.go +++ b/main.go @@ -97,7 +97,9 @@ func cmdBase(_ *cobra.Command, _ []string) { if err != nil { logrus.WithError(err). Error("Failed to get new task") - sleep(viper.GetDuration(ConfCooldown), appCtx) + if !sleep(viper.GetDuration(ConfCooldown), appCtx) { + goto shutdown + } continue } if t == nil { @@ -190,11 +192,11 @@ func hardShutdown(c context.Context) { os.Exit(1) } -func sleep(d time.Duration, c context.Context) { +func sleep(d time.Duration, c context.Context) bool { select { case <-time.After(d): - break + return true case <-c.Done(): - break + return false } } diff --git a/scheduler.go b/scheduler.go index 03e67c2..6a00420 100644 --- a/scheduler.go +++ b/scheduler.go @@ -65,11 +65,8 @@ func scheduleNewTask(c context.Context, remote *OD) bool { // Sleep if max number of tasks are active for atomic.LoadInt32(&numActiveTasks) > config.Tasks { - select { - case <-c.Done(): - return false - case <-time.After(time.Second): - return true + if !sleep(time.Second, c) { + break } }