-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RESTful polling #158
base: master
Are you sure you want to change the base?
RESTful polling #158
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'em-http' | ||
require 'cobravsmongoose' | ||
require 'json' | ||
|
||
module Agents | ||
class RestPollingAgent < Agent | ||
cannot_be_scheduled! | ||
cannot_receive_events! | ||
continuous! | ||
|
||
description <<-MD | ||
Does long polling on a rest API url. The resulting type should be json or XML (which will be translated to json). | ||
MD | ||
|
||
event_description "REST event" | ||
|
||
def default_options | ||
{ | ||
'url' => "http://some.polling/url", | ||
'type' => "xml" | ||
} | ||
end | ||
|
||
def em_start | ||
t = Time.now | ||
http = EM::HttpRequest.new(options['url']).get | ||
http.errback { | ||
if Time.now - t > 0.1 | ||
em_start | ||
else | ||
EM.add_timer(1) { | ||
em_start | ||
} | ||
end | ||
} | ||
http.callback { | ||
puts http.response | ||
if options['type'] == "json" | ||
pld = JSON.parse(http.response) | ||
else | ||
pld = CobraVsMongoose.xml_to_hash(http.response) | ||
end | ||
create_event :payload => pld | ||
if Time.now - t > 0.1 | ||
em_start | ||
else | ||
EM.add_timer(1) { | ||
em_start | ||
} | ||
end | ||
} | ||
end | ||
|
||
def working? | ||
true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be something like
|
||
end | ||
|
||
def validate_options | ||
errors.add(:base, "url and type are required") unless options['type'].present? && options['url'].present? | ||
if options['type'] != 'json' && options['type'] != 'xml' | ||
errors.add(:base, "unrecognized type") | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
end | ||
|
||
require 'rufus/scheduler' | ||
require 'eventmachine' | ||
require 'set' | ||
|
||
class HuginnScheduler | ||
attr_accessor :mutex | ||
|
@@ -87,9 +89,23 @@ def run! | |
end | ||
end | ||
|
||
active_agents = Set.new | ||
EM.run do | ||
EM.add_periodic_timer(10) { | ||
Agent.find_each do |agent| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about removing Agents that no longer exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha! Good point :) I'll fix it. |
||
if agent.continuous? | ||
if active_agents.add? agent | ||
puts "Starting continuous agent" | ||
agent.em_start | ||
end | ||
end | ||
end | ||
} | ||
end | ||
|
||
rufus_scheduler.join | ||
end | ||
end | ||
|
||
scheduler = HuginnScheduler.new | ||
scheduler.run! | ||
scheduler.run! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CobraVsMongoose hans't been updated since 2006. Can you use a different gem, or, ideally, just standard library tools or nokogiri?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to use https://github.com/msievers/badgerfish ?
It is more recent but has less features (only does XML -> json conversion) which is ok for our purpose. I think they are both quite short and use standard libraries. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is nokogiri too much of an inconvenience?
On Thu, Feb 20, 2014 at 6:37 PM, slef notifications@github.com wrote: