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

Arag_beta_pup_1 #678

Open
wants to merge 3 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
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
126 changes: 5 additions & 121 deletions lib/datapacktypes/omniscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var puppeteer = require('puppeteer-core');
var fs = require('fs-extra');
var path = require('path');
var yaml = require('js-yaml');
var compileOSLWCJob = require('../puppeteerHelper/compileOSLWC')

var OmniScript = module.exports = function (vlocity) {
this.vlocity = vlocity;
Expand Down Expand Up @@ -291,128 +292,11 @@ OmniScript.prototype.compileOmniScriptLwc = async function (jobInfo, omniScriptI
}
}

lastPromise = Promise.resolve();
OmniScript.prototype.compileOSLWC = async function (jobInfo, omniScriptId, omniScriptKey) {
try {

let puppeteerOptions = await utilityservice.prototype.getPuppeteerOptions(jobInfo);

VlocityUtils.verbose('Deployed OmniScript ID', omniScriptKey + ' (' + omniScriptId + ')');

if (!puppeteerOptions.executablePath && !jobInfo.puppeteerInstalled) {
VlocityUtils.error('Chromium not installed. LWC activation disabled. Run "npm install puppeteer -g" or set puppeteerExecutablePath in your Job File');
jobInfo.ignoreLWCActivationOS = true;
jobInfo.ignoreLWCActivationCards = true;
} else {
var package = this.vlocity.namespacePrefix;

var siteUrl = this.vlocity.jsForceConnection.instanceUrl;
var sessionToken = this.vlocity.jsForceConnection.accessToken;
var loginURl = siteUrl + '/secur/frontdoor.jsp?sid=' + sessionToken;
VlocityUtils.verbose('LWC Activation Login URL', loginURl);
var browser;

try {
browser = await puppeteer.launch(puppeteerOptions);
} catch (error) {
VlocityUtils.error('Puppeteer initialization Failed, LWC Activation disabled - ' + error);
jobInfo.ignoreLWCActivationOS = true;
return;
}

const page = await browser.newPage();
const loginTimeout = jobInfo.loginTimeoutForLoginLWC;

await Promise.all([
page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'load' }),
page.waitForNavigation({ timeout: loginTimeout, waitUntil: 'networkidle2'}),
page.goto(loginURl, {timeout: loginTimeout})
]);

var omniScriptDisignerpageLink = siteUrl + '/apex/' + package + 'OmniLwcCompile?id=' + omniScriptId + '&activate=true';
var omniScriptLogId = omniScriptKey + ' (' + omniScriptId + ')';

VlocityUtils.report('Starting OmniScript LWC Activation', omniScriptLogId);
VlocityUtils.verbose('LWC Activation URL', omniScriptDisignerpageLink);

await page.goto(omniScriptDisignerpageLink);
await page.waitForTimeout(5000);


let tries = 0;
var errorMessage;
var maxNumOfTries = Math.ceil((60/jobInfo.defaultLWCPullTimeInSeconds)*jobInfo.defaultMinToWaitForLWCOmniScript);
while (tries < maxNumOfTries && !jobInfo.ignoreLWCActivationOS) {
try {
let message;
try {
message = await page.waitForSelector('#compiler-message');
} catch (messageTimeout) {
VlocityUtils.verbose('Error', messageTimeout);
VlocityUtils.log(omniScriptKey, 'Loading Page taking too long - Retrying - Tries: ' + tries + ' of ' + maxNumOfTries);
}

if (message) {
let currentStatus = await message.evaluate(node => node.innerText);
VlocityUtils.report('Activating LWC for OmniScript', omniScriptLogId, currentStatus);
jobInfo.elapsedTime = VlocityUtils.getTime();
VlocityUtils.report('Elapsed Time', jobInfo.elapsedTime);
if (currentStatus === 'DONE') {
VlocityUtils.success('LWC Activated', omniScriptLogId);
break;
} else if (/^ERROR: No MODULE named markup/.test(currentStatus)) {
var missingLWCTrimedError = currentStatus.substring('ERROR: '.length, currentStatus.indexOf(' found :'));
errorMessage = ' Missing Custom LWC - ' + missingLWCTrimedError;
break;
} else if (/^ERROR/.test(currentStatus)) {
errorMessage = ' Error Activating LWC - ' + currentStatus;
break;
}
}
} catch (e) {
VlocityUtils.error('Error Activating LWC', omniScriptLogId, e);
errorMessage = ' Error: ' + e;
}
tries++;
await page.waitForTimeout(jobInfo.defaultLWCPullTimeInSeconds*1000);
}

if (tries == maxNumOfTries) {
errorMessage = 'Activation took longer than ' + jobInfo.defaultMinToWaitForLWCOmniScript + ' minutes - Aborting';
}

if (errorMessage) {
if (!jobInfo.omniScriptLwcActivationSkip) {
jobInfo.omniScriptLwcActivationSkip = {};
}
jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage;
jobInfo.hasError = true;
jobInfo.currentStatus[omniScriptKey] = 'Error';
jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage;
jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage);
VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + errorMessage);
await this.deactivateOmniScript(omniScriptId);
}
browser.close();
}
} catch (e) {
VlocityUtils.error(e);

if (!jobInfo.omniScriptLwcActivationSkip) {
jobInfo.omniScriptLwcActivationSkip = {};
}
jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage;
jobInfo.hasError = true;
jobInfo.currentStatus[omniScriptKey] = 'Error';
jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + e;
jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + e);
VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + e);
await this.deactivateOmniScript(omniScriptId, jobInfo);

