Fix sleeps

This commit is contained in:
Richard Patel 2019-02-03 16:34:29 +01:00
parent 25d0b0042c
commit 4ef4ab13a8
No known key found for this signature in database
GPG Key ID: C268B2BBDA2ABECB
2 changed files with 8 additions and 9 deletions

10
main.go
View File

@ -97,7 +97,9 @@ func cmdBase(_ *cobra.Command, _ []string) {
if err != nil { if err != nil {
logrus.WithError(err). logrus.WithError(err).
Error("Failed to get new task") Error("Failed to get new task")
sleep(viper.GetDuration(ConfCooldown), appCtx) if !sleep(viper.GetDuration(ConfCooldown), appCtx) {
goto shutdown
}
continue continue
} }
if t == nil { if t == nil {
@ -190,11 +192,11 @@ func hardShutdown(c context.Context) {
os.Exit(1) os.Exit(1)
} }
func sleep(d time.Duration, c context.Context) { func sleep(d time.Duration, c context.Context) bool {
select { select {
case <-time.After(d): case <-time.After(d):
break return true
case <-c.Done(): case <-c.Done():
break return false
} }
} }

View File

@ -65,11 +65,8 @@ func scheduleNewTask(c context.Context, remote *OD) bool {
// Sleep if max number of tasks are active // Sleep if max number of tasks are active
for atomic.LoadInt32(&numActiveTasks) > config.Tasks { for atomic.LoadInt32(&numActiveTasks) > config.Tasks {
select { if !sleep(time.Second, c) {
case <-c.Done(): break
return false
case <-time.After(time.Second):
return true
} }
} }