检查使用twistd启动的Twisted Server是否启动成功

发布于 2024-12-06 17:40:03 字数 1022 浏览 0 评论 0原文

我需要一种可靠的方法来检查通过 twind(和 TAC 文件)启动的基于 Twisted 的服务器是否已成功启动。它可能会失败,因为某些网络选项设置错误。由于我无法访问twistd日志(因为它记录到/dev/null,因为我不需要 log-clutter twind 产生),我需要查明服务器是否在包装 twind 调用的启动脚本中成功启动。

启动脚本是一个像这样的 Bash 脚本:

#!/usr/bin/bash
twistd \
  --pidfile "myservice.pid" \
  --logfile "/dev/null" \
  --python \
  myservice.tac

我在网上找到的都是一些 使用 ps 或类似的东西进行黑客攻击。但我不喜欢这样的方法,因为我认为它不可靠。

所以我在想是否有一种方法可以访问Twisted的内部,并获取所有当前正在运行的Twisted应用程序?这样我就可以查询当前运行的应用程序以获取要启动的 Twisted 应用程序的名称(正如我在 TAC 文件中命名的那样)。

我还在考虑不使用twistd可执行文件,而是实现一个基于Python的启动脚本,其中包含twistd内容,例如 这个问题的答案提供了,但我不知道这是否有助于我获取服务器运行的状态。

所以我的问题只是:当twistd日志记录被禁用时,是否有一种可靠且不丑陋的方法来判断使用twistd启动的Twisted服务器是否已成功启动?

I need a reliable way to check if a Twisted-based server, started via twistd (and a TAC-file), was started successfully. It may fail because some network options are setup wrong. Since I cannot access the twistd log (as it is logged to /dev/null, because I don't need the log-clutter twistd produces), I need to find out if the Server was started successfully within a launch-script which wraps the twistd-call.

The launch-script is a Bash script like this:

#!/usr/bin/bash
twistd \
  --pidfile "myservice.pid" \
  --logfile "/dev/null" \
  --python \
  myservice.tac

All I found on the net are some hacks using ps or stuff like that. But I don't like an approach like that, because I think it's not reliable.

So I'm thinking about if there is a way to access the internals of Twisted, and get all currently running Twisted applications? That way I could query the currently running apps for the the name of my Twisted application (as I named it in the TAC-file) to start.

I'm also thinking about not using the twistd executable but implementing a Python-based launch script which includes the twistd-content, like the answer to this question provides, but I don't know if that helps me in getting the status of the server to run.

So my question is just: is there a reliable not-ugly way to tell if a Twisted Server started with twistd was started successfully, when twistd-logging is disabled?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2024-12-13 17:40:03

您明确指定了 PID 文件。 twistd 会将其 PID 写入该文件。您可以检查系统是否存在具有该 PID 的进程。

您还可以使用自定义日志观察器重新启用日志记录,该观察器仅记录您的启动事件并丢弃所有其他日志消息。然后您可以查看启动事件的日志。

另一种可能性是向您的应用程序添加另一个服务器,该服务器公开您提到的内部结构。然后尝试连接到该服务器并环顾四周以查看您想要看到的内容(不过,服务器正在运行这一事实似乎很好地表明该进程已正确启动)。如果你把它变成沙井服务器,那么你就能够评估任意Python代码,这让你可以检查你想要的进程中的任何状态。

您还可以让应用程序代码写出一个额外的状态文件,明确指示成功启动。确保在启动应用程序之前将其删除,这样您就可以清楚地了解成功与失败。

You're explicitly specifying a PID file. twistd will write its PID into that file. You can check the system to see if there is a process with that PID.

You could also re-enable logging with a custom log observer which only logs your startup event and discards all other log messages. Then you can watch the log for the startup event.

Another possibility is to add another server to your application which exposes the internals you mentioned. Then try connecting to that server and looking around to see what you wanted to see (just the fact that the server is running seems like a good indication that the process started up properly, though). If you make it a manhole server then you get the ability to evaluate arbitrary Python code, which lets you inspect any state in the process you want.

You could also just have your application code write out an extra state file that explicitly indicates successful startup. Make sure you delete it before starting the application and you'll have a fine indicator of success vs failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文