Chapter 4. Monitoring, Multithreading, Scheduling and other Advanced Techniques

4. Monitoring, Multithreading, Scheduling and other Advanced Techniques

In this chapter we are covering several advanced scripting techniques such as how to monitor script executions (and possibly stop the execution of a script), how to work with multiple threads, how to use the registry to temporarily or persistently store values and how to schedule scripts. Let us start by looking at how ReportServer executes scripts via the terminal.

4.1. Execution of Scripts

Whenever you execute a script via the terminal command exec the script execution is wrapped into a new thread. This allows to monitor the execution and if necessary stop the script. You can display a list of the currently running scripts via the terminal command ps.

The following script doesn't do much, but it needs almost one minute for it:

import java.lang.Thread

Thread.sleep(60000)

"awake again"

If you run this script, the Terminal window will remain inactive during the time of execution. You can either open a second terminal window (CTRL+ALT+T), or wait for the script to return and then run it using the silent flag:

exec -s sleep.groovy

The silent flag tells ReportServer to ignore the output of the script and run it in the background.

Now, by using the command "ps" you can view executions which are presently active.

reportserver$ ps
ID		Date							User			Command							Thread Interrupted
1			03.05.13 16:23		3					exec -s sleep.groovy		false

By entering the kill command, you can cancel scripts. Here, first an interrupt will be sent to the script which in our case leads to sending the thread an interrupt. In our case we can interrupt the script as follows.

reportserver$ kill 1

When you call "ps" again you will see that the script was interrupted indeed. The following script, however, will not be terminated that easily. It catches any exception and thus also exceptions thrown on interrupt.

import java.lang.Thread

def slept = false;
while(! slept){
	try{
		Thread.sleep(60000)
      		slept = true;
  	} catch(all) {
  	}
}
"done"

If you run this script and try to terminate it using "kill ID", you will see that the script is still listed by the "ps" command. Note that the flag "interrupted" is now set.

reportserver$ ps
ID		Date						User			Command							Thread Interrupted
7	03.05.13 16:23			3					exec -s sleep.groovy		true

To hard-terminate the execution you can enter kill -f ID. This will terminate the thread by Thread.stop(). Please keep in mind that this may have undesirable side effects. You will find a description of the Thread.stop() method and related issues under http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html.

Note that scripts which are executed not via the terminal but, for example, during a script report execution or via the scheduler will not run in an extra thread. They will, thus, also not be listed by the ps command.

4.1.1. Starting scripts in the server thread

In some situations it might be helpful to avoid starting a script in an own thread, but to start it in the server thread. This means, however, that this script cannot be monitored and interrupted by "ps/kill". To start a script without an own thread use the -n flag.