From a866aa79c44fc4fe327fc0f7bac3c75441f41aa0 Mon Sep 17 00:00:00 2001 From: cheetah Date: Mon, 14 Nov 2022 22:02:49 +0000 Subject: [PATCH] added retry for puppeteering --- .../connectors/eMessagePuppeteerConnector.js | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/types/connectors/eMessagePuppeteerConnector.js b/types/connectors/eMessagePuppeteerConnector.js index 58ac7db..d674a45 100644 --- a/types/connectors/eMessagePuppeteerConnector.js +++ b/types/connectors/eMessagePuppeteerConnector.js @@ -32,8 +32,8 @@ class eMessagePuppeteerConnector extends Connector { payloadBuffer = Buffer.from(boskrypt.encrypt(payloadBuffer, keyTable[ FunkrufNummer ], 0)) } } - - let sendPromise = new Promise(async (res, rej) => { + + let sendPromise = () => new Promise(async (res, rej) => { const browser = await puppeteer.launch({ headless: true }) this.connectorRegistry.reportState(msg, UUID, 'puppettering') const page = await browser.newPage() @@ -44,29 +44,32 @@ class eMessagePuppeteerConnector extends Connector { await page.select(serviceSelect, serviceName) await page.click(serviceSelect) await page.click(serviceSelect) - await sleep(1e3) const selectServiceButton = 'input[name="pageSendForm:selectServiceButton"]' await page.waitForSelector(selectServiceButton) await page.click(selectServiceButton) - + await sleep(1e3) const pagerNumberInput = 'input[name="pageSendForm:pagerNumberInput"]' await page.waitForSelector(pagerNumberInput) await page.type(pagerNumberInput, identifier) + await sleep(1e3) const validatePagerNumberButton = 'input[id="pageSendForm:validatePagerNumberButton"]' await page.waitForSelector(validatePagerNumberButton) await page.click(validatePagerNumberButton) + await sleep(1e3) const pageContentInput = 'textarea[id="pageSendForm:pageContentInput"]' await page.waitForSelector(pageContentInput) await page.type(pageContentInput, payloadBuffer.toString('ascii')) + await sleep(1e3) const sendPageButton = 'input[id="pageSendForm:sendPageButton"]' await page.waitForSelector(sendPageButton) await page.click(sendPageButton) + await sleep(1e3) const messageSentResult = 'span[id="pageSendForm:messageSentResult"]' await page.waitForSelector(messageSentResult) @@ -75,22 +78,34 @@ class eMessagePuppeteerConnector extends Connector { let value = await page.evaluate(el => el.textContent, $messageSentResult) // pageSendForm:messageSentResult if (value.indexOf("Meldung : OK") > -1) { - this.connectorRegistry.reportState(msg, UUID, 'transit') await browser.close() return res(true) } else { - this.connectorRegistry.reportState(msg, UUID, 'fail') await browser.close() return rej(value) } }) - sendPromise - .catch((err) => { - console.error(err) - this.connectorRegistry.reportFail(msg, UUID) - return false - }) + try { + await sendPromise() + } catch (e1) { + console.error(e1) + try { + await sendPromise() + } catch (e2) { + console.error(e2) + await sendPromise() + .then(_=>{ + this.connectorRegistry.reportState(msg, UUID, 'transit') + return true + }) + .catch((err) => { + console.error(err) + this.connectorRegistry.reportFail(msg, UUID) + return false + }) + } + } } } module.exports = eMessagePuppeteerConnector \ No newline at end of file