Skip to content

Commit

Permalink
Merge branch 'main' into engine/monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeTrost authored Nov 23, 2024
2 parents 45c0143 + acd8f3f commit 49e7957
Show file tree
Hide file tree
Showing 75 changed files with 3,353 additions and 1,697 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dev-ms-old-iam": "cd src/management-system && yarn web:dev-iam",
"dev-web": "yarn build && cd src/engine/native/web/server && yarn serve",
"dev-many": "node src/engine/e2e_tests/process/deployment/startEngines.js",
"build": "cd src/engine/native/node && webpack --config webpack.native.config.js && webpack --config webpack.injector.config.ts && ts-node ./build-injector.js && cd ../../universal && NODE_OPTIONS=--openssl-legacy-provider webpack --config webpack.universal.config.js",
"build": "cd src/engine/native/node && webpack --config webpack.native.config.js && webpack --config webpack.injector.config.ts && cd ../../universal && NODE_OPTIONS=--openssl-legacy-provider webpack --config webpack.universal.config.js",
"build-ms": "cd src/management-system-v2 && yarn build",
"build-web": "yarn build && cd src/engine/native/web/server && yarn build",
"docker:run": "docker container run --publish 33029:33029 --rm --network host --detach --name engine proceed/engine:latest",
Expand Down Expand Up @@ -116,7 +116,8 @@
"uuid": "9.0.1",
"webpack": "^4.35.3",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
"webpack-dev-server": "^3.2.1",
"node-loader": "^1.0.3"
},
"dependencies": {
"react-resizable": "^3.0.5"
Expand Down
2 changes: 2 additions & 0 deletions src/engine/e2e_tests/process/deployment/mockEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const MockMachine = require('./mockNativeMachine.js');
const Nmdns = require('@proceed/native-mdns');
const Ncapabilities = require('@proceed/native-capabilities');
const Nconsole = require('@proceed/native-console');
const ChildProcessExecutor = require('@proceed/native-script-execution');

native.registerModule(new Nfs({ dir: process.argv[2] }));
native.registerModule(new Nexpress());
Expand All @@ -14,5 +15,6 @@ native.registerModule(new MockMachine(process.argv[2]));
native.registerModule(new Nmdns());
native.registerModule(new Ncapabilities());
native.registerModule(new Nconsole());
native.registerModule(new ChildProcessExecutor());

native.startEngine({ childProcess: false });
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<incoming>Flow_1lwdqo3</incoming>
<outgoing>Flow_1j1u3ad</outgoing>
<script>
return new Promise(resolve => setTimeout(resolve, 3000));
return new Promise(resolve => setTimeoutAsync(resolve, 3000));
</script>
</scriptTask>
</process>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<incoming>Flow_1pzlp7r</incoming>
<outgoing>Flow_0a94qz4</outgoing>
<script>
return new Promise(resolve => setTimeout(resolve, 1000));
return new Promise(resolve => setTimeoutAsync(resolve, 1000));
</script>
</scriptTask>
</process>
Expand Down Expand Up @@ -97,4 +97,4 @@
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
</definitions>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
<scriptTask id="Activity_024hiuu" scriptFormat="application/javascript">
<incoming>Flow_0xsb6pm</incoming>
<outgoing>Flow_0ijy7a4</outgoing>
<script>return new Promise(resolve => setTimeout(resolve, 2000))</script>
<script>
return new Promise(res => setTimeoutAsync(res, 2_000));
</script>
</scriptTask>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<scriptTask id="Activity_1alt9t5" scriptFormat="application/javascript">
<incoming>Flow_03qmegv</incoming>
<outgoing>Flow_1kq2fh1</outgoing>
<script>return new Promise(resolve => setTimeout(resolve, 3000))</script>
<script>return new Promise(resolve => setTimeoutAsync(resolve, 3000))</script>
</scriptTask>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<incoming>Flow_0n4ueli</incoming>
<outgoing>Flow_1t4tcmh</outgoing>
<script>
return new Promise(resolve => setTimeout(resolve, 2000));
return new Promise(res => setTimeoutAsync(res, 2000));
</script>
</scriptTask>
</process>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<incoming>Flow_1sk1w7g</incoming>
<outgoing>Flow_0zsnmut</outgoing>
<script>
return new Promise(resolve => setTimeout(resolve, 2000));
return new Promise(resolve => setTimeoutAsync(resolve, 2000));
</script>
</scriptTask>
</Process>
Expand Down
18 changes: 0 additions & 18 deletions src/engine/native/node/build-injector.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/engine/native/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Nmachine = require('@proceed/native-machine');
const Nmdns = require('@proceed/native-mdns');
const Ncapabilities = require('@proceed/native-capabilities');
const Nconsole = require('@proceed/native-console');
const Nvm2 = require('@proceed/native-vm2');
const NScriptExecution = require('@proceed/native-script-execution');
const NMQTT = require('@proceed/native-mqtt');

native.registerModule(new Nfs());
Expand All @@ -16,7 +16,7 @@ native.registerModule(new Nmachine());
native.registerModule(new Nmdns());
native.registerModule(new Ncapabilities());
native.registerModule(new Nconsole());
native.registerModule(new Nvm2());
native.registerModule(new NScriptExecution());
native.registerModule(new NMQTT());

native.startEngine({ childProcess: false });
1 change: 1 addition & 0 deletions src/engine/native/node/native-express/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class NativeExpress extends NativeModule {
query: req.query,
path: req.path,
body: req.body,
headers: req.headers,
files,
};

Expand Down
12 changes: 12 additions & 0 deletions src/engine/native/node/native-script-execution/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@proceed/native-script-execution",
"version": "1.0.0",
"description": "Native module that executes scripts in a subprocess",
"main": "src/index.js",
"author": "PROCEED Project",
"license": "MIT",
"dependencies": {
"@proceed/native-module": "^1.0.0",
"isolated-vm": "^5.0.1"
}
}
172 changes: 172 additions & 0 deletions src/engine/native/node/native-script-execution/src/childProcess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
const ivm = require('isolated-vm');

