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.

dda
  • 6,030
  • 2
  • 25
  • 34
Andrey
  • 191
  • 2
  • 6

2 Answers2

0

Here's a snippet to get you started. It uses the em-http-request gem to perform a GET request and retries if a 404 status code is returned after a small delay. You should be able to build what you have asked using this code as a base.

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
Tim Potter
  • 2,437
  • 21
  • 31
0

You'll need persistent queues and you'll need a way to manage and monitor exceptions which probably means EventMachine isn't the right tool for the job. Try some background job service, see https://www.ruby-toolbox.com/categories/Background_Jobs

If you're going for high volume of simple tasks and a reactor-like pattern then bealnstalkd is probably the way to go, see:

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

bbozo
  • 7,075
  • 3
  • 30
  • 56