Skip to content

Commit

Permalink
fix: load worker correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman committed Apr 24, 2024
1 parent 0a90a04 commit 5873700
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@accordproject/template-engine",
"version": "2.5.5",
"version": "2.5.6",
"description": "Generation of AgreementMark from TemplateMark + JSON Data",
"homepage": "https://github.com/accordproject",
"engines": {
Expand Down
13 changes: 12 additions & 1 deletion src/JavaScriptEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import child_process from 'child_process';
import jp from 'jsonpath';
import dayjs from 'dayjs';
import { EventEmitter } from 'node:events';
import path from 'path';

export type EvalOptions = {
timeout: number // timeout in milliseconds for this eval request
Expand Down Expand Up @@ -149,6 +150,16 @@ export class JavaScriptEvaluator {
});
}
}
private getWorkerPath() : string {
try {
const thisPath = require.resolve('@accordproject/template-engine');
return path.join(thisPath,'..','worker.js');
} catch(err) {
// if we cannot load the module it likely means we are running
// a unit test inside the module...
return './dist/worker.js';
}
}
private doWork(work: WorkItem, options: EvalOptions): Promise<EvalResponse> {
return new Promise((resolve, reject) => {
const start = new Date().getTime();
Expand All @@ -157,7 +168,7 @@ export class JavaScriptEvaluator {
reject({ message: 'Cannot use evalChildProcess because child_process.fork is not defined.' });
}
// on timeout will send SIGTERM
const worker = child_process.fork('./dist/worker.js', { timeout: options.timeout, env: {} });
const worker = child_process.fork(this.getWorkerPath(), { timeout: options.timeout, env: {} });
if (!worker.pid) {
throw new Error('Failed to fork child process');
}
Expand Down

0 comments on commit 5873700

Please sign in to comment.