This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Make job backend pluggable

  • Loading branch information...
titanous committed Sep 17, 2012
1 parent 0aa521e commit 1c90f94a424f1b7ff5fe390b01d61315fabf61ef
View
@@ -7,3 +7,4 @@ gem 'rack-test', :git => 'https://github.com/brynary/rack-test.git'
gem 'tent-client', :git => 'git@github.com:tent/tent-client-ruby.git', :branch => 'master'
gem 'rb-fsevent', '~> 0.9.1'
gem 'do_jdbc', :platform => :jruby
+gem 'girl_friday', '~> 0.10'
View
@@ -8,12 +8,14 @@ module TentD
autoload :TentVersion, 'tentd/tent_version'
autoload :TentType, 'tentd/tent_type'
autoload :RackRequest, 'tentd/rack_request'
- autoload :Notifications, 'tentd/notifications'
def self.new(options={})
if options[:database] || ENV['DATABASE_URL']
DataMapper.setup(:default, options[:database] || ENV['DATABASE_URL'])
end
+
+ require "tentd/notifications/#{options[:job_backend] || 'girl_friday'}"
+
@faraday_adapter = options[:faraday_adapter]
API.new
end
@@ -148,7 +148,7 @@ def action(env)
:action => env.notify_action
}
)
- Notifications::TRIGGER_QUEUE << { :type => post.type, :post_id => post.id }
+ Notifications.trigger(:type => post.type, :post_id => post.id)
env
end
end
@@ -164,7 +164,7 @@ def action(env)
:action => env.notify_action
}
)
- Notifications::TRIGGER_QUEUE << { :type => post.type, :post_id => post.id }
+ Notifications.trigger(:type => post.type, :post_id => post.id)
env
end
end
View
@@ -103,7 +103,7 @@ def action(env)
:action => env.notify_action
}
)
- Notifications::TRIGGER_QUEUE << { :type => post.type, :post_id => post.id }
+ Notifications.trigger(:type => post.type, :post_id => post.id)
env
end
end
View
@@ -247,7 +247,7 @@ def action(env)
else
return env unless (post = env.response) && post.kind_of?(Model::Post)
end
- Notifications::TRIGGER_QUEUE << { :type => post.type, :post_id => post.id }
+ Notifications.trigger(:type => post.type, :post_id => post.id)
env
end
end
View
@@ -60,7 +60,7 @@ def action(env)
:types => env.notify_types,
}
)
- Notifications::TRIGGER_QUEUE << { :type => post.type, :post_id => post.id }
+ Notifications.trigger(:type => post.type, :post_id => post.id)
env
end
end
@@ -21,7 +21,7 @@ def subject
def self.notify_all(type, post_id)
all(:type_base => [type.base, 'all'], :fields => [:id, :app_authorization_id, :follower_id]).each do |subscription|
next unless Post.first(:id => post_id, :fields => [:id, :original, :public]).can_notify?(subscription.subject)
- Notifications::NOTIFY_QUEUE << { :subscription_id => subscription.id, :post_id => post_id }
+ Notifications.notify(:subscription_id => subscription.id, :post_id => post_id)
end
end
View
@@ -81,7 +81,7 @@ def self.create(data)
follower = Follower.first(:entity => mention.entity)
next if follower && NotificationSubscription.first(:follower => follower, :type_base => post.type.base)
- Notifications::NOTIFY_ENTITY_QUEUE << { :entity => mention.entity, :post_id => post.id }
+ Notifications.notify_entity(:entity => mention.entity, :post_id => post.id)
end
end
View
@@ -1,17 +1,18 @@
require 'girl_friday'
+require 'tentd/notifications'
module TentD
- module Notifications
- TRIGGER_QUEUE = GirlFriday::WorkQueue.new(:notification_trigger) do |msg|
- Model::NotificationSubscription.notify_all(msg[:type], msg[:post_id])
+ class Notifications
+ def self.trigger(msg)
+ queue_job(:trigger, msg)
end
- NOTIFY_QUEUE = GirlFriday::WorkQueue.new(:notification) do |msg|
- Model::NotificationSubscription.first(:id => msg[:subscription_id]).notify_about(msg[:post_id])
+ def self.notify(msg)
+ queue_job(:notify, msg)
end
- NOTIFY_ENTITY_QUEUE = GirlFriday::WorkQueue.new(:notification) do |msg|
- Model::NotificationSubscription.notify_entity(msg[:entity], msg[:post_id])
+ def self.notify_entity(msg)
+ queue_job(:notify_entity, msg)
end
end
end
@@ -0,0 +1,22 @@
+require 'tentd/notifications'
+require 'girl_friday'
+
+module TentD
+ class Notifications
+ def self.queue_job(job, msg)
+ const_get(job.to_s.upcase+'_QUEUE').push(msg)
+ end
+
+ TRIGGER_QUEUE = GirlFriday::WorkQueue.new(:notification_trigger) do |msg|
+ Model::NotificationSubscription.notify_all(msg[:type], msg[:post_id])
+ end
+
+ NOTIFY_QUEUE = GirlFriday::WorkQueue.new(:notification) do |msg|
+ Model::NotificationSubscription.first(:id => msg[:subscription_id]).notify_about(msg[:post_id])
+ end
+
+ NOTIFY_ENTITY_QUEUE = GirlFriday::WorkQueue.new(:notification) do |msg|
+ Model::NotificationSubscription.notify_entity(msg[:entity], msg[:post_id])
+ end
+ end
+end
@@ -0,0 +1,35 @@
+require 'tentd/notifications'
+require 'sidekiq'
+
+class TentD
+ class Notifications
+ def self.queue_job(job, msg)
+ const_get(job.to_s.split('_').map(&:capitalize).push('Worker').join).perform_async(msg)
+ end
+
+ class TriggerWorker
+ include Sidekiq::Worker
+
+ def perform(msg)
+ Model::NotificationSubscription.notify_all(msg[:type], msg[:post_id])
+ end
+ end
+
+ class NotifyWorker
+ include Sidekiq::Worker
+
+ def perform(msg)
+ Model::NotificationSubscription.first(:id => msg[:subscription_id]).notify_about(msg[:post_id])
+ end
+ end
+
+
+ class NotifyEntityWorker
+ include Sidekiq::Worker
+
+ def perform(msg)
+ Model::NotificationSubscription.notify_entity(msg[:entity], msg[:post_id])
+ end
+ end
+ end
+end
@@ -11,7 +11,7 @@ def initialize
@items = []
end
- def <<(item)
+ def push(item)
(@items ||= []) << item
end
end
View
@@ -8,7 +8,7 @@
require 'fabrication'
require 'tentd/core_ext/hash/slice'
require 'girl_friday'
-require 'tentd/notifications'
+require 'tentd/notifications/girl_friday'
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
View
@@ -24,7 +24,6 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'dm-constraints', '~> 1.2.0'
gem.add_runtime_dependency 'dm-postgres-adapter', '~> 1.2.0'
gem.add_runtime_dependency 'tent-client'
- gem.add_runtime_dependency 'girl_friday', '~> 0.10'
gem.add_development_dependency 'rack-test', '~> 0.6.1'
gem.add_development_dependency 'rspec', '~> 2.11'

0 comments on commit 1c90f94

Please sign in to comment.