Skip to content

Commit

Permalink
Merge branch '4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Good committed Oct 19, 2020
2 parents 6bce22b + 8a20175 commit e03c3a7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1,138 deletions.
47 changes: 26 additions & 21 deletions lib/exportJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ const DEFAULT_OPTIONS = {
// Use the maid to ensure we don't leak windows
setInterval(WindowMaid.cleanupHungWindows, HUNG_WINDOW_CLEANUP_INTERVAL)

const ipcMessagePromises = {}
const ipcMessagePromiseResolveFunctions = {}

if (electron.ipcMain) { // Not available or needed for tests
electron.ipcMain.on('process-stats', (event, stats) => {
const { messageId, windowId } = stats
const job = WindowMaid.getCacheEntry(windowId).job
const promise = ipcMessagePromises[messageId]
if (promise) {
const resolveFn = ipcMessagePromiseResolveFunctions[messageId]
if (resolveFn) {
job.debug(`resolving promise for messageId: ${messageId}`)
promise(stats)
delete ipcMessagePromises[messageId]
resolveFn(stats)
delete ipcMessagePromiseResolveFunctions[messageId]
} else {
job.info('no promise was found for process-stats message:', messageId)
}
job.info('renderer-process-stats', JSON.stringify(stats, undefined, 2))
})
Expand Down Expand Up @@ -393,7 +395,9 @@ class ExportJob extends EventEmitter {
s.setUserAgent(s.getUserAgent(), this.args.acceptLanguage)
}

this.emit(`${RENDER_EVENT_PREFIX}loadurl`, { url: url })
const event = `${RENDER_EVENT_PREFIX}loadurl`
this.emit(event, { url: url })
this._triggerProcessStats(event)
window.loadURL(wargs.urlWithArgs(url, null), loadOpts)
}

Expand Down Expand Up @@ -554,12 +558,14 @@ class ExportJob extends EventEmitter {
)`

// Don't let a ready event hang, set a max timeout interval
const f = this._cancelReadyEvent.bind(this, eventName, ipcListener, generateFunction)
const f = this._proceedWithExport.bind(this, eventName, ipcListener, generateFunction)
const maxWait = this.getMaxWait()
const timeout = setTimeout(f, maxWait)

// clear the timeout as soon as we get the ready event from the browser
this.once('window.event.wait.end', () => clearTimeout(timeout))
// Or if the window was closed because it was hung for too long
this.once('window.termination', () => clearTimeout(timeout))

window.webContents.executeJavaScript(`ipcApi.initialize()`)
window.webContents.executeJavaScript(cmd)
Expand All @@ -583,7 +589,7 @@ class ExportJob extends EventEmitter {
* window
* @private
*/
_cancelReadyEvent (eventName, ipcListener, generateFunction) {
_proceedWithExport (eventName, ipcListener, generateFunction) {
this.emit('window.event.wait.timeout', { eventName: eventName })
electron.ipcMain.removeListener(IPC_MAIN_CHANNEL_RENDER, ipcListener)
generateFunction()
Expand All @@ -598,7 +604,6 @@ class ExportJob extends EventEmitter {
* @private
*/
_attachIPCListener (eventName, generateFunction) {
this._triggerProcessStats('window.event.wait.start')
this.emit('window.event.wait.start', { eventName: eventName })
const listener = (name, jobId, customEventDetail) => {
// Multiple listeners could be active concurrently,
Expand Down Expand Up @@ -667,12 +672,12 @@ class ExportJob extends EventEmitter {
const messageId = `${this.jobId}:${event}`
this.debug(`invoking ipcApi.eventStats for messageId: ${messageId}`)

//_.invoke used to accommodate testing
// _.invoke used to accommodate testing
_.invoke(this.window, 'webContents.executeJavaScript',
`ipcApi.eventStats('${messageId}','${this.window.id}','${event}')`)
`ipcApi.eventStats('${messageId}','${this.window.id}','${event}')`)

return new Promise(resolve => {
ipcMessagePromises[messageId] = resolve
ipcMessagePromiseResolveFunctions[messageId] = resolve
})
} else {
return Promise.resolve()
Expand Down Expand Up @@ -703,15 +708,15 @@ class ExportJob extends EventEmitter {
dim && this.emit('window.resize', dim)

this.emit('window.capture.start', {})
this._triggerProcessStats('window.capture.start')

if (outputFile.toLowerCase().endsWith('.png')) {
this._captureImage(window, outputFile, outputDoneFn)
} else if (outputFile.toLowerCase().endsWith('.html')) {
this._captureHtml(window, outputFile, outputDoneFn)
} else {
this._capturePDF(this.args, window, outputDoneFn, outputFile)
}
this._triggerProcessStats('window.capture.start').then(() => {
if (outputFile.toLowerCase().endsWith('.png')) {
this._captureImage(window, outputFile, outputDoneFn)
} else if (outputFile.toLowerCase().endsWith('.html')) {
this._captureHtml(window, outputFile, outputDoneFn)
} else {
this._capturePDF(this.args, window, outputDoneFn, outputFile)
}
})
}
}

Expand Down
14 changes: 10 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PDFExporter extends EventEmitter {
}

/**
* https://github.com/electron/electron/blob/master/docs/api/chrome-command-line-switches.md
* https://www.electronjs.org/docs/api/command-line-switches
*/
configureElectron () {
// Prevents Chromium from lowering the priority of invisible pages' renderer processes. This flag is global to all renderer processes
Expand All @@ -101,9 +101,15 @@ class PDFExporter extends EventEmitter {
const maxMem = process.env.ELECTRONPDF_RENDERER_MAX_MEMORY ||
// 75% of the memory of the server, up to a ceiling of 8GB
// The default is 512MB, this should be more than enough for a single export process
Math.min(8192, Math.round(convertBytesToMB(os.totalmem() * .75)))
electronApp.commandLine.appendSwitch('js-flags', `--max-old-space-size=${maxMem}`)
this.info(`Assigned renderer process js-flags=--max-old-space-size=${maxMem}`)
Math.min(8192, Math.round(convertBytesToMB(os.totalmem() * 0.75)))

// V8 (only) Flags are supported by Chromium; Node flags no worky; run `node --v8-options`
// --prof ; creates a file like isolate-0x7fa6c0008000-v8.log in the local directory
// then run: node --prof-process isolate-0x7fe248008000-v8.log > isolate-0x7fe248008000-v8.txt
const jsFlags = `--max-old-space-size=${maxMem}`

electronApp.commandLine.appendSwitch('js-flags', jsFlags)
this.info(`Assigned renderer process js-flags=${jsFlags}`)
}

stop () {
Expand Down
2 changes: 1 addition & 1 deletion lib/windowMaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const windowMaid = {
* @param {Number} id
* @returns {id,job,window}
*/
getCacheEntry(id) {
getCacheEntry (id) {
return windowCache[id]
},

Expand Down
Loading

0 comments on commit e03c3a7

Please sign in to comment.