Waiting in a Gateway Script

Hello - I’m looking for best practices for looping in a gateway script without impacting system performance. I am working with a vendor API implementation that looks a bit like this:
Get Status from API (should be ‘IDLE’)
Send Input Data to API
Poll Status from API until it is ‘IDLE’
Retrieve Output Data from API

Is there a better option than simply using a while loop? I looked at the ‘Adding a Delay to a Script’ help page, but none of the options besides a while loop or sleep allowed me to use the polling feedback as a conditional break out.

You can set up a gateway timer script to run and do this every X seconds. Or used a scheduled script to set it on a cron job. In a way Ignition takes care of the while loop, you just need to write the inside of your loop and tell Ignition how often you want to run it.

The approaches @bkarabinchak.psi mention are good ones. I’ll supplement that if you might need a more dynamic “sleep” period, I’ve had good luck in the past using something like a timer script that defines your minimum frequency for checking to see if it is time to do work. You could do this by maintaining a NextActionTime tag that contains the timestamp after which to do work. Each invocation of the timer script you compare and see if current time exceeds NextActionTime and then set NextActionTime (to the next time) and do your current work. This helps you prevent a blocking “sleep”. It also gives you a handy way to debug by viewing that tag.

2 Likes

Thanks both. I was hoping to avoid using tags at all for managing the state, but it seems like I will need to.

If you get an instance of GatewayContext (search the forums, I’m being deliberately vague here), you can get the shared ExecutionManager, which allows you to register a SelfSchedulingRunnable which has the same capability Kevin described built in. It would require some moderately advanced scripting know-how, but your scheduled task would also show up on Status → Execution.

4 Likes