Skip to content

Commit

Permalink
added heartbeat functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmgrant committed Feb 13, 2020
1 parent 39c0d61 commit 74eea92
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/launcher/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,33 @@ export function SaucelabsLauncher(args,
// Array of connected drivers. This is useful for quitting all connected drivers on kill.
let connectedDrivers: WebDriver[] = [];

// number of pending cancellations for heartbeat functionality
let pendingCancellations = 0;

// Setup Browser name that will be printed out by Karma.
this.name = browserName + ' on SauceLabs';

let pendingHeartBeat;
const formatSauceError = (err) => {
return err.message + '\n' + (err.data ? ' ' + err.data : '')
}
// Heartbeat function to keep alive sessions on Sauce Labs via webdriver JSON wire calls
const heartbeat = () => {
pendingHeartBeat = setTimeout( (driver) => {
log.debug('Heartbeat to Sauce Labs (%s) - fetching title', browserName)

driver.title()
.then(null, (err) => {
log.error('Heartbeat to %s failed\n %s', browserName, formatSauceError(err))

clearTimeout(pendingHeartBeat)
return this._done('failure')
});

heartbeat()
}, 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 @@ -74,7 +98,13 @@ export function SaucelabsLauncher(args,
browserMap.set(this.id, {sessionId, username, accessKey, proxy: sauceApiProxy});

await driver.get(pageUrl);
heartbeat();
} catch (e) {
// determine if there are still pending cancellations based on the heartbeat
if (pendingCancellations > 0) {
pendingCancellations--;
return;
}
log.error(e);

// Notify karma about the failure.
Expand All @@ -83,6 +113,11 @@ export function SaucelabsLauncher(args,
});

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

try {
await Promise.all(connectedDrivers.map(driver => driver.quit()));
} catch (e) {
Expand Down

0 comments on commit 74eea92

Please sign in to comment.