检查我的 PHP beanstalkd 后台进程的状态
我有一个用 PHP (CakePHP) 编写的网站,其中某些资源密集型任务由后台进程处理。这是通过 Beanstalkd 消息队列完成的。我需要某种方法来检索该后台进程的状态,以便我可以使用 Monit 监视它。
后台进程是一个与 Beanstalkd 通信的 CakePHP Shell(只是一个 PHP CLI 脚本)。它只是在 Benastalkd 上执行 Reserve() 并等待新消息。当它收到消息时,它会处理它。我想要某种方式使用 Monit 监视此进程,以便在出现问题时可以重新启动后台进程。
到目前为止,我一直在考虑编写一个 PHP CLI 脚本,在 Beanstalkd 中删除一条消息。后台进程接收消息并以某种方式将其内部状态传达回 CLI 脚本。但如何呢?插座?共享内存?还有其他IPC方法吗?
或者我在这里可能太复杂了,有没有更简单的方法来使用 Monit 监视这样的过程?
提前致谢!
I have a website written in PHP (CakePHP) where certain resource intensive tasks are handled by a background process. This is done through the Beanstalkd message queue. I need some way to retrieve the status of that background process so I can monitor it with Monit.
The background process is a CakePHP Shell (just a PHP CLI script) that communicates with Beanstalkd. It simply does a reserve() on Benastalkd and waits for a new message. When it gets a message, it processes it. I want some way of monitoring this process with Monit so that it can restart the background process if something has gone wrong.
What I have been thinking about so far is writing a PHP CLI script that drops a message in Beanstalkd. The background process picks up the message and somehow communicates it's internal status back to the CLI script. But how? Sockets? Shared memory? Some other IPC method?
Or am I perhaps being too complicated here and is there a much easier way to monitor such a process with Monit?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我最终所做的。
CLI 脚本连接到 beanstalkd,创建一个新队列 (tube) 并开始监视它。然后,它会删除后台守护程序正在监视的队列中的最高优先级消息。该消息包含 CLI 脚本正在监视的新队列的名称。
后台进程几乎立即收到此消息(因为它具有最高优先级),生成状态消息并将其放入 CLI 脚本正在监视的队列中。 CLI 脚本接收它,然后关闭队列。
当 CLI 脚本在 30 秒内未得到响应时,它将退出并显示错误,指示后台守护程序(很可能)挂起。
我将所有这些都与 Monit 联系起来。 Monit 现在可以检查后台守护进程是否正在运行(通过 pidfile 和进程列表)并验证它实际上仍在处理消息(通过使用 CLI 工具测试它是否响应状态请求)
Here's what I ended up doing in the end.
The CLI script connects to beanstalkd, creates a new queue (tube) and starts watching it. Then it drops a highest priority message in the queue that the background daemon is watching. That message contains the name of the new queue that the CLI script is monitoring.
The background process receives this message almost immediately (because it is highest priority), generates a status message and puts it in the queue that the CLI script is watching. The CLI script receives it and then closes the queue.
When the CLI script does not get a response in 30 seconds it will exit with an error indicating the background daemon is (most likely) hung.
I tied all this into Monit. Monit can now check that the background daemon is running (via the pidfile and process list) and verify that it is actually still processing messages (by using the CLI tool to test that it responds to status requests)
可能有一个 Monit 或 Nagios 插件可以连接、运行统计数据并在“太多”时返回。目前还没有为此编写的“协议”,但修改现有的基于文本的协议(如 nntp 或 smtp)来执行您想要的操作似乎并不困难。但从表面上看,它确实意味着用 C 语言编写。
在 CLI-PHP 脚本中,我将通过两种不同方法中的一种(或两种)来完成此操作。
1/ 将(低)优先级消息放入队列中,并确保它在几秒钟内返回。将其放入专用队列并确保在放入之前没有任何内容也是一个很好的补充。
2/ 执行“统计”并查看有多少人正在等待:“当前作业就绪”。
要将信息返回到网站(无论哪种方式),您可以写入文件或写入 Memcached 之类的文件中,gts 会读取并执行操作。
There probably is a plugin to Monit or Nagios to connect, run the stats and return if there are 'too many'. There isn't a 'protocol' written already for that, but t doesn't appear to be exceeding difficult to modify an existing text-based one (like nntp, or smtp) to do what you want. It does mean writing it in C though, by the looks of it.
From a CLI-PHP script, I would go about it through one (or both) of two different methods.
1/ drop a (low-ish) priority message into the queue, and make sure it comes back within a few seconds. Putting it into a dedicated queue and making sure there's nothing there before you put it in there would be a good addition as well.
2/ perform a 'stats' and see how many are waiting: 'current-jobs-ready'.
To get the information back to a website (either way), you can write to a file, or into something like Memcached which gts read and acted upon.