如何定期从node.js服务器轮询另一台服务器? [英] How to poll another server periodically from a node.js server?
问题描述
我有一个mongodb数据库的node.js服务器 A . 还有另一个远程服务器 B (不需要基于节点),它公开了HTTP/GET API'/status'并返回'FREE'或'BUSY'作为响应.
I have a node.js server A with mongodb for database. There is another remote server B (doesn't need to be node based) which exposes a HTTP/GET API '/status' and returns either 'FREE' or 'BUSY' as the response.
当用户点击服务器 A 中的特定API端点(例如POST/test)时,我希望每分钟开始轮询服务器 B 的状态API,直到服务器 B 返回"FREE"作为响应.用户不需要等到服务器 B 返回"FREE"响应(轮询B是服务器A中的后台作业).一旦服务器 A 从 B 获得免费"响应,它将向用户发送电子邮件.
When a user hits a particular API endpoint in server A(say POST /test), I wish to start polling server B's status API every minute, until server B returns 'FREE' as the response. The user doesn't need to wait till the server B returns a 'FREE' response (polling B is a background job in server A). Once the server A gets a 'FREE' response from B, it shall send out an email to the user.
要如何在服务器 A 中实现这一点,请记住并发用户数可以增加很多?
How can this be achieved in server A, keeping in mind that the number of concurrent users can go large ?
推荐答案
我建议您使用议程. https://www.npmjs.com/package/agenda 使用议程,您可以创建定期计划,在该计划下您可以安排任何非常灵活的计划.
I suggest you use Agenda. https://www.npmjs.com/package/agenda With agenda you can create recurring schedules under which you can schedule anything pretty flexible.
我建议您使用请求模块发出HTTP获取/发布请求. https://www.npmjs.com/package/request
I suggest you use request module to make HTTP get/post requests. https://www.npmjs.com/package/request
这篇关于如何定期从node.js服务器轮询另一台服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!