Commit aabf3559 authored by Kyle Clarke's avatar Kyle Clarke 💬

Flappy test highlighted issue around infinite for loop and channel stops....

Flappy test highlighted issue around infinite for loop and channel stops. Added mutex lock to and additional stop check. Fixed.
parent 950ffbd5
Pipeline #5969398 passed with stages
in 36 seconds
......@@ -3,6 +3,7 @@ package kevin
import (
"math"
"reflect"
"sync"
"time"
)
......@@ -49,6 +50,7 @@ type Runner struct {
stop chan struct{}
stopped bool
Status string
sync.Mutex
}
// Given a Job, use reflection to create the func and func params to
......@@ -130,6 +132,8 @@ func (r *Runner) IsRunning() bool {
// IsStopped returns this runners current stop flag value.
func (r *Runner) IsStopped() bool {
r.Lock()
defer r.Unlock()
return r.stopped
}
......@@ -145,11 +149,13 @@ func (r *Runner) Run() {
// Stop will stop execution of the ticker and assign the correct stopped status.
func (r *Runner) Stop() {
r.Lock()
defer r.Unlock()
r.stopped = true
r.Status = StatusStopped
if r.stop != nil {
close(r.stop)
}
r.stopped = true
r.Status = StatusStopped
}
// do is called via Run in a go routine.
......@@ -170,6 +176,12 @@ func do(r *Runner) {
for {
select {
case <-t.C:
// Only continue processing if this job is not stopped.
if r.IsStopped() {
return
}
// If we have a start time, skip this tick if time not passed.
if !r.CanBegin() {
r.Status = StatusCannotBegin
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment