From 3bc8882f3ed0a65101172556f2bc4bc37fa37405 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Sun, 17 Oct 2021 20:27:07 -0700 Subject: [PATCH] Set timeout for shooter --- packages/skin-database/shooter.js | 15 +++++++++++++-- packages/skin-database/utils.ts | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/skin-database/shooter.js b/packages/skin-database/shooter.js index 54db0104..f2ebedf7 100644 --- a/packages/skin-database/shooter.js +++ b/packages/skin-database/shooter.js @@ -4,6 +4,7 @@ const puppeteer = require("puppeteer"); const imagemin = require("imagemin"); const imageminOptipng = require("imagemin-optipng"); const Skins = require("./data/skins"); +const { throwAfter } = require("./utils"); function min(imgPath) { return imagemin([imgPath], path.dirname(imgPath), { @@ -61,9 +62,19 @@ export default class Shooter { } } - async takeScreenshot(skin, screenshotPath, { minify = false, md5 }) { + async takeScreenshot( + skin, + screenshotPath, + { minify = false, timeout = 30000, md5 } + ) { try { - await this._takeScreenshot(skin, screenshotPath, { minify }); + await Promise.race([ + this._takeScreenshot(skin, screenshotPath, { minify }), + throwAfter( + `Screenshot did not complete within ${timeout / 1000} seconds.`, + timeout + ), + ]); } catch (e) { await Skins.recordScreenshotUpdate( md5, diff --git a/packages/skin-database/utils.ts b/packages/skin-database/utils.ts index 721db239..d40c4b90 100644 --- a/packages/skin-database/utils.ts +++ b/packages/skin-database/utils.ts @@ -21,3 +21,9 @@ export function chunk(items: T[], chunkSize: number): T[][] { export const MD5_REGEX = /([a-fA-F0-9]{32})/; export const TWEET_SNOWFLAKE_REGEX = /([0-9]{19})/; + +export function throwAfter(message, ms) { + return new Promise((resolve, reject) => { + setTimeout(() => reject(new Error(message)), ms); + }); +}