Skip to content

Commit

Permalink
feat: add heartbeat to keep session alive every 60 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
wswebcreation committed Oct 28, 2020
1 parent 76ad432 commit c17711a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/launcher/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ export function SaucelabsLauncher(args,
// Setup Browser name that will be printed out by Karma.
this.name = browserName + ' on SauceLabs';

let pendingHeartBeat;
// Heartbeat function to keep alive sessions on Sauce Labs via WebDriver calls
const heartbeat = ()=> {
const driver = connectedDrivers.get(this.id);

pendingHeartBeat = setTimeout( async () => {
if(driver) {
try {
await driver.getTitle();
log.debug('Heartbeat to Sauce Labs (%s) - fetching title', browserName)
heartbeat();
} catch (ign) {
// Do nothing, just clear the timeout
clearTimeout(pendingHeartBeat)
}
}
return;
}, 60000);
}

// Listen for the start event from Karma. I know, the API is a bit different to how you
// would expect, but we need to follow this approach unless we want to spend more work
// improving type safety.
Expand Down Expand Up @@ -76,6 +96,7 @@ export function SaucelabsLauncher(args,
});

await driver.url(pageUrl);
heartbeat();
} catch (e) {
log.error(e);

Expand All @@ -85,6 +106,11 @@ export function SaucelabsLauncher(args,
});

this.on('kill', async (done: () => void) => {
// If there is still pending heartbeats, clear the timeout
if (pendingHeartBeat) {
clearTimeout(pendingHeartBeat);
}

try {
const driver = connectedDrivers.get(this.id);
await driver.deleteSession();
Expand Down

0 comments on commit c17711a

Please sign in to comment.