【问题标题】:Await function is not executed by DockerDocker 不执行等待函数
【发布时间】:2022-01-23 01:06:07
【问题描述】:

我正在使用 Docker 和 docker-compose 运行一个带有 puppeteer 的 NodeJS 抓取工具。我的故障排除如下:

预期输出: 我从维基百科获取标题页

案例 1: 使用 await 运行 function1() 时,进程停止

OUTPUT: 
Browser is running
//console.log("function1() end" ) does not execute

案例2: 如果 function1() 没有 await 则该函数不会执行,但 console.log after 会被执行

OUTPUT:
Browser is running
function1() end

如何使用 await 运行 function1 并获取页面输出的标题。


async function function1() {
  let page = await browser.newPage()
  await page.setUserAgent(
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
  )
  await page.goto(
    'https://en.wikipedia.org/wiki/Main_Page',
    {
      waitUntil: 'networkidle2',
    },
  )
  console.log(await page.title())
}


async function looper() {
  await function1()
  await console.log('function1 end')
}

async function startPuppeteer() {
  browser = await puppeteer.launch({
    headless: true,
    args: ['--no-sandbox', '--disable-setuid-sandbox'],
  })

  console.log('Browser is running')
  setInterval(looper, 30000)

}

Dockerfile


FROM buildkite/puppeteer:latest

USER root

COPY . /app

RUN cd /app && npm install 

EXPOSE 8000

WORKDIR /app

CMD npm run start

Docker-compose.yml

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8000:8000"

【问题讨论】:

  • 你能显示function1和function2的代码吗?你怎么知道他们没有被处决?你有console.log这些功能吗?
  • 在启动 headless chrome 后有一个 console.log 始终运行。刮板功能在没有 docker 的情况下执行,当使用 docker 时,await function1 和 await function2 不执行。此外,我编辑了帖子以显示 console.logs
  • 那么 function1 和/或 function2 中一定有一些东西阻止它运行。没有看到代码是不可能提供帮助的。
  • 我添加了main函数,谢谢大家的帮助

标签: javascript node.js docker docker-compose puppeteer


【解决方案1】:

在 headless 中使用 chrome 部署刮板时,使用 puppeteer 不起作用。我不得不切换到 heroku 并实现 webpacks。使用此答案的帮助:https://stackoverflow.com/a/55090914/7696261 我能够部署该服务。

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2021-02-25
    • 2019-12-30
    相关资源
    最近更新 更多