pending: resolving post action in queue
This commit is contained in:
parent
da9f33aa72
commit
43207aeb41
8 changed files with 141 additions and 17 deletions
5
Gemfile
5
Gemfile
|
|
@ -1,7 +1,10 @@
|
|||
source 'http://rubygems.org'
|
||||
|
||||
gem 'rails', '3.0.0.beta4'
|
||||
|
||||
gem 'mongrel'
|
||||
gem 'thin'
|
||||
gem 'em-http-request'
|
||||
gem 'addressable'
|
||||
gem "mongoid", :git => "http://github.com/durran/mongoid.git"
|
||||
gem "bson_ext", "1.0.1"
|
||||
gem "haml"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,10 @@
|
|||
class ApplicationController < ActionController::Base
|
||||
protect_from_forgery
|
||||
layout 'application'
|
||||
|
||||
def receive
|
||||
puts response.inspect
|
||||
puts "holy boner batman"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
class Post
|
||||
require 'lib/common'
|
||||
|
||||
# XML accessors must always preceed mongo field tags
|
||||
|
||||
|
|
@ -6,6 +7,8 @@ class Post
|
|||
include Mongoid::Timestamps
|
||||
include ROXML
|
||||
|
||||
include Diaspora::Hookey
|
||||
|
||||
xml_accessor :owner
|
||||
xml_accessor :snippet
|
||||
xml_accessor :source
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class StatusMessage < Post
|
|||
end
|
||||
|
||||
def self.retrieve_from_friend(friend)
|
||||
StatusMessages.from_xml Curl.curl(friend.url+"status_messages.xml")
|
||||
StatusMessages.from_xml Curl.get(friend.url+"status_messages.xml")
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ Diaspora::Application.routes.draw do |map|
|
|||
match 'logout', :to => 'devise/sessions#destroy', :as => "destroy_user_session"
|
||||
match 'signup', :to => 'devise/registrations#new', :as => "new_user_registration"
|
||||
|
||||
match 'receive', :to => 'application#receive'
|
||||
|
||||
resources :users
|
||||
resources :status_messages
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
module CommonField
|
||||
|
||||
module Diaspora
|
||||
module CommonFields
|
||||
def self.included(klass)
|
||||
klass.class_eval do
|
||||
include Mongoid::Document
|
||||
|
|
@ -16,3 +16,46 @@ module CommonField
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
klass.class_eval do
|
||||
require 'lib/message_handler'
|
||||
before_save :notify_friends
|
||||
|
||||
def notify_friends
|
||||
m = MessageHandler.new
|
||||
|
||||
xml = prep_webhook
|
||||
#friends_with_permissions.each{ |friend| puts friend; Curl.post( "\"" + xml + "\" " + friend) }
|
||||
m.add_post_request( friends_with_permissions, xml )
|
||||
m.process
|
||||
end
|
||||
|
||||
def prep_webhook
|
||||
self.to_xml.to_s.chomp
|
||||
end
|
||||
|
||||
def friends_with_permissions
|
||||
#Friend.only(:url).map{|x| x = x.url + "/receive/"}
|
||||
googles = []
|
||||
5.times{ googles <<"http://google.com/"} #"http://localhost:4567/receive/"} #"http://google.com/"}
|
||||
googles
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
64
lib/message_handler.rb
Normal file
64
lib/message_handler.rb
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
require 'em-http'
|
||||
require 'eventmachine'
|
||||
require 'addressable/uri'
|
||||
|
||||
class MessageHandler
|
||||
|
||||
|
||||
NUM_TRIES = 3
|
||||
TIMEOUT = 5 #seconds
|
||||
|
||||
def initialize
|
||||
@queue = EM::Queue.new
|
||||
end
|
||||
|
||||
def add_get_request(destinations)
|
||||
destinations.each{ |dest| @queue.push(Message.new(:get, dest))}
|
||||
end
|
||||
|
||||
|
||||
def add_post_request(destinations, body)
|
||||
destinations.each{|dest| @queue.push(Message.new(:post, dest, body))}
|
||||
end
|
||||
|
||||
def process
|
||||
@queue.pop{ |query|
|
||||
case query.type
|
||||
when :post
|
||||
http = EventMachine::HttpRequest.new(query.destination).post :timeout => TIMEOUT, :body =>query.body
|
||||
http.callback { process}
|
||||
when :get
|
||||
http = EventMachine::HttpRequest.new(query.destination).get :timeout => TIMEOUT
|
||||
http.callback {send_to_seed(query, http.response); process}
|
||||
else
|
||||
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
|
||||
end
|
||||
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
class Curl
|
||||
def self.curl(s)
|
||||
`curl #{s}`
|
||||
def self.post(s)
|
||||
`curl -X POST -d #{s}`;;
|
||||
end
|
||||
|
||||
def self.get(s)
|
||||
`curl -X GET #{s}`
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue