Skip to content

Commit

Permalink
log instead of throw on duration or date parse failure
Browse files Browse the repository at this point in the history
  • Loading branch information
paed01 committed Jan 11, 2021
1 parent 9c7188f commit 83c85d3
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 144 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
Changelog
=========

# 4.3.1

- Stop throwing errors when failing to parse `timeDuration` or `timeDate` as it was before and still should've been before someone changed it

# 4.3.0

Timetracking
Expand Down
134 changes: 67 additions & 67 deletions dist/src/eventDefinitions/TimerEventDefinition.js
Expand Up @@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
});
exports.default = TimerEventDefinition;

var _Errors = require("../error/Errors");

var _messageHelper = require("../messageHelper");

var _iso8601Duration = require("iso8601-duration");
Expand Down Expand Up @@ -71,18 +69,7 @@ function TimerEventDefinition(activity, eventDefinition) {

const messageContent = executeMessage.content;
const startedAt = 'startedAt' in messageContent ? new Date(messageContent.startedAt) : new Date();

try {
var resolvedTimer = getTimers(foundTimers, environment, executeMessage); // eslint-disable-line no-var
} catch (err) {
logger.error(`<${executionId} (${id})>`, err);
return broker.publish('execution', 'execute.error', (0, _messageHelper.cloneContent)(messageContent, {
error: new _Errors.ActivityError(err.message, executeMessage, err)
}, {
mandatory: true
}));
}

const resolvedTimer = getTimers(foundTimers, executeMessage);
const timerContent = (0, _messageHelper.cloneContent)(messageContent, { ...resolvedTimer,
...(isResumed ? {
isResumed
Expand Down Expand Up @@ -194,72 +181,85 @@ function TimerEventDefinition(activity, eventDefinition) {
broker.cancel(`_api-delegated-${executionId}`);
}
}
}

function getTimers(timers, environment, executionMessage) {
const content = executionMessage.content;
let expireAt;
function getTimers(timers, executionMessage) {
const content = executionMessage.content;
let expireAt;

if ('expireAt' in content) {
expireAt = new Date(content.expireAt);
}
if ('expireAt' in content) {
expireAt = new Date(content.expireAt);
}

const now = Date.now();
const timerContent = ['timeDuration', 'timeDate', 'timeCycle'].reduce((result, t) => {
if (t in content) result[t] = content[t];else if (t in timers) result[t] = environment.resolveExpression(timers[t], executionMessage);else return result;
let expireAtDate;

const now = Date.now();
const timerContent = ['timeDuration', 'timeDate', 'timeCycle'].reduce((result, t) => {
if (t in content) result[t] = content[t];else if (t in timers) result[t] = environment.resolveExpression(timers[t], executionMessage);else return result;
let expireAtDate;

switch (t) {
case 'timeDuration':
{
const durationStr = result[t];

if (durationStr) {
const delay = (0, _iso8601Duration.toSeconds)((0, _iso8601Duration.parse)(durationStr)) * 1000;
expireAtDate = new Date(now + delay);
} else {
expireAtDate = new Date(now);
switch (t) {
case 'timeDuration':
{
const durationStr = result[t];

if (durationStr) {
const delay = getDurationInMilliseconds(durationStr);
if (delay !== undefined) expireAtDate = new Date(now + delay);
} else {
expireAtDate = new Date(now);
}

break;
}

break;
}
case 'timeDate':
{
const dateStr = result[t];

if (dateStr) {
const ms = Date.parse(dateStr);

case 'timeDate':
{
const dateStr = result[t];
if (isNaN(ms)) {
logger.warn(`<${content.executionId} (${id})> invalid timeDate >${dateStr}<`);
break;
}

if (dateStr) {
const ms = Date.parse(dateStr);
if (isNaN(ms)) throw new Error(`invalid timeDate >${dateStr}<`);
expireAtDate = new Date(ms);
} else {
expireAtDate = new Date(now);
expireAtDate = new Date(ms);
} else {
expireAtDate = new Date(now);
}

break;
}
}

break;
}
}
if (!expireAtDate) return result;

if (!('expireAt' in result) || result.expireAt > expireAtDate) {
result.timerType = t;
result.expireAt = expireAtDate;
}

if (!expireAtDate) return result;
return result;
}, { ...(expireAt ? {
expireAt
} : undefined)
});

if (!('expireAt' in result) || result.expireAt > expireAtDate) {
result.timerType = t;
result.expireAt = expireAtDate;
if ('expireAt' in timerContent) {
timerContent.timeout = timerContent.expireAt - now;
} else if ('timeout' in content) {
timerContent.timeout = content.timeout;
} else if (!Object.keys(timerContent).length) {
timerContent.timeout = 0;
}

return result;
}, { ...(expireAt ? {
expireAt
} : undefined)
});
return timerContent;

if ('expireAt' in timerContent) {
timerContent.timeout = timerContent.expireAt - now;
} else if ('timeout' in content) {
timerContent.timeout = content.timeout;
} else if (!Object.keys(timerContent).length) {
timerContent.timeout = 0;
function getDurationInMilliseconds(duration) {
try {
return (0, _iso8601Duration.toSeconds)((0, _iso8601Duration.parse)(duration)) * 1000;
} catch (err) {
logger.warn(`<${content.executionId} (${id})> failed to parse timeDuration >${duration}<: ${err.message}`);
}
}
}

return timerContent;
}
11 changes: 6 additions & 5 deletions docs/Scripts.md
Expand Up @@ -40,7 +40,7 @@ export function Scripts() {
register,
};

function register({id, type, behaviour}) {
function register({id, type, behaviour, environment}) {
let scriptBody, language;

switch (type) {
Expand All @@ -59,7 +59,7 @@ export function Scripts() {

if (!/^javascript$/i.test(language)) return;

const script = javaScript(language, `${type}/${id}`, scriptBody);
const script = javaScript(language, `${type}/${id}`, scriptBody, environment);
scripts[id] = script;

return script;
Expand All @@ -69,14 +69,15 @@ export function Scripts() {
return scripts[id];
}

function javaScript(language, filename, scriptBody) {
function javaScript(language, filename, scriptBody, environment) {
const script = new Script(scriptBody, {filename});
return {
script,
language,
execute(executionContext, callback) {
return script.runInNewContext({...executionContext, next: callback});
},
const timers = environment.timers.register(executionContext);
return script.runInNewContext({...executionContext, ...timers, next: callback});
}
};
}
}
Expand Down
5 changes: 4 additions & 1 deletion docs/TimerEventDefinition.md
Expand Up @@ -18,6 +18,7 @@ Object with properties. A subset:
- [`timeDate`](#timedate): the resolved expire date if any
- [`timeCycle`](#timecycle): the resolved time cycle if any
- `startedAt`: timer started at date
- `expireAt`: timer expires at date

## `activity.timeout`

Expand Down Expand Up @@ -47,4 +48,6 @@ The shortest timeout will be picked to start the timer.

# Set your own timeout

If the parent event start message has a `timeout` positive integer property a timer will be started. See how to format these messages [here](/docs/Extension.md).
If the parent event start message has an `expireAt` date or `timeout` positive integer property a timer will be started.

See how to format these messages [here](/docs/Extension.md).
6 changes: 4 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "bpmn-elements",
"version": "4.3.0",
"version": "4.3.1",
"description": "Executable workflow elements based on BPMN 2.0",
"main": "dist/index.js",
"module": "index.js",
Expand All @@ -10,7 +10,9 @@
"prepare": "npm run dist",
"cov:html": "nyc mocha -R dot && nyc report --reporter=html",
"test:lcov": "nyc mocha -R dot && nyc report --reporter lcov && npm run posttest",
"dist": "babel index.js -d dist && rm -rf dist/src && babel src -d dist/src"
"dist": "babel index.js -d dist && rm -rf dist/src && babel src -d dist/src",
"test-md": "node scripts/test-markdown ./API.md && node scripts/test-markdown ./docs/Examples.md",
"toc": "node scripts/generate-api-toc ./API.md,./docs/Examples.md,./docs/Form.md"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 83c85d3

Please sign in to comment.