Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Added: async service calling with EM.defer refs #178
Browse files Browse the repository at this point in the history
Conflicts:
	plugins/as_android_notifier/lib/as_android_notifier.rb
  • Loading branch information
mallowlabs committed Apr 3, 2014
1 parent 0dbeffb commit a84cfed
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 31 deletions.
14 changes: 8 additions & 6 deletions app/helpers/chat_helper.rb
Expand Up @@ -37,12 +37,14 @@ def publish_message(event, message, room)
{ :content => to_json(message, room) }
end

begin
AsakusaSatellite::MessagePusher.trigger("as-#{room.id}",
"message_#{event}",
data.to_json)
rescue => e
Rails.logger.warn "fail to send message: #{e.inspect}"
AsakusaSatellite::AsyncRunner.run do
begin
AsakusaSatellite::MessagePusher.trigger("as-#{room.id}",
"message_#{event}",
data.to_json)
rescue => e
Rails.logger.warn "fail to send message: #{e.inspect}"
end
end

if event == :create then
Expand Down
1 change: 1 addition & 0 deletions lib/asakusa_satellite.rb
Expand Up @@ -3,6 +3,7 @@
require 'asakusa_satellite/filter/filter_config'
require 'asakusa_satellite/filter/loader'
require 'asakusa_satellite/hook'
require 'asakusa_satellite/async_runner'
require 'asakusa_satellite/config'
require 'asakusa_satellite/message_pusher'
require 'asakusa_satellite/omniauth/adapter'
Expand Down
14 changes: 14 additions & 0 deletions lib/asakusa_satellite/async_runner.rb
@@ -0,0 +1,14 @@
require 'eventmachine'

module AsakusaSatellite
class AsyncRunner
def self.run(&block)
if EM.reactor_running?
EM.defer(block)
else
block.call
end
end
end
end

24 changes: 13 additions & 11 deletions plugins/as_android_notifier/lib/as_android_notifier.rb
Expand Up @@ -19,19 +19,21 @@ def after_create_message(context)
device.device_type == 'android'
}

android.to_a.map{|device|
{ :registration_id => device.name,
:data => {
:message => text,
:id => room.id
AsakusaSatellite::AsyncRunner.run do
android.to_a.map{|device|
{ :registration_id => device.name,
:data => {
:message => text,
:id => room.id
}
}
}.tap{|xs|
C2DM.send_notifications(ENV["ANDROID_MAIL_ADDRESS"],
ENV["ANDROID_PASSWORD"],
xs,
ENV["ANDROID_APPLICATION_NAME"])
}
}.tap{|xs|
C2DM.send_notifications(ENV["ANDROID_MAIL_ADDRESS"],
ENV["ANDROID_PASSWORD"],
xs,
ENV["ANDROID_APPLICATION_NAME"])
}
end

end

Expand Down
10 changes: 6 additions & 4 deletions plugins/as_chrome_notifier/lib/as_chrome_notifier.rb
Expand Up @@ -6,10 +6,12 @@ def after_create_message(context)
message = context[:message]
room = context[:room]

room.owner_and_members.each do |member|
member.devices.each do |device|
if device.device_type == "chrome" and device.name
Chrome.send(device.name, message.id)
AsakusaSatellite::AsyncRunner.run do
room.owner_and_members.each do |member|
member.devices.each do |device|
if device.device_type == "chrome" and device.name
Chrome.send(device.name, message.id)
end
end
end
end
Expand Down
22 changes: 12 additions & 10 deletions plugins/as_iphone_notifier/lib/as_iphone_notifier.rb
Expand Up @@ -31,16 +31,18 @@ def after_create_message(context)
device.device_type.nil? or device.device_type == 'iphone'
end

iphones.to_a.map{|iphone|
APNS::Notification.new(iphone.name,
:alert => text,
:sound => 'default',
:other => {
:id => room.id
})
}.tap{|xs|
APNS.send_notifications xs
}
AsakusaSatellite::AsyncRunner.run do
iphones.to_a.map{|iphone|
APNS::Notification.new(iphone.name,
:alert => text,
:sound => 'default',
:other => {
:id => room.id
})
}.tap{|xs|
APNS.send_notifications xs
}
end
rescue => e
Rails.logger.error e
end
Expand Down

0 comments on commit a84cfed

Please sign in to comment.