I have a background task that updates its status (% complete) to the db. Then the app polls the db to update a progress bar. The problem is I'm not getting the updated value from the db, even though I'm doing save(flush:true). So right now, my progress bar never gets updated and I'm stuck in an endless loop, since my percent complete is stuck at 0. What am I doing wrong?
def backgroundAction = {
println "backgroundAction"
def duration = theEvent?.duration ?: 10
def name = theEvent?.name ?: "none"
def percentComplete = theEvent?.percentComplete ?: 0
def lastPct = -1
runAsync {
toEvent(name,duration,percentComplete,false)
}
while(percentComplete < 100) {
println "percentComplete:${percentComplete}"
if (percentComplete != lastPct ) {
progressService.setProgressBarValue(name, percentComplete)
lastPct = percentComplete
}
percentComplete = theEvent.percentComplete
}
render "the progress is done"
}
def toEvent(name,duration,startAt,updateBar) {
println "duration:${duration}"
println "name:${name}"
println "startat:${startAt}"
for (int i = startAt; i <= 100; i++) {
println "i:${i}"
if(updateBar){
progressService.setProgressBarValue(name, i)
} else {
def theEvent = Event.findByName(name)
theEvent.percentComplete = i
theEvent.save(flush:true)
println "theEvent.percentComplete:${theEvent.percentComplete}"
}
//let's waste some time
for (int a = 0; a < duration; a++) {
for (int b = 0; b < 1000; b++) {
}
}
}
}