gpt4 book ai didi

javascript - Nightmare Js 多次评估后评估超时

转载 作者:太空宇宙 更新时间:2023-11-04 01:39:31 27 4
gpt4 key购买 nike

我正在尝试设置一个我一直在开发的应用程序,其中涉及记录 HTML 小部件(带有动画)并将其转换为 mp4。在本地效果很好。我使用 Nightmare Js 处理屏幕截图,并使用 FFMPEG 将屏幕截图转换为 mp4,这在我的 PC 上大约需要 90 秒。

我通过托管服务将其设置在 Docker 容器中,但无法解决此问题。我现在的测试脚本看起来像这样:

const Nightmare = require('nightmare')
const Xvfb = require('xvfb')

module.exports = function(app) {
app.get('/api/nightmare', async (req, res) => {
console.log('GET request to /api/nightmare')

try {
// Start xvfb and create our nightmare object
const close = await xvfb()
const nightmare = Nightmare({ executionTimeout: 1000 })

const [err, result] = await poss(run(nightmare))
if (err) {
// Cleanup before throwing error
await nightmare.end()
await close()
throw err
}

// shut'er down
await nightmare.end()
await close()
res.status(200).send('Completed')
} catch (error) {
console.log(error)
res.status(500).send(error)
}
})
}

async function run(nightmare) {
var data = {
'id': '2OWBTMUL',
'somedata': 'blah'
// More data pulled from the database goes here
}

// Create folder to hold images
var procName = new Date().getTime() + '_testing_' + data.id
fs.mkdirSync(path.resolve(__dirname, '../processing/', procName))

// URL to inject into webpage so the webpage can access the data
var url = `http://example.com?` +
`id=${data.id}&` +
`somedata=${data.somedata}`

console.log('Starting NightmareJs')
await nightmare
.viewport(1920, 1080)
.goto('file:///' + path.resolve(__dirname, '../templates/example.html'))
.evaluate(newUrl => {
// Set url and get the page ready for recording
url = new URL(newUrl)
initiate()
start()
timeline.pause()
}, url)
.catch(error => { console.log(error) })

// Take 200 screenshots (8s @ 25fps)
var frames = 200
for (var i = 0; i < frames; i++) {
console.log('Taking screenshot ' + i)

await nightmare
.screenshot(path.resolve(__dirname, '../processing/', procName, 'screen_' + i + '.png'))
.evaluate(shot => { timeline.seek((8 / 200) * shot) }, i)
.catch(error => { console.log(error) })
}

console.log('Done.')
}

// xvfb wrapper
function xvfb(options) {
var xvfb = new Xvfb(options)

function close() {
return new Promise((resolve, reject) => {
xvfb.stop(err => (err ? reject(err) : resolve()))
})
}

return new Promise((resolve, reject) => {
xvfb.start(err => (err ? reject(err) : resolve(close)))
})
}

// try/catch helper
async function poss(promise) {
try {
const result = await promise
return [null, result]
} catch (err) {
return [err, null]
}
}

一些环境详细信息:
Ubuntu 18.04.1
主管
Node 8.12.0(DEBUG=* 设置)

问题:大约 17 次循环后,Nightmare 抛出两个不同的错误。在我的 nodejs-stderr.log 文件中,我得到了这个

Taking screenshot x
Error: Evaluation timed out after 1000msec. Are you calling done() or resolving your promises?

在评估中使用done()回调或promise进行测试没有改变任何东西,所以我保持简单

在我的 nodejs-stdout.log 文件中,每次成功的循环都会得到此信息

Mon, 19 Nov 2018 09:37:06 GMT nightmare:actions .screenshot()
Mon, 19 Nov 2018 09:37:06 GMT nightmare:log subscribing to browser window frames
Mon, 19 Nov 2018 09:37:06 GMT nightmare:log Highlighting page to trigger rendering.
Mon, 19 Nov 2018 09:37:06 GMT nightmare:log unsubscribing from browser window frames
Mon, 19 Nov 2018 09:37:07 GMT nightmare:actions .screenshot() captured with length 1092963
Mon, 19 Nov 2018 09:37:07 GMT nightmare:actions .evaluate() fn on the page
Mon, 19 Nov 2018 09:37:07 GMT nightmare queueing action "screenshot"
Mon, 19 Nov 2018 09:37:07 GMT nightmare queueing action "evaluate"
Mon, 19 Nov 2018 09:37:07 GMT nightmare running

然后经过 17 个左右的循环后,我得到:

Mon, 19 Nov 2018 09:37:07 GMT nightmare:actions .screenshot()
Mon, 19 Nov 2018 09:37:07 GMT nightmare:log subscribing to browser window frames
Mon, 19 Nov 2018 09:37:07 GMT nightmare:log Highlighting page to trigger rendering.
Tue, 20 Nov 2018 02:11:43 GMT nightmare:log crashed [{},false]
Mon, 19 Nov 2018 09:37:12 GMT nightmare:log FrameManager timing out after 1000 ms with no new rendered frames
Mon, 19 Nov 2018 09:37:12 GMT nightmare:log unsubscribing from browser window frames
Mon, 19 Nov 2018 09:37:12 GMT nightmare:actions .screenshot() captured with length 0
Mon, 19 Nov 2018 09:37:12 GMT nightmare:actions .evaluate() fn on the page
Mon, 19 Nov 2018 09:37:13 GMT nightmare queueing action "screenshot"
Mon, 19 Nov 2018 09:37:13 GMT nightmare queueing action "evaluate"
Mon, 19 Nov 2018 09:37:13 GMT nightmare running

其余的全部失败,没有 crashed [{},false] 消息。
在循环中删除 .evaluate(shot => {timeline.seek((8/200) * shot) }, i) 可以解决问题,但我需要它,否则它只是一个静止的视频!我尝试过单独的调用(不是链接评估和屏幕截图),将 .wait(200) 放在东西之间,但我无法弄清楚或找到有关此问题的任何帮助。

最佳答案

原来是共享内存太小了。我假设当图像全部加载时,它超出了内存限制并且 Electron 崩溃了。将共享内存从64MB增加到128MB后,问题解决。

关于javascript - Nightmare Js 多次评估后评估超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com