-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Base of timing out contexts * Docs --------- Co-authored-by: Alexander <[email protected]>
- Loading branch information
1 parent
edfb883
commit 8ef48e9
Showing
4 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const {BrowserContext} = require('puppeteer'); | ||
const loggers = require('./loggers'); | ||
|
||
/** | ||
* ContextId -> Timeout timer' IDs | ||
* | ||
* @type {{string: number}} | ||
*/ | ||
let contextTimeoutIds = {}; | ||
let contextTimeout; | ||
|
||
/** | ||
* Set timeout for context. | ||
* | ||
* @param {BrowserContext} context | ||
*/ | ||
function setContextTimeout(context) { | ||
const logger = loggers.getLogger(); | ||
|
||
contextTimeoutIds[context.id] = setTimeout( | ||
async () => { | ||
logger.warn(`Closing context ${context.id} due to timeout\n`); | ||
await context.close(); | ||
delete contextTimeoutIds[context.id]; | ||
}, | ||
contextTimeout); | ||
} | ||
exports.setContextTimeout = setContextTimeout; | ||
|
||
/** | ||
* The function clears context's timeout timer. | ||
* | ||
* @param {BrowserContext} context context to be cleared | ||
*/ | ||
function clearContextTimeout(context) { | ||
clearTimeout(contextTimeoutIds[context.id]); | ||
delete contextTimeoutIds[context.id]; | ||
} | ||
exports.clearContextTimeout = clearContextTimeout; | ||
|
||
/** | ||
* Update timeout for context. | ||
* | ||
* @param {BrowserContext} context | ||
*/ | ||
exports.updateContextTimeout = function updateContextTimeout (context) { | ||
clearContextTimeout(context); | ||
setContextTimeout(context); | ||
} | ||
|
||
/** | ||
* Init service that timeouts contexts after CONTEXT_TIMEOUT ms. | ||
* | ||
* @param {number} timeout | ||
*/ | ||
exports.initTimeoutContext = function initTimeoutContext (timeout) { | ||
contextTimeout = timeout; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters