开发者

API polling bot on heroku

I want to create a bot which makes API request per minute开发者_Python百科 to some API url. This then needs to ping a particular user if data entry has changed against his name in the API feed. I want to go for a free solution on Heroku. Can this be achieved?


Yes, heroku supports thin as a web server, which is EventMachine enabled, so an easy way to do this is to write a quick sinatra app and use EM.add_periodic_timer for your API calls. When you deploy this sinatra app to heroku, it'll use thin by default, so there's no extra configuration needed. You can test via thin start -p 4567 assuming your config.ru is correct. Here's a pretty standard one, assuming your app is in app.rb:

require 'bundler/setup'
Bundler.require :default
require File.expand_path('app', File.dirname(__FILE__))

run Sinatra::Application


I currently check the status of some sites for free on heroku. The secret? Rufus-Scheduer.

Install the gem

 gem install rufus-scheduler

Make sure you include the gem in bundler or however you are including it.

Then you need to create a file called task_scheduler.rb and stick this in you initializers directory.

 require 'rufus/scheduler'
 scheduler = Rufus::Scheduler.start_new

 scheduler.every '1m' do
    url = "http://codeglot.com"
    response = Net::HTTP.get_response(URI.parse(url))
    #do stuff with response.body
 end

If you have any trouble you can see this blog post:

http://intridea.com/2009/2/13/dead-simple-task-scheduling-in-rails?blog=company

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