const [
processId,
processInstanceId,
scriptIdentifier,
tokenId,
scriptString,
token,
processCommunicationAdress,
] = process.argv.slice(2);

if (
!processId ||
!processInstanceId ||
!scriptIdentifier ||
!tokenId ||
!scriptString ||
!token ||
!processCommunicationAdress
)
throw new Error(
'Expected the following args processId, processInstanceId, tokenId, scriptString, token',
);

async function callToExecutor(endpoint, body) {
try {
const response = await fetch(
`${processCommunicationAdress}/${processId}/${processInstanceId}/${scriptIdentifier}/${endpoint}`,
{
method: 'POST',
body: JSON.stringify(body),
headers: {
['content-type']: 'application/json',
authorization: `Bearer ${token}`,
},
},
);

if (!response.ok) throw new Error(`${endpoint}: Response not ok`);

const contentType = response.headers.get('content-type');

if (!contentType) {
return;
} else if (contentType.includes('application/json')) {
const json = await response.json();
return 'result' in json ? json.result : json;
} else {
return await response.text();
}
} catch (e) {
console.error(e);

if (endpoint === 'result') process.exit(1);
}
}

const isolate = new ivm.Isolate({ memoryLimit: 128 });
const context = isolate.createContextSync();

context.global.setSync('global', context.global.derefInto());

context.global.setSync('_stdout_log', function (...args) {
console.log(...args);
});

// NOTE: Extern capabilities (provided by neo-engine)
// log, console, variable, getService, BpmnEscalation, BpmnError
// TODO: pass these in as a process argument

// TODO: setProgress(<number between 0 - 100>)
// TODO: getService('capabilities')
// TODO: getService('network')

const structure = {
log: ['get'],
console: ['trace', 'debug', 'info', 'warn', 'error', 'log', 'time', 'timeEnd'],
variable: ['get', 'set', 'getAll'],
};

for (const objName of Object.keys(structure)) {
const functionNames = structure[objName];

context.evalSync(`globalThis["${objName}"] = {}`);

// NOTE: maybe replace JSON for isolated-vm solution
for (const functionName of functionNames) {
context.evalClosureSync(
`globalThis["${objName}"]["${functionName}"] = function (...args) {
return $0.applySyncPromise(null, [JSON.stringify(args)], {}).copyInto();
}`,
[
new ivm.Reference(async function (args) {
const result = await callToExecutor('call', {
functionName: `${objName}.${functionName}`,
args: JSON.parse(args),
});

return new ivm.ExternalCopy(result);
}),
],
);
}
}

const errorClasses = ['BpmnError', 'BpmnEscalation'];

for (const errorName of errorClasses)
context.evalClosureSync(
`class ${errorName} {
constructor(...args) {
this.errorArgs = args;
this.errorClass = '${errorName}';
}
}
globalThis["${errorName}"] = ${errorName};
`,
);

function wrapScriptWithErrorHandling(script) {
return `try {
${script}
} catch(e){
if(${errorClasses.map((error) => 'e instanceof ' + error).join(' || ')})
throw JSON.stringify(e);
else throw e;
}
`;
}

const sleep = new ivm.Reference((ms) => new Promise((res) => setTimeout(res, ms)));

context.evalClosureSync(
async function setTimeoutAsync(cb, ms) {
await $0.apply(null, [ms], { result: { promise: true } });
cb();
}.toString() + `globalThis["setTimeoutAsync"] = setTimeoutAsync;`,
[sleep],
);

context.evalClosureSync(
async function setIntervalAsync(cb, ms) {
do {
await $0.apply(null, [ms], { result: { promise: true } });
} while (!(await cb()));
}.toString() + 'globalThis["setIntervalAsync"]=setIntervalAsync;',
[sleep],
);

function wrapScriptInAsyncFunction(script) {
return `async function main() { ${script} }; main();`;
}

context
.eval(wrapScriptInAsyncFunction(wrapScriptWithErrorHandling(scriptString)), {
promise: true,
externalCopy: true,
})
.then((result) => {
callToExecutor('result', { result: result.copy() });
})
.catch((err) => {
let result = err;
try {
result = JSON.parse(err);
} catch (_) {}

console.error(result);

callToExecutor('result', { result }).finally(() => process.exit(1));
});
Loading

0 comments on commit 49e7957

Please sign in to comment.