From 36f97c8d06fc64db55581bb4c46ec32e6d0026f0 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 12 Oct 2020 01:18:12 +0530 Subject: [PATCH] Refactors batching --- utils/constants.js | 1 + utils/domain-service.js | 36 +++++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/utils/constants.js b/utils/constants.js index a2a22d408..086bb0f29 100644 --- a/utils/constants.js +++ b/utils/constants.js @@ -14,6 +14,7 @@ const DOMAINS_PATH = require('path').resolve('domains'); module.exports = { ENV, + IS_TEST, VALID_RECORD_TYPES: ['CNAME', 'A', 'URL'], DOMAIN_DOMAIN: DOMAIN_DOMAIN || 'booboo.xyz', DOMAIN_USER: IS_TEST ? 'testuser' : DOMAIN_USER, diff --git a/utils/domain-service.js b/utils/domain-service.js index e1e9abf2a..f78652740 100644 --- a/utils/domain-service.js +++ b/utils/domain-service.js @@ -1,6 +1,8 @@ const R = require('ramda'); const { cpanel } = require('./lib/cpanel'); -const { DOMAIN_DOMAIN } = require('./constants'); +const { DOMAIN_DOMAIN, IS_TEST } = require('./constants'); + +const log = IS_TEST ? () => {} : console.log; const recordToRedirection = ({ name, address }) => ({ domain: `${name}.${DOMAIN_DOMAIN}`, @@ -13,7 +15,8 @@ const recordToZone = ({ name, type, address, ...rec }) => ({ ...rec, //line name, type, - ...(type === 'CNAME' ? { cname: address } : { address }), + address, + ...(type === 'CNAME' ? { cname: address } : {}), }); const cleanName = name => `${name}`.replace(new RegExp(`\.${DOMAIN_DOMAIN}\.?$`), '').toLowerCase(); @@ -75,17 +78,19 @@ const batchLazyTasks = count => tasks => tasks.reduce((batches, task) => { return [...full, [...last, task]]; }, []); -const executeBatch = (batches) => batches.reduce((promise, batch) => { - return promise.then(() => { - console.log('>>> Running batch', batch.length); - return Promise.all(batch.map(fn => fn().catch(e => { - console.error(e); - }))).then(values => { - const results = values.map(R.pathOr([], ['cpanelresult', 'data', 0])); - const failed = results.filter(x => (x.result || {}).status != 1); - console.log(`${values.length - failed.length}/${values.length}`); - failed.length && console.log(failed); - }); +const executeBatch = (batches) => batches.reduce((promise, batch, index) => { + return promise.then(async () => { + log('>>> Running batch number:', index + 1, `(size: ${batch.length})`); + + const values = await Promise.all(batch.map(fn => fn().catch(e => console.error(e)))); + + const results = values.map(R.pathOr({}, ['cpanelresult', 'data', 0])); + const failed = results.filter(x => (x.result || {}).status != 1); + + log(`${values.length - failed.length}/${values.length}`); + failed.length && log(failed); + + return null; }); }, Promise.resolve()); @@ -109,9 +114,10 @@ const getDomainService = ({ cpanel }) => { return list; }; - const BATCH_SIZE = 10; + const BATCH_SIZE = 1; - const addRecords = R.compose(batchLazyTasks(BATCH_SIZE), R.map(R.cond([ + const addRecords = R.compose(batchLazyTasks(BATCH_SIZE), R.filter(Boolean), R.map(R.cond([ + [ R.propEq('name', 'www'), () => null ], [ R.propEq('type', 'URL'), addRedirection ], [ R.T, addZoneRecord ], ])));