 |
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
simpleq
Joined: 02 Mar 2009 Posts: 4 Country: United Kingdom |
|
Result code from within JavaScript? |
|
I am currently evaluating the 24x7 scheduler for managing some overnight batch jobs which are written in Java. I am planning to use the JavaScript functionality to enable the system.out stream to be written to a file, which seems to be working OK.
However, I need to also retrieve the result-code returned by the Java application so that I can raise an error and trigger "fail" scripts/programs to execute using dependencies. How can I achieve this, there doesn't seem to be a result-code parameter from the Process.runAndWait extension?
My script is shown below
 |
 |
// Parameters
var jobID = 2; // Export Job Identifier
var processDir = "D:\\datafeeds\\ENRGExport" // Process directory
var tempDir = "C:\\temp"
// Build the java command line
var strJavaCommand = "java DataFactory.Export.ENRG.ENRGExportMgr ID=" + jobID + " PROCESS_DIR=" + processDir;
// Execute the command
var timeout = 0;
var rwi = Process.runAndWait(strJavaCommand, timeout);
File.save(tempDir+"\\enrgOutput.txt",rwi.getOutput()); // Save the output
// Check the result code
if (rc!=0) { // <---- How do I get this???
Scheduler.raiseError(30, "**************ENRG export has failed**************");
} |
|
|
Mon Mar 02, 2009 4:14 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7953
|
|
|
|
I checked the documentation and didn't find direct way to get process exit code in JavaScript.
I suggest either adding a batch file to 24x7 install folder which will print the exit code of the process to standard output and which you can read from JavaScript, or using JAL/VBScript alternatives. Both JAL and VBScript support direct methods for getting the exit code of the launched process.
If you decide to use the batch file method, the following example demonstrates how to print the exit code
 |
 |
%1
echo Exit code: [%ERRORLEVEL%] |
Of course the command line from JavaScript job would be also a little bit different, for example,
 |
 |
strRunComnmand = "cmd /C runner.bat \"" + strJavaCommand + "\"";
... RunAndWait( strRunComnmand, .... |
and the exit code can be easily parsed out becasue it is enclosed in brackets
|
|
Mon Mar 02, 2009 7:46 pm |
|
 |
simpleq
Joined: 02 Mar 2009 Posts: 4 Country: United Kingdom |
|
|
|
Thank you for the helpful response, it works OK (with a minor amendment to the batch file to remove the double-quotes). It is a bit clunky but it works, maybe it could be considered for a future enhancement to make it easier.
Here is the final javascript, in case it is useful to anyone doing something similar...
 |
 |
// Parameters
var jobID = 2; // Job Identifier from IMPORT_EXPORT_SEQUENCE
var processDir = "D:\\datafeeds\\ENRGExport\\Process" // Process directory
var outputFile = "C:\\temp\\ENRGoutput.txt" // Command output file
// Build the java command line
var strJavaCommand = "java DataFactory.Export.ENRG.ENRGExportMgr"+
" ID=" + jobID +
" PROCESS_DIR=" + processDir;
// Execute the command
var timeout = 0;
var strBatchCommand = "cmd /C C:\\bin\\runbatch.bat \"" + strJavaCommand + "\"";
var rwi = Process.runAndWait(strBatchCommand, timeout);
// Save the output
var strOutput = rwi.getOutput();
if ((outputFile!=null) && (outputFile!="")) {
File.save(outputFile,strOutput); // Save the output
}
// Extract the Return Code from the output
var rc = -1;
var iPos = strOutput.indexOf("Exit code: [");
if (iPos>-1) {
iPos = strOutput.indexOf("[",iPos);
var iPos2 = strOutput.indexOf("]",iPos);
if (iPos2>-1) {
rc = strOutput.substring(iPos+1,iPos2);
}
}
// Check the result code
if (rc!=0) {
if (rc==4) {
Scheduler.logAddMessage("INFO", @V"job_id", "JOBNAME", "No ENRG Data to export");
} else {
Scheduler.raiseError(rc, "******** ENRG export has failed RC= "+rc+" ********");
}
} |
...and the batch file...
 |
 |
%~1
@echo Exit code: [%ERRORLEVEL%] |
|
|
Tue Mar 03, 2009 4:00 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7953
|
|
|
|
Thank you. I'm sure other people can find this solution helpful
|
|
Tue Mar 03, 2009 9:17 pm |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|