Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Resources

Display children header

Links

Display content by label
Labelsjs7 api javascript
OtherTitleHow To ... JavaScript

Table of Contents

Introduction

  • Node.js® is an open-source, cross-platform JavaScript runtime environment and is a frequently used scripting language available for Linux, MacOS, Windows, and other platforms.
  • This article explains how to syntactically include Node.js® scripts with JS7 job scripts.
  • You want to Execute the Node.js code using the JS7 shell job script. First, you need to install Node.js on your server, where you are installing the Agent for Linux, MacOS, Windows, and other platforms.

Unix

  • Find the below examples for download (.json upload)run-Node.js-unix.workflow.json
  • In order to directly run Node.js® script code from a JS7 shell job script, the recommended approach is to use a shebang that can be invoked directly and can be parameterized like this:

    Code Block
    languagejs
    titleExample how run Node.js® script code with a shebang
    linenumberstrue
    #!/usr/bin/node
    
    var name =(process.env.name);
    var num = parseInt(process.env.num);
    
    //print value of variable   
    console.log( name);
    console.log( num);
  • As a another alternative, the Node.js® script can be executed from a file that has to be located by the executing Agent:
Code Block
languagejs
titleExample how to run Node.js® script code from a file
linenumberstrue
node  /home/{User}/JS7-2.5.1/demo_Node.js

Windows

...

In order to directly run Node.js® script code from a JS7 shell job script, the recommended approach is to use a shebang that can be invoked directly and can be parameterized like this:

Code Block
languagejs
titleExample how run PowerShell® script code with a shebang replacement
linenumberstrue
@@findstr/v "^@@f.*&" "%~f0"|node.exe -&goto:eof

var name =(process.env.name);
var num = parseInt(process.env.num);

//print value of variable   
console.log( name);
console.log( num);

Explanation:

...

e.g. @@findstr/v "^@@f.*&" "%~f0"|node.exe -&goto:eof

...

Last but not least a Node.js® script can be executed from a file that has to be located by the executing Agent:

Code Block
languagejs
titleExample how to run Node.js® script code from a file
linenumberstrue
node.exe C:\Users\Documents\demo_Node.js

To Pass Variables to subsequent jobs 

Users frequently find a situation when a job creates a result which should be forwarded to subsequent jobs in a workflow.

Shell Jobs

Download Example for Unix (.json upload): pdwVariablesPassingUnix.workflow.json

Download Example for Windows (.json upload): pdwVariablesPassingWindows.workflow.json

First Job: Write Variables

Shell jobs can pass results to subsequent jobs:

  • by creating a key/value pair with the syntax: key=value.
  • The key/value pair is appended to a temporary file which is provided by JS7 and that is indicated by the JS7_RETURN_VALUES environment variable.
  • The key provided is the name of the order variable which can be used by subsequent jobs.
    • If the variable does not yet exist it will be created on-the-fly.
    • If the variable exists then the value will be overwritten

The job script implementation looks like this:

Code Block
languagejs
titleExample of a Unix Shell job passing variables
linenumberstrue
#!/usr/bin/node
 
const fs = require('fs');
 
//fetch the env. variables
var name = (process.env.name);
var num = parseInt(process.env.num);
 
//print value of variable   
console.log(name);
console.log( num);

//Modify the values
var num1 = num + num;
console.log( num1);

var name1 = name + " This is JS7 ";
console.log( name1);

//pass results from a key/value pair that is appended to a temporary file provided by JS7

fs.appendFile(process.env.JS7_RETURN_VALUES ,'num1='+num1+'\n',(err) => {});
fs.appendFile(process.env.JS7_RETURN_VALUES ,'name1='+name1+'\n',(err) => {});
Code Block
languagejs
titleExample of a Windows Shell job passing variables
linenumberstrue
@@findstr/v "^@@f.*&" "%~f0"|node.exe -&goto:eof

const fs = require('fs');
 
//fetch the env. variables
var name = (process.env.name);
var num = parseInt(process.env.num);
 
//print value of variable   
console.log(name);
console.log( num);

//Modify the values
var num1 = num + num;
console.log( num1);

var name1 = name + " This is JS7 ";
console.log( name1);

//pass results from a key/value pair that is appended to a temporary file provided by JS7

fs.appendFile(process.env.JS7_RETURN_VALUES ,'num1='+num1+'\n',(err) => {});
fs.appendFile(process.env.JS7_RETURN_VALUES ,'name1='+name1+'\n',(err) => {});

Second Job: Read Variables

Shell jobs access order variables and order variables from a mapping to environment variables.

  • The JOC Cockpit GUI allows the mapping to be added per job from the right lower corner with the Environment Variables sub-tab.
  • The mapping includes free choice of the name of an environment variable which is used in the job script and to assign an existing order variable.
  • The spelling of variable names is case-sensitive.

Image Removed

The job script implementation looks like this:

Code Block
languagejs
titleExample of a Unix Shell job reading variables
linenumberstrue
#!/usr/bin/node

var name1 =(process.env.name1);
var num1 = parseInt(process.env.num1);

//print value of variable   
console.log(name1);
console.log( num1);

...

languagejs
titleExample of a Windows Shell job reading variables
linenumberstrue

...