【问题标题】:Checking the status of Apify during the execution of any process在任何进程执行期间检查 Apify 的状态
【发布时间】:2020-12-21 16:20:07
【问题描述】:

假设一个服务器

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 云。

【问题讨论】:

    标签: node.js express apify


    【解决方案1】:

    您需要将所有代码包装在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多