I have a script in my project library which I'm calling it whenever the session is launched.
That script takes some time for execution.
I want to break the script and navigate the user to a different URL if the script takes more than 10s in execution
import threading
import time
import system
Check = True
def break_script():
global Check
system.perspective.print("Script breaking after 10 seconds...")
Check = False
def TestScript():
global Check
try:
timer = threading.Timer(10, TestExitScript.break_script)
timer.start()
system.perspective.print(session.props.id)
while Check:
system.perspective.print("Running main script code...")
time.sleep(1)
except SystemExit:
system.perspective.print("Script was interrupted.")
Check = False
Why does the script take so long to execute? Does it have to be done at start-up?
Anytime you're using time.sleep() in Ignition you are probably doing something wrong, and need to take a step back and ask what problem you're really trying to solve.
Not related but don't think you ever need to import system anymore.
I would show the main script you are trying to run that is take over 10 seconds long. There are probably better ways to handle things as @lrose mentioned.
Does the script iterate over something in a loop? Or does it call some function that just takes a while?
If it is the former, then inside that loop you could check how long the script has been taking and break out if it's been longer than 10s.
def longScript():
startOfScript = system.date.now()
while longTime:
# Do stuff
# Check execution time
now = system.date.now()
if system.date.secondsBetween(startOfScript, now) > 10:
break
Since you cannot share your script, we can only provide general advice, like this:
Java doesn't have a safe mechanism to kill threads, which means jython doesn't either. The closest approximation is java's Thread.interrupt() mechanism, but that requires in-thread checking in many cases. (I/O operations will throw exceptions on interruptions. Not much else responds to interruption.) Use something like Daniel's pseudocode to have a long task exit early.
Using jython's standard library for any timing, sleeping, or calendar operations is buggy and generally doesn't play well with the rest of Ignition. Use java or Ignition-supplied libraries, not the jython stdlib.
A ten-second workload triggered by a session start event (which happens before authentication) is denial of service attack waiting to be exploited. Don't do this. Any script that a session can trigger prior to login should execute extremely quickly.
Let me give a brief about the script.
I am running a script on session start which queries the data from the database, does some operation over it and stores the final value on the session.custom.
I have observed that the startup script takes more time, only sometimes. I want to break the script and navigate the user to other view when the script takes more the x seconds (let's take 10s for eg.)
Is the data truly specific to the Session? Meaning that each session could have potentially different data?
If you are already good with navigating them to a different view, why not just navigate them there from the start.
Instead of session start up, use a page start-up event of your home page, to initiate an asynchronous task to gather your data. No need to "break out" to navigate, and the user doesn't know that some task in the background is processing.
If the user needs that data before they can do any work then display a loading icon of some type to let them know the system is working on retrieving the data.
if the data is not client specifief you can use a tag to poll for it every hour or somethin (NOT TO OFTEN IF IT TAKES LONG TO COMPLETE)
that way everyone will see the same data, and if you have a lot of clients it will reduce the load a lot
It seems very weird that you need to run such long queries for every client seperatly... And that it apperently doesnt matter if it fails that you can just kill the thread without doing anything else