Force Project Scripts Restart

In our system we use system.util.invokeAsynchronous() to run a number of state machines in a dedicated thread. At times some of the functions running in this thread can fault and stop working while the remaining functions will continue working. We have a mechanism to stop and restart the state machines and dedicated thread, but this does not always work to bring them all back to a working state. In such cases, we have found to get everything working again we get the Designer to think there are changes to the Gateway Event Scripts by opening the Gateway Event Script dialog, pressing the Apply button, and then closing the dialog. We then save and publish the Project from the Designer which will go through the Shutdown Script and then the Startup script. This typically will resolve any issues and all the state machines will function normally. We have also seen this resolve issues with Gateway Timer Scripts that have stopped responding. We would like to be able to accomplish this via scripting inside a button or a timer script so we can do this automatically when we detect this is needed without needing to have access to a Designer. Is there any way to do this? I have tried system.util.retarget() back to the same project, but it does not seem to restart the project scripts.

Thanks

You are treading on dangerous ground… restarting scripts like that doesn’t actually kill off background threads. Which can cause memory leaks and/or unexpected operation of old code. You really – really – need to wrap the different pieces of code in try … except … blocks to capture the causes of your problems. You should also use the dictionary from system.util.getGlobals() to keep a weak reference to your thread – then not let a new thread start until the prior thread is known to be dead.

Consider using a timer script instead of an asynchronous thread to run your state machine, with the state information held in a script module global variable. That will always run the correct code, and will have empty or freshly initialized state information after any scripting restart. Again, use system.util.getGlobals() to hold information that should persist in spite of script restarts.