在任何进程执行期间检查 Apify 的状态

Checking the status of Apify during the execution of any process

让我们假设一个服务器

const Apify = require("apify");
const express = require("express");

const app = express();

app.get("/run", async (_, res) => {
  Apify.main(async () => {
    // Let the actor run for an hour.
    await Apify.utils.sleep(60 * 60 * 1000);
  });
  res.redirect("/");
});

app.get("/state", async (req, res) => {
  const pageHtml = `

<html>
    <head><title>Example</title></head>
    <body>
       <pre>${JSON.stringify(Apify.utils.log)}</pre>
    </body>
</html>
`;
  res.send(pageHtml);
});

app.listen(3000, () => {
  console.log(`Application is listening at URL ${APIFY_CONTAINER_URL}.`);
});

它除了 expressapify 工作之外什么都不做。

如何捕捉 Apify 的状态?

我需要知道

没有 Apify 云。

您需要将所有代码包装在 Apify.main:

const Apify = require("apify");
const express = require("express");

Apify.main(async () => {
    const app = express();

    app.get("/run", async (_, res) => {
      res.redirect("/");
    });
    
    app.get("/state", async (req, res) => {
      const pageHtml = `
    
    <html>
        <head><title>Example</title></head>
        <body>
           <pre>${JSON.stringify(Apify.utils.log)}</pre>
        </body>
    </html>
    `;
      res.send(pageHtml);
    });
    
    app.listen(3000, () => {
      console.log(`Application is listening at URL ${APIFY_CONTAINER_URL}.`);
    });
    
    await new Promise(() => {}); // wait forever    
});

您可以在此处使用最低限度的示例以供将来参考https://github.com/metalwarrior665/apify-utils/blob/master/examples/express-server.js