使用 Sinatra Web 服务处理客户端超时
我是 Ruby 和 Sinatra 的初学者,但已经设法想出了一个在 Heroku 上运行得很好的 Web 服务。我从 Salesforce.com 访问此 Web 服务。
我在 Salesforce/Apex 中使用的 HTTPRequest 类的最大超时为 60 秒。如果我达到该超时(或者,当我达到用于测试目的的 1 秒超时时),我会在 Salesforce 端收到异常,我可以轻松处理该异常。我感兴趣的是如何在 Sinatra 方面处理这个问题。
如果我的客户端超时并以某种方式关闭连接,有没有办法在我的 Sintra 应用程序中“感知”这一点?我想记录客户端超时,继续执行应用程序已开始的工作,然后发送电子邮件让用户知道超时后作业已完成。
我应该注意到,当我现在确实超时时,Sinatra 应用程序很高兴地完成了它正在做的事情,并且我猜,返回了它应该返回的 JSON 数据。只是客户端没有任何东西可以获取该数据。
有什么想法吗?
I'm a rank beginner with Ruby and Sinatra, but have managed to come up with a web service that works pretty well, running on Heroku. I access this web service from Salesforce.com.
The HTTPRequest class that I use in Salesforce/Apex has a maximum timeout of 60s. If I hit that timeout (or, when I hit a 1 second timeout I'm using for testing purposes), I get an exception on the Salesforce side, which I can easily handle. What I'm interested in is how to handle this on the Sinatra side.
If my client gets a timeout, and somehow closes the connection, is there a way to "sense" this in my Sintra app? I'd like to note client timeout, continue doing the work the app had started, and then send an email to let the user know the job had finished after the timeout.
I should note that when I do get the timeout now, the Sinatra app happily finishes what it was doing, and, I'm guessing, returns the JSON data that it is supposed to. Only there's nothing on the client side to get that data.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的问题。作为无状态协议,我不认为 HTTP 包含“感知”客户端何时关闭连接的方法。我真的不知道 SalesForce 是做什么的,但这里有一些标准的 HTTP 解决方案(我假设 Web 套接字已经淘汰)。
最简单,但容易出现误报
由于您知道最大超时,因此请为您的 Sinatra 请求计时。如果花费的时间超过 60 秒,则认为超时并发送电子邮件。显然,这在 59-61 秒左右的任何地方都容易出错,并且您可能会得到一些误报和漏报。
更难,但容易趋于完美
您可以实现“已读回执”。您的 JSON 响应将包含 UID。如果您的 SalesForce 请求没有超时,请将 UID 作为收据发回。然后西纳特拉就会知道一切都好。
如果 Sinatra 应用程序在 n 秒/分钟内没有收到收据(因为 SalesForce 超时并且您从未获得 UID),Sinatra 应用程序可能会在 n 秒/分钟后发送电子邮件(或其他内容)。
这可以通过多种方式实现。最简单的可能涉及数据库、脚本和 cron。最困难的可能涉及 HTTP 流(现在在 Sinatra 1.3 中微不足道)以及可能的多线程或事件服务器,如 Thin 或 Zbattery。我很乐意详细说明。
Fun problem. As a stateless protocol, I do not believe HTTP includes a method of "sensing" when a client closes a connection. I really don't know anything about what SalesForce does, but here's some standard HTTP solutions (I'm assuming Web Sockets are out).
Easiest, but prone to false positives
Since you know the max timeout, time your Sinatra request. If it took longer than 60 sec, assume it timed out and send an email. Obviously this is prone to error anywhere around 59-61 seconds, and you may get some false positives and false negatives.
Harder, but prone to perfection
You could implement a "read receipt." Your JSON response would include a UID. If your SalesForce request doesn't timeout, send the UID back as a receipt. Then Sinatra would know everything was okay.
If the Sinatra app doesn't receive the receipt within n seconds/minutes (because SalesForce timed out and you never got the UID), the Sinatra app could send an email (or whatever) after n seconds/minutes.
This could be implemented several ways. The simplest probably involves a database, a script, and cron. The most difficult probably involves HTTP streaming (now trivial in Sinatra 1.3) and possibly multi-threaded or evented servers like Thin or Zbattery. I'd be happy to elaborate.