Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ gem "twitter"
gem 'twitter-stream', '>=0.1.16'
gem 'em-http-request'
gem 'weibo_2'
gem 'cobravsmongoose'
Copy link
Member

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?

Copy link
Author

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?

Copy link
Member

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:

In Gemfile:

@@ -36,6 +36,7 @@ gem "twitter"
gem 'twitter-stream', '>=0.1.16'
gem 'em-http-request'
gem 'weibo_2'
+gem 'cobravsmongoose'

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?

Reply to this email directly or view it on GitHubhttps://github.com//pull/158/files#r9934744
.


platforms :ruby_18 do
gem 'system_timer'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GEM
kaminari (>= 0.13)
rails (>= 3.1)
builder (3.0.4)
cobravsmongoose (0.0.2)
coderay (1.0.9)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
Expand Down Expand Up @@ -270,6 +271,7 @@ DEPENDENCIES
better_errors
binding_of_caller
bootstrap-kaminari-views
cobravsmongoose
coffee-rails (~> 3.2.1)
coveralls
daemons
Expand Down
16 changes: 16 additions & 0 deletions app/models/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def receive_webhook(params)
["not implemented", 404]
end

def em_start
# implement me in your subclass of Agent if continuous.
end

# Implement me in your subclass to decide if your Agent is working.
def working?
raise "Implement me in your subclass"
Expand Down Expand Up @@ -153,6 +157,10 @@ def can_create_events?
!cannot_create_events?
end

def continuous?
self.class.continuous?
end

def log(message, options = {})
puts "Agent##{id}: #{message}" unless Rails.env.test?
AgentLog.log_for_agent(self, message, options)
Expand Down Expand Up @@ -229,6 +237,14 @@ def cannot_receive_events?
!!@cannot_receive_events
end

def continuous!
@continuous = true
end

def continuous?
!!@continuous
end

# Find all Agents that have received Events since the last execution of this method. Update those Agents with
# their new `last_checked_event_id` and queue each of the Agents to be called with #receive using `async_receive`.
# This is called by bin/schedule.rb periodically.
Expand Down
65 changes: 65 additions & 0 deletions app/models/agents/rest_polling_agent.rb
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something like

event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?

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
18 changes: 17 additions & 1 deletion bin/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
end

require 'rufus/scheduler'
require 'eventmachine'
require 'set'

class HuginnScheduler
attr_accessor :mutex
Expand Down Expand Up @@ -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|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about removing Agents that no longer exist?

Copy link
Author

Choose a reason for hiding this comment

The 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!
12 changes: 6 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20131227000021) do
ActiveRecord::Schema.define(:version => 20140127164931) do

create_table "agent_logs", :force => true do |t|
t.integer "agent_id", :null => false
Expand Down Expand Up @@ -47,17 +47,17 @@
add_index "agents", ["user_id", "created_at"], :name => "index_agents_on_user_id_and_created_at"

create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
t.integer "attempts", :default => 0
t.text "handler"
t.integer "priority", :default => 0
t.integer "attempts", :default => 0
t.text "handler", :limit => 16777215
t.text "last_error"
t.datetime "run_at"
t.datetime "locked_at"
t.datetime "failed_at"
t.string "locked_by"
t.string "queue"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
Expand Down