Home | Table of Contents | Execute Groovy Code | CloverETL 4.6.1
--- | --- | ---
Prev | Tasks | Next

## Execute Groovy Code

This type of task allows execute code written in script language Groovy. The script can be defined in place or using a path to external `.groovy` file. It is possible to use some variables.

Basic attribute of this task is source code of written in Groovy.

If source codes are provided from both a file and through the input form only the code from the input form will be executed.

In cluster environment there is also one additional attribute "Node IDs to process the task". If it's empty, it may be any node, if there are nodes specified, the task will be processed on the first node which is online and ready.

CloverETL Server contains Groovy version 2.0.0

Table 20.15. List of variables available in Groovy code

variable | class | description | availability
--- | --- | --- | ---
event | com.cloveretl.server.events.AbstractServerEvent |  | every time
task | com.cloveretl.server. persistent.Task |  | every time
now | java.util.Date | current time | every time
parameters | java.util.Properties | Properties of a task | every time
user | com.cloveretl.server. persistent.User | Same as event.getUser() | every time
run | com.cloveretl.server. persistent.RunRecord |  | When the event is instance of GraphServerEvent
tracking | com.cloveretl.server. persistent.TrackingGraph | same as run.getTrackingGraph() | When the event is instance of GraphServerEvent
sandbox | com.cloveretl.server. persistent.Sandbox | same as run.getSandbox() | When the event is instance of GraphServerEvent
schedule | com.cloveretl.server. persistent.Schedule | same as ((ScheduleServerEvent)event). getSchedule() | When the event is instance of ScheduleServerEvent
servletContext | javax.servlet.ServletContext |  | every time
cloverConfiguration | com.cloveretl.server.spring. CloverConfiguration | Configuration values for CloverETL Server | every time
serverFacade | com.cloveretl.server.facade. api.ServerFacade | Reference to the facade interface. Useful for calling CloverETL Server core. WAR file contains JavaDoc of facade API and it is accessible on URL: http://host:port/clover/javadoc/index.html | every time
sessionToken | String | Valid session token of the user who owns the event. It is useful for authorisation to the facade interface. | every time

Variables run, tracking and sandbox are available only if event is instance of GraphServerEvent class. Variable schedule is only available for ScheduleServerEvent as event variable class.

### Example of use Groovy script

This example shows a script which writes text file describing the finished graph. It shows use of 'run' variable.

    import com.cloveretl.server.persistent.RunRecord;
    String dir = "/tmp/";
    RunRecord rr = (RunRecord)run;

    String fileName = "report"+rr.getId()+"_finished.txt";

    FileWriter fw = new FileWriter(new File(dir+fileName));
    fw.write("Run ID       :"+rr.getId()+"\n");
    fw.write("Graph ID     :"+rr.getGraphId()+"\n");
    fw.write("Sandbox      :"+rr.getSandbox().getName()+"\n");
    fw.write("\n");
    fw.write("Start time   :"+rr.getStartTime()+"\n");
    fw.write("Stop time    :"+rr.getStopTime()+"\n");
    fw.write("Duration     :"+rr.getDurationString()+"\n");
    fw.write("Status 	   :"+rr.getStatus()+"\n");
    fw.close();

Prev | Up | Next
Send a JMS Message | Home | Table of Contents | Manual Task Execution