Skip to content

Commit

Permalink
fix: retry drawing if workers crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 27, 2024
1 parent 00a2bce commit eb5da64
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
37 changes: 31 additions & 6 deletions lib/Graphics/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { isPackaged } from '../Resources/Util.js'
import { fileURLToPath } from 'url'
import path from 'path'

const CRASHED_WORKER_RETRY_COUNT = 10

/**
* @typedef {{
* page_direction_flipped: boolean,
Expand Down Expand Up @@ -159,12 +161,12 @@ class GraphicsController extends CoreBase {
render = this.#renderLRUCache.get(key)

if (!render) {
const { buffer, width, height, dataUrl, draw_style } = await this.#pool.exec('drawButtonImage', [
this.#drawOptions,
const { buffer, width, height, dataUrl, draw_style } = await this.#executePoolDrawButtonImage(
buttonStyle,
location,
pagename,
])
CRASHED_WORKER_RETRY_COUNT
)
render = GraphicsRenderer.wrapDrawButtonImage(buffer, width, height, dataUrl, draw_style, buttonStyle)
}
} else {
Expand Down Expand Up @@ -252,10 +254,12 @@ class GraphicsController extends CoreBase {
size: buttonStyle.size === 'auto' ? 'auto' : Number(buttonStyle.size),
}

const { buffer, width, height, dataUrl, draw_style } = await this.#pool.exec('drawButtonImage', [
this.#drawOptions,
const { buffer, width, height, dataUrl, draw_style } = await this.#executePoolDrawButtonImage(
drawStyle,
])
undefined,
undefined,
CRASHED_WORKER_RETRY_COUNT
)
return GraphicsRenderer.wrapDrawButtonImage(buffer, width, height, dataUrl, draw_style, drawStyle)
}

Expand Down Expand Up @@ -397,6 +401,27 @@ class GraphicsController extends CoreBase {

return GraphicsRenderer.drawBlank(this.#drawOptions, location)
}

/**
* Draw a button image in the worker pool
* @param {import('../Shared/Model/StyleModel.js').DrawStyleModel} drawStyle The style to draw
* @param {import('../Resources/Util.js').ControlLocation | undefined} location
* @param {string | undefined} pagename
* @param {number} remainingAttempts
* @returns {Promise<{ buffer: Buffer, width: number, height: number, dataUrl: string, draw_style: import('../Shared/Model/StyleModel.js').DrawStyleModel['style'] | undefined}>} Image render object
*/
async #executePoolDrawButtonImage(drawStyle, location, pagename, remainingAttempts) {
try {
return this.#pool.exec('drawButtonImage', [this.#drawOptions, drawStyle, location, pagename])
} catch (/** @type {any} */ e) {
// if a worker crashes, the first attempt will fail, retry when that happens, but not infinitely
if (remainingAttempts > 1 && e?.message?.includes('Worker is terminated')) {
return this.#executePoolDrawButtonImage(drawStyle, location, pagename, remainingAttempts - 1)
} else {
throw e
}
}
}
}

export default GraphicsController
Expand Down
1 change: 0 additions & 1 deletion lib/Resources/EventDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ export function visitEventOptions(visitor, event) {
}

for (const key of Object.keys(event.options || {})) {
console.log('check', key, event.options[key])
visitor.visitString(event.options, key)
}
}

0 comments on commit eb5da64

Please sign in to comment.