tried to git rid of error in em-http-request
This commit is contained in:
parent
43207aeb41
commit
67535c1113
4 changed files with 59 additions and 71 deletions
2
Gemfile
2
Gemfile
|
|
@ -3,7 +3,7 @@ source 'http://rubygems.org'
|
||||||
gem 'rails', '3.0.0.beta4'
|
gem 'rails', '3.0.0.beta4'
|
||||||
gem 'mongrel'
|
gem 'mongrel'
|
||||||
gem 'thin'
|
gem 'thin'
|
||||||
gem 'em-http-request'
|
gem 'em-http-request', :git => 'git@github.com:maxwell/em-http-request.git'
|
||||||
gem 'addressable'
|
gem 'addressable'
|
||||||
gem "mongoid", :git => "http://github.com/durran/mongoid.git"
|
gem "mongoid", :git => "http://github.com/durran/mongoid.git"
|
||||||
gem "bson_ext", "1.0.1"
|
gem "bson_ext", "1.0.1"
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
class Post
|
class Post
|
||||||
require 'lib/common'
|
require 'lib/common'
|
||||||
|
require 'lib/message_handler'
|
||||||
|
|
||||||
|
|
||||||
# XML accessors must always preceed mongo field tags
|
# XML accessors must always preceed mongo field tags
|
||||||
|
|
||||||
include Mongoid::Document
|
include Mongoid::Document
|
||||||
include Mongoid::Timestamps
|
include Mongoid::Timestamps
|
||||||
include ROXML
|
include ROXML
|
||||||
|
@@queue = MessageHandler.new
|
||||||
include Diaspora::Hookey
|
include Diaspora::Hookey
|
||||||
|
|
||||||
xml_accessor :owner
|
xml_accessor :owner
|
||||||
|
|
@ -21,7 +23,6 @@ class Post
|
||||||
#after_update :notify_friends
|
#after_update :notify_friends
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@models = ["StatusMessage", "Bookmark", "Blog"]
|
@@models = ["StatusMessage", "Bookmark", "Blog"]
|
||||||
|
|
||||||
def self.recent_ordered_posts
|
def self.recent_ordered_posts
|
||||||
|
|
|
||||||
|
|
@ -19,30 +19,19 @@ module Diaspora
|
||||||
|
|
||||||
module Hookey
|
module Hookey
|
||||||
|
|
||||||
class Curl
|
|
||||||
def self.post(s)
|
|
||||||
`curl -X POST -d #{s}`;;
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get(s)
|
|
||||||
`curl -X GET #{s}`
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def self.included(klass)
|
def self.included(klass)
|
||||||
|
|
||||||
klass.class_eval do
|
klass.class_eval do
|
||||||
require 'lib/message_handler'
|
#include EventQueue::MessageHandler
|
||||||
before_save :notify_friends
|
before_save :notify_friends
|
||||||
|
|
||||||
def notify_friends
|
def notify_friends
|
||||||
m = MessageHandler.new
|
|
||||||
|
|
||||||
|
@@queue = MessageHandler.new
|
||||||
xml = prep_webhook
|
xml = prep_webhook
|
||||||
#friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) }
|
#friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) }
|
||||||
m.add_post_request( friends_with_permissions, xml )
|
@@queue.add_post_request( friends_with_permissions, xml )
|
||||||
m.process
|
@@queue.process
|
||||||
end
|
end
|
||||||
|
|
||||||
def prep_webhook
|
def prep_webhook
|
||||||
|
|
|
||||||
|
|
@ -4,61 +4,59 @@
|
||||||
|
|
||||||
class MessageHandler
|
class MessageHandler
|
||||||
|
|
||||||
|
NUM_TRIES = 3
|
||||||
|
TIMEOUT = 5 #seconds
|
||||||
|
|
||||||
NUM_TRIES = 3
|
def initialize
|
||||||
TIMEOUT = 5 #seconds
|
@queue = EM::Queue.new
|
||||||
|
end
|
||||||
|
|
||||||
def initialize
|
def add_get_request(destinations)
|
||||||
@queue = EM::Queue.new
|
destinations.each{ |dest| @queue.push(Message.new(:get, dest))}
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_get_request(destinations)
|
|
||||||
destinations.each{ |dest| @queue.push(Message.new(:get, dest))}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def add_post_request(destinations, body)
|
def add_post_request(destinations, body)
|
||||||
destinations.each{|dest| @queue.push(Message.new(:post, dest, body))}
|
destinations.each{|dest| @queue.push(Message.new(:post, dest, body))}
|
||||||
end
|
end
|
||||||
|
|
||||||
def process
|
def process
|
||||||
@queue.pop{ |query|
|
@queue.pop{ |query|
|
||||||
case query.type
|
case query.type
|
||||||
when :post
|
when :post
|
||||||
http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>query.body
|
http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body => query.body
|
||||||
http.callback { process}
|
http.callback { process}
|
||||||
when :get
|
when :get
|
||||||
http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT
|
http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT
|
||||||
http.callback {send_to_seed(query, http.response); process}
|
http.callback {send_to_seed(query, http.response); process}
|
||||||
else
|
else
|
||||||
raise "message is not a type I know!"
|
raise "message is not a type I know!"
|
||||||
|
end
|
||||||
|
|
||||||
|
http.errback {
|
||||||
|
query.try_count +=1
|
||||||
|
@queue.push query unless query.try_count >= NUM_TRIES
|
||||||
|
process
|
||||||
|
}
|
||||||
|
} unless @queue.size == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def send_to_seed(message, http_response)
|
||||||
|
#DO SOMETHING!
|
||||||
|
end
|
||||||
|
|
||||||
|
def size
|
||||||
|
@queue.size
|
||||||
|
end
|
||||||
|
|
||||||
|
class Message
|
||||||
|
attr_accessor :type, :destination, :body, :try_count
|
||||||
|
def initialize(type, dest, body= nil)
|
||||||
|
@type = type
|
||||||
|
@destination = dest
|
||||||
|
@body = body
|
||||||
|
@try_count = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
http.errback {
|
|
||||||
query.try_count +=1
|
|
||||||
@queue.push query unless query.try_count >= NUM_TRIES
|
|
||||||
process
|
|
||||||
}
|
|
||||||
} unless @queue.size == 0
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def send_to_seed(message, http_response)
|
|
||||||
#DO SOMETHING!
|
|
||||||
end
|
|
||||||
|
|
||||||
def size
|
|
||||||
@queue.size
|
|
||||||
end
|
|
||||||
|
|
||||||
class Message
|
|
||||||
attr_accessor :type, :destination, :body, :try_count
|
|
||||||
def initialize(type, dest, body= nil)
|
|
||||||
@type = type
|
|
||||||
@destination = dest
|
|
||||||
@body = body
|
|
||||||
@try_count = 0
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue