Skip to content

Commit b1dfc33

Browse files
authored
timeOut (#43)
* Added timeout for `Upsert Object` action
1 parent da01def commit b1dfc33

File tree

7 files changed

+41
-32
lines changed

7 files changed

+41
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.2.4 (June 03, 2022)
2+
* Added timeout for `Upsert Object` action
3+
14
## 2.2.3 (April 14, 2022)
25
* Bump dependencies
36

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Name|Mandatory|Description|Values|
4343
|REFRESH_TOKEN_RETRIES| false | Determines how many retries to refresh token should be done before throwing an error | Default: `10` |
4444
|HASH_LIMIT_TIME| false | Hash expiration time in ms | Default: `600000` |
4545
|HASH_LIMIT_ELEMENTS| false | Hash size number limit | Default: `10` |
46+
|UPSERT_TIME_OUT| false | Time out for `Upsert Object` action in ms | Default: `120000` (2min) |
4647

4748
## Credentials
4849
Authentication occurs via OAuth 2.0.

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"docsUrl": "https://github.com/elasticio/salesforce-component-v2",
55
"url": "http://www.salesforce.com/",
66
"buildType": "docker",
7-
"version": "2.2.3",
7+
"version": "2.2.4",
88
"authClientTypes": [
99
"oauth2"
1010
],

lib/actions/upsert_v2.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const { AttachmentProcessor } = require('@elastic.io/component-commons-library')
55
const { callJSForceMethod } = require('../helpers/wrapper');
66
const { createProperty, getLookupFieldsModelWithTypeOfSearch } = require('../helpers/utils');
77

8+
const timeOut = process.env.UPSERT_TIME_OUT ? Number(process.env.UPSERT_TIME_OUT) : 120000;
9+
810
module.exports.getObjectTypes = async function getObjectTypes(configuration) {
911
return callJSForceMethod.call(this, configuration, 'getObjectTypes');
1012
};
@@ -75,7 +77,7 @@ class UpsertObject extends Upsert {
7577
if (cfg.lookupField === 'Id' && (!msg.body.Id)) {
7678
existingObj = undefined;
7779
} else {
78-
existingObj = await callJSForceMethod.call(this, cfg, 'sobjectLookup', msg);
80+
existingObj = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectLookup', msg);
7981
if (existingObj.length > 1) {
8082
throw new Error('Found more than 1 Object');
8183
} else if (existingObj.length === 0) {
@@ -101,12 +103,12 @@ class UpsertObject extends Upsert {
101103

102104
async updateObject(_criteria, _type, object, cfg, _msg, existingObject) {
103105
object.body.Id = existingObject[0].Id;
104-
const result = await callJSForceMethod.call(this, cfg, 'sobjectUpdate', object);
106+
const result = await callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectUpdate', object);
105107
return result;
106108
}
107109

108110
async createObject(object, cfg, _msg) {
109-
const result = callJSForceMethod.call(this, cfg, 'sobjectCreate', object);
111+
const result = callJSForceMethod.call(this, { ...cfg, timeOut }, 'sobjectCreate', object);
110112
return result;
111113
}
112114
}

lib/helpers/wrapper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const { getSecret, refreshToken } = require('../util');
44
const { REFRESH_TOKEN_RETRIES } = require('../common.js').globalConsts;
55

66
let client;
7+
const timeOut = async (t) => {
8+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
9+
await sleep(t);
10+
throw new Error(`UPSERT_TIME_OUT ${t}ms limit reached`);
11+
};
712

813
exports.callJSForceMethod = async function callJSForceMethod(configuration, method, options) {
914
this.logger.debug('Preparing SalesForce Client...');
@@ -39,7 +44,7 @@ exports.callJSForceMethod = async function callJSForceMethod(configuration, meth
3944
this.logger.debug('SalesForce Client is created');
4045
}
4146
this.logger.debug('Trying to call method %s', method);
42-
result = await client[method](options);
47+
result = configuration.timeOut ? await Promise.race([client[method](options), timeOut(configuration.timeOut)]) : await client[method](options);
4348
isSuccess = true;
4449
this.logger.debug('Method %s was successfully executed', method);
4550
break;

spec/actions/upsert_v2.spec.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const metaModelDocumentFields = require('../testData/sfDocumentFields.json')
88
const sfDocumentMetamodel = require('../testData/sfDocumentMetamodel.json')
99
const { globalConsts } = require('../../lib/common.js');
1010
const {
11-
getContext, fetchToken, defaultCfg, testsCommon,
11+
getContext, fetchToken, describeDocReq, defaultCfg, testsCommon,
1212
} = require('../common.js');
1313

1414
describe('Upsert v2 Object test', () => {
@@ -90,49 +90,43 @@ describe('Upsert v2 Object test', () => {
9090

9191
describe('Main process', () => {
9292
const configuration = {
93+
...defaultCfg,
9394
secretId: 'secretId',
9495
sobject: 'Document',
9596
typeOfSearch: 'allFields',
9697
updateFields: ['Url', 'Body'],
9798
lookupField: 'Id'
9899
}
99100

100-
it.skip('Object found, going to update', async () => {
101+
it('Object found, going to update', async () => {
101102
const testCfg = {
102103
...configuration
103104
}
104105
const msg = {
105106
body: {
106107
Id: 1,
107108
Url: '😂',
108-
Body: 'http://test.env.mock/somedata.txt'
109+
Body: 'YXNkYXNkYXNkcXdlcXdlcXdl'
109110
}
110111
}
111-
112112
fetchToken()
113-
const describeReq = nock(testsCommon.instanceUrl)
114-
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/sobjects/Document/describe`)
115-
.reply(200, metaModelDocumentReply)
113+
describeDocReq(globalConsts.SALESFORCE_API_VERSION, metaModelDocumentReply)
114+
fetchToken()
115+
describeDocReq(globalConsts.SALESFORCE_API_VERSION, metaModelDocumentReply)
116116
fetchToken()
117117
const queryReq = nock(testsCommon.instanceUrl)
118-
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/query?q=${
119-
testsCommon.buildSOQL(metaModelDocumentReply, { Id: 1 })
118+
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/query?q=${testsCommon.buildSOQL(metaModelDocumentReply, { Id: 1 })
120119
}`)
121120
.reply(200, { done: true, totalSize: 1, records: [{ Id: 1, Url: '😂' }] })
122121
fetchToken();
123122
const patchDocReq = nock(testsCommon.instanceUrl)
124123
.patch(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/sobjects/Document/1`, { Url: '😂', Body: 'YXNkYXNkYXNkcXdlcXdlcXdl' })
125124
.reply(204)
126-
const getTxtReq = nock('http://test.env.mock')
127-
.get('/somedata.txt')
128-
.replyWithFile(200, `${__dirname}/../testData/somedata.txt`);
129125

130126
const result = await upsert.process.call(getContext(), msg, testCfg)
131127
expect(result.body.success).to.eql(true);
132-
describeReq.done()
133128
queryReq.done()
134129
patchDocReq.done()
135-
getTxtReq.done()
136130
})
137131

138132
it('Object not found, going to create', async () => {
@@ -147,13 +141,12 @@ describe('Upsert v2 Object test', () => {
147141
}
148142

149143
fetchToken()
150-
const describeReq = nock(testsCommon.instanceUrl)
151-
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/sobjects/Document/describe`)
152-
.reply(200, metaModelDocumentReply)
144+
describeDocReq(globalConsts.SALESFORCE_API_VERSION, metaModelDocumentReply)
145+
fetchToken()
146+
describeDocReq(globalConsts.SALESFORCE_API_VERSION, metaModelDocumentReply)
153147
fetchToken()
154148
const queryReq = nock(testsCommon.instanceUrl)
155-
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/query?q=${
156-
testsCommon.buildSOQL(metaModelDocumentReply, { Id: 1 })
149+
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/query?q=${testsCommon.buildSOQL(metaModelDocumentReply, { Id: 1 })
157150
}`)
158151
.reply(200, { done: true, totalSize: 1, records: [] })
159152
fetchToken()
@@ -163,7 +156,6 @@ describe('Upsert v2 Object test', () => {
163156

164157
const result = await upsert.process.call(getContext(), msg, testCfg)
165158
expect(result.body.success).to.eql(true);
166-
describeReq.done()
167159
queryReq.done()
168160
postDocReq.done()
169161
})
@@ -179,17 +171,14 @@ describe('Upsert v2 Object test', () => {
179171
}
180172

181173
fetchToken()
182-
const describeReq = nock(testsCommon.instanceUrl)
183-
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/sobjects/Document/describe`)
184-
.reply(200, metaModelDocumentReply)
174+
describeDocReq(globalConsts.SALESFORCE_API_VERSION, metaModelDocumentReply)
185175
fetchToken()
186176
const createDocReq = nock(testsCommon.instanceUrl)
187177
.post(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/sobjects/Document`, msg.body)
188178
.reply(200, { id: 2, success: true, })
189179

190180
const result = await upsert.process.call(getContext(), msg, testCfg)
191181
expect(result.body.success).to.eql(true);
192-
describeReq.done()
193182
createDocReq.done()
194183
})
195184

@@ -204,10 +193,13 @@ describe('Upsert v2 Object test', () => {
204193
}
205194
}
206195

196+
fetchToken()
197+
const describeReq = nock(testsCommon.instanceUrl)
198+
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/sobjects/Document/describe`)
199+
.reply(200, metaModelDocumentReply)
207200
fetchToken()
208201
const scope = nock(testsCommon.instanceUrl)
209-
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/query?q=${
210-
testsCommon.buildSOQL(metaModelDocumentReply, { Id: 1 })
202+
.get(`/services/data/v${globalConsts.SALESFORCE_API_VERSION}/query?q=${testsCommon.buildSOQL(metaModelDocumentReply, { Id: 1 })
211203
}`)
212204
.reply(200, { done: true, totalSize: 1, records: [1, 2] })
213205

@@ -217,6 +209,7 @@ describe('Upsert v2 Object test', () => {
217209
expect(err.message).to.eql('Found more than 1 Object');
218210
}
219211
scope.done()
212+
describeReq.done()
220213
})
221214
})
222215
})

spec/common.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ module.exports = {
7676
if (typeof (where) === 'string') {
7777
soql += where;
7878
} else {
79-
// eslint-disable-next-line guard-for-in,no-restricted-syntax
79+
// eslint-disable-next-line guard-for-in,no-restricted-syntax
8080
for (const key in where) {
8181
soql += `${key}%20%3D%20`;
8282
const field = objectMeta.fields.find((f) => f.name === key);
@@ -106,6 +106,11 @@ module.exports = {
106106
.get(`/v2/workspaces/${process.env.ELASTICIO_WORKSPACE_ID}/secrets/${secretId}`)
107107
.reply(200, secret);
108108
},
109+
describeDocReq: (api, meta) => {
110+
nock(instanceUrl)
111+
.get(`/services/data/v${api}/sobjects/Document/describe`)
112+
.reply(200, meta);
113+
},
109114
validateEmitEqualsToData: (emit, data) => {
110115
expect(emit.callCount).to.be.equal(1);
111116
expect(emit.getCall(0).args[0]).to.be.equal('data');

0 commit comments

Comments
 (0)