0

I need to resolve this task.

I need to develop a service, which delivers HTTP GET requests with guarantee using EventMachine.

  • Service has API /send? url=http%3A%2F%2Fya.ru, where url it is http request which is need to be delivered/

On every request the API responds at once 200 with body OK. After this, the service tries to deliver all requests. If answer 200 is received, then the request is considered as delivered. If the request failed, then try to repeat later.

  • Service has function /stats, which shows a diagram with 'highcharts' where we see three lines - received requests, successful requests, failed requests.

The step is 1 second. The code should be covered with tests rspec/cucumber. Is it possible if the service is not persistent, it will loose data after restart?

Any help will be greatly appreciated. I don't know where to begin. But I have some experience with building applications using RoR. I'll be pleased if someone can give me really similar task which is solved already.

4

2 回答 2

0

这是一个片段,可以帮助您入门。它使用em-http-requestgem 来执行一个 GET 请求,如果在一小段延迟后返回 404 状态码,它会重试。您应该能够使用此代码作为基础来构建您所要求的内容。

require "eventmachine"
require "em-http"

def do_get_request(url)

  request = EM::HttpRequest.new(url).get

  request.callback do

    status = request.response_header.status

    if status == 404
      puts "Request #{url} failed with status #{status}, retrying"
      EM.add_timer(1) do
        do_get_request(url)
      end
    else
      # Process request.response
    end

  end
end

EM.run do
  do_get_request(ARGV[0])
end
于 2013-02-09T21:04:34.037 回答
0

您将需要持久队列,并且需要一种管理和监视异常的方法,这可能意味着 EventMachine 不是适合该工作的工具。尝试一些后台作业服务,请参阅https://www.ruby-toolbox.com/categories/Background_Jobs

如果您要执行大量简单任务和类似反应器的模式,那么 bealnstalkd 可能是要走的路,请参阅:

http://kr.github.io/beanstalkd/ http://railscasts.com/episodes/243-beanstalkd-and-stalker

于 2013-11-13T10:17:19.727 回答