Adding a small delay into a while loop for stability.

Discuss your general Proclaim related queries here.
Mark_ACA
Posts: 17
Joined: Mon Aug 09, 2021 10:58 am
Has thanked: 6 times

Adding a small delay into a while loop for stability.

Post by Mark_ACA »

So I want to do a while loop in a linked action to import some xml data generated by an external program...

WHILE {XMLResponseStatus.Text} = "Waiting" DO
IMPORT
UPDATE( {case.key} , "" )
END

The import Job will change the field from Waiting to either Done or Fail.

It works but it can sometimes make the external application producing the XML files really laggy and sometimes fail with this error.

"communication buffer resources with another process and has been chosen as the deadlock victim"
I just want to add a 1-2 second delay it the loop so its not constantly trying to run the import job, it runs then waits in a loop instead until its been successful. So other applications accessing these files get the gap to do it.

What's the best way ? I can pop up message box for the user to click but they have to keep hitting close then until it comes through. Its not very intuitive.
How can I just put a delay into the while loop ?

Mark_ACA
Posts: 17
Joined: Mon Aug 09, 2021 10:58 am
Has thanked: 6 times

Re: Adding a small delay into a while loop for stability.

Post by Mark_ACA »

forgot to add these variables are setup earlier in the LA ..

importTemplate = "ACG_cr_s"
importFilename = "\\server\CR_Status\" + {case.key} + ".xml"

steve
Posts: 474
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 121 times

Re: Adding a small delay into a while loop for stability.

Post by steve »

Hi,
you could put this code in a maths item, then call it every time you want a delay of (2) seconds

Code: Select all

# Delay in seconds:
v-delay = 2 
#

v-timenow = TIME-SECONDS 
v-end = v-timenow + v-delay 
WHILE TIME-SECONDS < v-end DO 
#loop 
#need to actually do something or free up resource here? 
END
let me know if it works

Mark_ACA
Posts: 17
Joined: Mon Aug 09, 2021 10:58 am
Has thanked: 6 times

Re: Adding a small delay into a while loop for stability.

Post by Mark_ACA »

Hi Steve, this is really good thanks, I will just insert the code inside the loop for now.

Do I make a new maths field ? then how do I call that ? just try and put it somewhere like NUL ?

Still fairly new to coding in proclaim.
Thanks :)

Mark_ACA
Posts: 17
Joined: Mon Aug 09, 2021 10:58 am
Has thanked: 6 times

Re: Adding a small delay into a while loop for stability.

Post by Mark_ACA »

I see how it will work, during that loop it worn be running the import job. Thanks :)

steve
Posts: 474
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 121 times

Re: Adding a small delay into a while loop for stability.

Post by steve »

great, sounds like you've got it sorted.

you could make a universal 'delay' maths item for (re)use anywhere you like:

e.g. make a new Number field in DB maint {Delay number of seconds.Number}
then make a new Maths field in DB maint e.g. {M Delay routine.Text}

Code: Select all

# Delay in seconds:
v-delay = {Delay number of seconds.Number}
v-timenow = TIME-SECONDS 
v-end = v-timenow + v-delay 
WHILE TIME-SECONDS < v-end DO 
#nop
END
Then, in any place you want to have a delay, just add the mahs field:

Code: Select all

## Set up our delay function.
PUT (2, {delay number of seconds.Number})
## Perform initial import job.
IMPORT
## If XML import is not complete, status will return Waiting. If so, delay 2 seconds then call again until complete.
WHILE {XMLResponseStatus.Text} = "Waiting" DO
   ## Wait 2 seconds.
   vrun = {M Delay routine.Text}
   ## Run the import. Re-populate XMLResponseStatus.Text.
   IMPORT
   UPDATE( {case.key} , "" )
END

My fear was it would just hog the CPU whilst in the delay loop but it seems happy enough with a quick test, but you might want to just kick the tyres on it first.