Skip to content

Commit

Permalink
Merge pull request #16 from lenchvolodymyr/fix/partition-key
Browse files Browse the repository at this point in the history
set timeout limit and fix error message
  • Loading branch information
lenchvolodymyr authored Jan 19, 2021
2 parents 8723d2a + 1707902 commit 2bd8546
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
6 changes: 4 additions & 2 deletions forward_engineering/applyToInstance/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const reApi = require('../../reverse_engineering/api');
const executeWithTimeout = require('../../reverse_engineering/helpers/executeWithTimeout');
const applyToInstanceHelper = require('./applyToInstanceHelper');

const createOrUpdate = async (sample, container) => {
Expand Down Expand Up @@ -78,8 +79,8 @@ const getUniqueKeys = (uniqueKeys) => {
module.exports = {
testConnection: reApi.testConnection,
async applyToInstance(connectionInfo, logger, callback, app) {
const _ = app.require('lodash');
try {
const _ = app.require('lodash');
const helper = applyToInstanceHelper(_);
logger.progress = logger.progress || (() => {});
logger.clear();
Expand All @@ -100,7 +101,7 @@ module.exports = {

progress('Create database if not exists...');

const { database } = await client.databases.createIfNotExists({ id: databaseId });
const { database } = await executeWithTimeout(() => client.databases.createIfNotExists({ id: databaseId }));

progress('Create container if not exists ...');

Expand Down Expand Up @@ -151,6 +152,7 @@ module.exports = {

callback(null);
} catch (error) {
error.message = _.get(error, 'response.data.error.message', error.message || error.name);
logger.log('error', error);
callback({
message: error.message,
Expand Down
3 changes: 2 additions & 1 deletion reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const setUpDocumentClient = require('./helpers/setUpDocumentClient');
const _ = require('lodash');
const axios = require('axios');
const qs = require('qs');
const executeWithTimeout = require('./helpers/executeWithTimeout');
let client;

module.exports = {
Expand All @@ -20,7 +21,7 @@ module.exports = {
client = setUpDocumentClient(connectionInfo);
logger.log('info', connectionInfo, 'Reverse-Engineering connection settings', connectionInfo.hiddenKeys);
try {
await getDatabasesData();
await executeWithTimeout(getDatabasesData);
return cb();
} catch(err) {
return cb(mapError(err));
Expand Down
18 changes: 18 additions & 0 deletions reverse_engineering/helpers/executeWithTimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

const executeWithTimeout = (f, timeout = 30000) => new Promise(async (resolve, reject) => {
const t = setTimeout(async () => {
reject(new Error('Timeout exceeded. Please check your connections settings and try again'));
}, timeout);

try {
const result = await f();

resolve(result);
} catch (error) {
reject(error);
} finally {
clearTimeout(t);
}
});

module.exports = executeWithTimeout;
5 changes: 4 additions & 1 deletion reverse_engineering/helpers/setUpDocumentClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ function getEndpoint(data) {
function setUpDocumentClient(connectionInfo) {
const endpoint = getEndpoint(connectionInfo);
const key = connectionInfo.accountKey;
const connectionPolicy = {
requestTimeout: 30000
};
if ((connectionInfo.disableSSL)) {
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
}
return new CosmosClient({ endpoint, key });
return new CosmosClient({ endpoint, key, connectionPolicy });
}

module.exports = setUpDocumentClient;

0 comments on commit 2bd8546

Please sign in to comment.