new(all): #327 OPEN AS web automation.

This commit is contained in:
rodrigorodriguez 2023-02-17 15:34:29 -03:00
parent 4d30d5988c
commit bc85f714ca
2 changed files with 34 additions and 27 deletions

View file

@ -237,7 +237,7 @@ export class GBVMService extends GBService {
${code} ${code}
await wa.closeHandles({pid: pid}); await wa.getCloseHandles({pid: pid});
})(); })();

View file

@ -114,16 +114,18 @@ export class WebAutomationServices {
this.debugWeb = this.min.core.getParam<boolean>(this.min.instance, 'Debug Web Automation', false); this.debugWeb = this.min.core.getParam<boolean>(this.min.instance, 'Debug Web Automation', false);
} }
public async closeHandles({pid}){ public async getCloseHandles({ pid }) {
// Releases previous allocated OPEN semaphores. const { min, user } = await DialogKeywords.getProcessInfo(pid);
// Releases previous allocated OPEN semaphores.
let keys = Object.keys(GBServer.globals.webSessions); let keys = Object.keys(GBServer.globals.webSessions);
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
const session = GBServer.globals.webSessions[keys[i]]; const session = GBServer.globals.webSessions[keys[i]];
if (session.pid === pid) { if (session.activePid === pid) {
session.semaphore.release(); session.semaphore.release();
} GBLogEx.info(min, `Release for PID: ${pid} done.`);
} }
}
} }
/** /**
@ -135,7 +137,7 @@ export class WebAutomationServices {
public async getPage({ pid, sessionKind, sessionName, url, username, password }) { public async getPage({ pid, sessionKind, sessionName, url, username, password }) {
GBLog.info(`BASIC: Web Automation GET PAGE ${sessionName ? sessionName : ''} ${url}.`); GBLog.info(`BASIC: Web Automation GET PAGE ${sessionName ? sessionName : ''} ${url}.`);
const { min, user } = await DialogKeywords.getProcessInfo(pid); const { min, user } = await DialogKeywords.getProcessInfo(pid);
let handle; let handle;
// Try to find an existing handle. // Try to find an existing handle.
@ -149,14 +151,17 @@ export class WebAutomationServices {
break; break;
} }
} }
// Semaphore logic to block multiple entries on the same session. // Semaphore logic to block multiple entries on the same session.
let page; let page;
if (session) { if (session) {
const [value, release] = await session.semaphore.acquire(); GBLogEx.info(min, `Acquiring (1) for PID: ${pid}...`);
const release = await session.semaphore.acquire();
GBLogEx.info(min, `Acquire (1) for PID: ${pid} done.`);
try { try {
GBServer.globals.webSessions[handle].release = release; session.activePid = pid;
session.release = release;
page = session.page; page = session.page;
} catch { } catch {
release(); release();
@ -164,7 +169,7 @@ export class WebAutomationServices {
} }
// Creates the page if it is the first time. // Creates the page if it is the first time.
let browser; let browser;
if (!page) { if (!page) {
browser = await createBrowser(null); browser = await createBrowser(null);
@ -177,28 +182,30 @@ export class WebAutomationServices {
// There is no session yet, // There is no session yet,
if (!session && sessionKind === 'AS') { if (!session && sessionKind === 'AS') {
// A new web session is being created. // A new web session is being created.
handle = WebAutomationServices.cyrb53(this.min.botId + url); handle = WebAutomationServices.cyrb53(this.min.botId + url);
GBServer.globals.webSessions[handle] = session= {}; GBServer.globals.webSessions[handle] = session = {};
GBServer.globals.webSessions[handle].sessionName = sessionName; session.sessionName = sessionName;
GBServer.globals.webSessions[handle].pid = pid;
GBServer.globals.webSessions[handle].page = page; session.page = page;
GBServer.globals.webSessions[handle].browser = browser; session.browser = browser;
GBServer.globals.webSessions[handle].semaphore = withTimeout( session.semaphore = new Mutex();
new Semaphore(5), GBLogEx.info(min, `Acquiring (2) for PID: ${pid}...`);
60 * 1000, const release = await session.semaphore.acquire();
new Error('Error waiting for OPEN keyword.') GBLogEx.info(min, `Acquire (2) for PID: ${pid} done.`);
); session.release = release;
session.activePid = pid;
} }
// WITH is only valid in a previously defined session. // WITH is only valid in a previously defined session.
if (!session && sessionKind == 'WITH') { if (!session && sessionKind == 'WITH') {
GBLogEx.error(min, `NULL session for OPEN WITH #${sessionName}.`); const error = `NULL session for OPEN WITH #${sessionName}.`;
GBLogEx.error(min, error);
} }
await page.goto(url); await page.goto(url);
return handle; return handle;