Question
I would like to interrupt a script when its running time exceeds a certain amount of time.
Is it possible to create a script like this?
Answer
You can implement a timeout in a script using Thread, Conditional Branch, and Wait operations.
Basic Structure
Run the main process and the timeout determination process in parallel by Thread operation, and interrupt the script if the main process running time exceeds a threshold.
Script flow
Process flow
- In Thread operation, run the main process and the timeout determination process in parallel.
- In the main process, Read CSV File operation reads a file and input "true" in a script variable as an end flag.
- In the timeout determination process, repeat the following process* as set in Loop operation.
- Wait operation waits for the specified time.
- Conditional Branch operation checks if the main process is completed and get out from the Loop operation if the main process is completed.
- After the Loop operation, another Conditional Branch operation checks if the main process is completed and interrupt the script by Raise Exception operation if the main process is not completed.
*For example, if you want to set one minute as a threshold, you can achieve this by repeating 12 seconds wait for five times.
How to implement
- Create script variables for the timeout determination process.
- Loop Count (Integer): set how many waits are repeated
- End Flag (Boolean): set "false" as default value - Put Thread operation from [Basic] > [Flow] on the script canvas.
- Put Read CSV File operation from [File] > [CSV] on the script canvas.
* Put any operation as your requirement. - Put Assign Variable operation from [Basic] > [Processing] on the script canvas.
- Open Mapper editor of Assign Variable and set "true" into the End Flag.
To do so, use Boolean Constant logic from [Condition] > [Boolean]. - Put Loop operation from [basic] > [Flow] on the script canvas.
For [Loop count], set the script variable Loop Count. - Put Conditional Branch operation from [Basic] > [Flow] on the script canvas.
As the condition, check if End Flag is equal to "true." - Put Break operation from [Basic] > [Flow] on the script canvas.
-
Put Wait operation from [Basic] > [Processing] on the script canvas.
Set the waiting time in required setting. - Put another Conditional Branch operation from [Basic] > [Flow] on the script canvas.
As the condition, check if End Flag is not equal to "true." - Put Raise Exception operation from [Basic] > [Flow] on the script canvas.
For [Message], set the text shown when the script times out. - Run the script to see if you can get the following result.
- If the main process completed within the time threshold set, the script finished successfully.
- If the main process did not complete within the time threshold set, the script interrupted.
Applications
Application 1: Determine timeout by date and time
- Prepare script variables for current date and time, and a threshold.
- Set and input the threshold right after the script starts.
e.g., an hour later from now - In the timeout determination process, compare current time and the threshold at a certain interval, shown as the Wait operation in the image, and proceed Raise Exception if the current time exceeds the threshold.
If the main process has not been completed, repeat the determination process again.
Application 2: Execute other operations when the main script times out
- Create another script which calls the main script containing the timeout process.
- In Conditional Branch operation, draw process flow as executing other operations, shown as the Wait operation in the image, when the called script timed out.
If you have any other question, please contact to HULFT Square Technical Support Center.
Comments
0 comments
Article is closed for comments.