try {
browser.close();
} catch (e2) {}
}
}
lastPromise = lastPromise.then(() => compileOSLWCJob(jobInfo, omniScriptId, omniScriptKey, async (omniscript)=> await this.deactivateOmniScript(omniscript), this.vlocity));
await lastPromise;
};

OmniScript.prototype.onActivateError = async function (dataPackData) {
var onActivateErrorResult = {},
Expand Down
92 changes: 92 additions & 0 deletions lib/puppeteerHelper/browserHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
exports.goToUniquePage = async (page, siteUrl, packageNamespace, omniScriptId) => {
const omniScriptDesignerPageLink = `${siteUrl}/lightning/cmp/${packageNamespace}OmniDesignerAuraWrapper?c__recordId=${omniScriptId}`;
await page.goto(omniScriptDesignerPageLink, { timeout: 900000, waitUntil: 'networkidle2' });
};

exports.clickPreviewButton = async (page, timeout) => {
await page.mouse.click(1127, 134);
};

exports.clickDeactivate = async (page) => {
let pageChanged = false;
page.on('framenavigated', frame => {
if (frame === page.mainFrame()) pageChanged = true;
});

await page.mouse.click(1358, 134);
await new Promise(res => setTimeout(res, 15000));
await page.mouse.click(1042, 572);
await new Promise(res => setTimeout(res, 5000));

if (!pageChanged) {
await new Promise(res => setTimeout(res, 5000));
await page.keyboard.down('Tab');
await page.keyboard.up('Tab');
}
await page.keyboard.down('Tab');
await page.keyboard.up('Tab');
await page.keyboard.down('Enter');
await page.keyboard.up('Enter');

if (pageChanged) {
await new Promise(res => setTimeout(res, 10000));
await page.keyboard.down('Tab');
await page.keyboard.up('Tab');
await page.keyboard.down('Enter');
await page.keyboard.up('Enter');
}
};

exports.clickActivate = async (page) => {
let pageChanged = false;
page.on('framenavigated', frame => {
if (frame === page.mainFrame()) pageChanged = true;
});

await page.mouse.click(1358, 134);
await new Promise(res => setTimeout(res, 5000));
await page.mouse.click(1042, 572);
await new Promise(res => setTimeout(res, 25000));

if (!pageChanged) {
await new Promise(res => setTimeout(res, 5000));
await page.keyboard.down('Tab');
await page.keyboard.up('Tab');
}
await page.keyboard.down('Tab');
await page.keyboard.up('Tab');
await page.keyboard.down('Enter');
await page.keyboard.up('Enter');
await page.mouse.click(1054, 670);

if (pageChanged) {
await new Promise(res => setTimeout(res, 7000));
await page.keyboard.down('Tab');
await page.keyboard.up('Tab');
await page.keyboard.down('Enter');
await page.keyboard.up('Enter');
}
};


exports.verifyOmniscriptActivation = async (page) => {
const getShadowElement = async (element, selector) => {
return await element.evaluateHandle((el, sel) => el.shadowRoot.querySelector(sel), selector);
};

const canvasWebComponentHandle = await page.$('omnistudio-omni-designer-canvas');
if (!canvasWebComponentHandle) throw new Error('omnistudio-omni-designer-canvas element not found');

const designerCanvasHandle = await getShadowElement(canvasWebComponentHandle, 'c-omni-designer-canvas-body');
if (!designerCanvasHandle) throw new Error('c-omni-designer-canvas-body element not found');

const designerCanvasBodyHandle = await getShadowElement(designerCanvasHandle, 'c-omni-designer-preview');
if (!designerCanvasBodyHandle) throw new Error('c-omni-designer-preview element not found');

const iframeHandle = await getShadowElement(designerCanvasBodyHandle, 'iframe');
if (!iframeHandle) throw new Error('iframe element not found');

const contentFrame = await iframeHandle.contentFrame();
const errorElement = await contentFrame.$('.slds-text-color_error');
return !errorElement;
};
66 changes: 66 additions & 0 deletions lib/puppeteerHelper/compileOSLWC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const utilityservice = require('../utilityservice.js');
const { launchBrowser, loginToBrowser,checkChromiumInstallation } = require('../puppeteerHelper/puppeteerHelper.js');
const { goToUniquePage, clickPreviewButton, clickDeactivate, clickActivate, verifyOmniscriptActivation } = require('../puppeteerHelper/browserHelper.js');
const { activateOmniScript, handleErrorAndDeactivate } = require('../puppeteerHelper/omniScriptHelper.js');

const compileOSLWCJob = async (jobInfo, omniScriptId, omniScriptKey, deactivateOmniScript, vlocity) => {
let browser;

try {
const puppeteerOptions = await utilityservice.prototype.getPuppeteerOptions(jobInfo);
VlocityUtils.report('Deployed OmniScript ID', omniScriptKey + ' (' + omniScriptId + ')');

if (!checkChromiumInstallation(puppeteerOptions, jobInfo)) return;
const packageNamespace = vlocity.namespacePrefix;
const siteUrl = vlocity.jsForceConnection.instanceUrl;
const sessionToken = vlocity.jsForceConnection.accessToken;
const loginUrl = `${siteUrl}/secur/frontdoor.jsp?sid=${sessionToken}`;
VlocityUtils.verbose('LWC Activation Login URL', loginUrl);

browser = await launchBrowser(puppeteerOptions);
if (!browser) {
jobInfo.ignoreLWCActivationOS = true;
return;
}

const page = await loginToBrowser(browser, loginUrl, jobInfo);
await page.setViewport({ width: 1520, height: 1000 });

const errorMessage = await activateOmniScript(page, siteUrl, packageNamespace, omniScriptId, omniScriptKey, jobInfo);
if (errorMessage) {
await handleErrorAndDeactivate(errorMessage, jobInfo, omniScriptId, omniScriptKey,deactivateOmniScript);
return;
}

await new Promise(resolve => setTimeout(resolve, 5000));
await goToUniquePage(page, siteUrl, packageNamespace, omniScriptId);
await new Promise(resolve => setTimeout(resolve, 20000));
await clickPreviewButton(page, 15000);
await new Promise(resolve => setTimeout(resolve, 10000));
const omniScriptLogId = `${omniScriptKey} (${omniScriptId})`;
const enableReactivate = await verifyOmniscriptActivation(page);
if (enableReactivate) {
await clickDeactivate(page);
await new Promise(resolve => setTimeout(resolve, 10000));
await clickActivate(page);
await new Promise(resolve => setTimeout(resolve, 10000));

const omniscriptIsDeployedProperly = await verifyOmniscriptActivation(page);
if (!omniscriptIsDeployedProperly) {
throw new Error(`Even after reactivating we found ${omniScriptKey} has a text Error after click preview`);
}
VlocityUtils.success('LWC Activated_Manually', omniScriptLogId);
return
}
VlocityUtils.success('LWC Activated_Manually wasnt required', omniScriptLogId);
} catch (e) {
const omniScriptLogId = `${omniScriptKey} (${omniScriptId})`;
VlocityUtils.error(e);
VlocityUtils.error('LWC Activated_Manually Error', omniScriptLogId + ' - ' + e);
await handleErrorAndDeactivate(e, jobInfo, omniScriptId, omniScriptKey,deactivateOmniScript);
} finally {
if (browser) await browser.close();
}
};

module.exports = compileOSLWCJob;
69 changes: 69 additions & 0 deletions lib/puppeteerHelper/omniScriptHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

exports.activateOmniScript = async (page, siteUrl, packageNamespace, omniScriptId, omniScriptKey, jobInfo) => {
const omniScriptDesignerPageLink = `${siteUrl}/apex/${packageNamespace}OmniLwcCompile?id=${omniScriptId}&activate=true`;
const omniScriptLogId = `${omniScriptKey} (${omniScriptId})`;

VlocityUtils.report('Starting OmniScript LWC Activation', omniScriptLogId);
VlocityUtils.verbose('LWC Activation URL', omniScriptDesignerPageLink);

await page.goto(omniScriptDesignerPageLink, { timeout: 900000, waitUntil: 'networkidle0' });
await new Promise(resolve => setTimeout(resolve, 5000));

let tries = 0;
let errorMessage;
const maxNumOfTries = Math.ceil((60 / jobInfo.defaultLWCPullTimeInSeconds) * jobInfo.defaultMinToWaitForLWCOmniScript);

while (tries < maxNumOfTries && !jobInfo.ignoreLWCActivationOS) {
try {
let message;
try {
message = await page.waitForSelector('#compiler-message');
} catch (messageTimeout) {
VlocityUtils.verbose('Error', messageTimeout);
console.log(omniScriptKey, 'Loading Page taking too long - Retrying - Tries: ' + tries + ' of ' + maxNumOfTries);
}

if (message) {
const currentStatus = await message.evaluate(node => node.innerText);
VlocityUtils.report('Activating LWC for OmniScript', omniScriptLogId, currentStatus);
jobInfo.elapsedTime = VlocityUtils.report('Elapsed Time', jobInfo.elapsedTime);

if (currentStatus === 'DONE') {
VlocityUtils.success('LWC Activated', omniScriptLogId);
break;
} else if (/^ERROR: No MODULE named markup/.test(currentStatus)) {
errorMessage = 'Missing Custom LWC - ' + currentStatus.substring('ERROR: '.length, currentStatus.indexOf(' found :'));
break;
} else if (/^ERROR/.test(currentStatus)) {
errorMessage = 'Error Activating LWC - ' + currentStatus;
break;
}
}
} catch (e) {
VlocityUtils.error('Error Activating LWC', omniScriptLogId, e);
errorMessage = 'Error: ' + e;
}
tries++;
await new Promise(resolve => setTimeout(resolve, jobInfo.defaultLWCPullTimeInSeconds * 1000));
}

if (tries === maxNumOfTries) {
errorMessage = 'Activation took longer than ' + jobInfo.defaultMinToWaitForLWCOmniScript + ' minutes - Aborting';
}

return errorMessage;
};

exports.handleErrorAndDeactivate = async (errorMessage, jobInfo, omniScriptId, omniScriptKey, deactivateOmniScript) => {
if (!jobInfo.omniScriptLwcActivationSkip) {
jobInfo.omniScriptLwcActivationSkip = {};
}
jobInfo.omniScriptLwcActivationSkip[omniScriptKey] = errorMessage;
jobInfo.hasError = true;
jobInfo.currentStatus[omniScriptKey] = 'Error';
jobInfo.currentErrors[omniScriptKey] = 'LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage;
jobInfo.errors.push('LWC Activation Error >> ' + omniScriptKey + ' - ' + errorMessage);
VlocityUtils.error('LWC Activation Error', omniScriptKey + ' - ' + errorMessage);
await deactivateOmniScript(omniScriptId);
};

Loading