Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Outgoing integrations were stopping the oplog tailing sometimes #11333

Merged
merged 1 commit into from
Jul 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/rocketchat-integrations/server/lib/triggerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import _ from 'underscore';
import s from 'underscore.string';
import moment from 'moment';
import vm from 'vm';
import Fiber from 'fibers';
import Future from 'fibers/future';

RocketChat.integrations.triggerHandler = new class RocketChatIntegrationHandler {
constructor() {
Expand Down Expand Up @@ -202,7 +204,15 @@ RocketChat.integrations.triggerHandler = new class RocketChatIntegrationHandler

buildSandbox(store = {}) {
const sandbox = {
_, s, console, moment,
scriptTimeout(reject) {
return setTimeout(() => reject('timed out'), 3000);
},
_,
s,
console,
moment,
Fiber,
Promise,
Store: {
set: (key, val) => store[key] = val,
get: (key) => store[key]
Expand Down Expand Up @@ -303,7 +313,21 @@ RocketChat.integrations.triggerHandler = new class RocketChatIntegrationHandler
sandbox.params = params;

this.updateHistory({ historyId, step: `execute-script-before-running-${ method }` });
const result = this.vm.runInNewContext('script[method](params)', sandbox, { timeout: 3000 });

const result = Future.fromPromise(this.vm.runInNewContext(`
new Promise((resolve, reject) => {
Fiber(() => {
scriptTimeout(reject);
try {
resolve(script[method](params))
} catch(e) {
reject(e);
}
}).run();
}).catch((error) => { throw new Error(error); });
`, sandbox, {
timeout: 3000
})).wait();

logger.outgoing.debug(`Script method "${ method }" result of the Integration "${ integration.name }" is:`);
logger.outgoing.debug(result);
Expand Down