-
Notifications
You must be signed in to change notification settings - Fork 1
/
notification_server.rb
65 lines (57 loc) · 1.63 KB
/
notification_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'bunny'
require 'thread'
require 'json'
require 'sinatra/activerecord'
require_relative './models/notification.rb'
require 'eventmachine'
class NotificationServer
def initialize ch
@ch=ch
end
def start(queue_name)
@q = @ch.queue(queue_name)
@x = @ch.default_exchange
@q.subscribe(:block => true) do |delivery_info, properties, payload|
print "\ndelivery_info:#{delivery_info}\nproperties:#{properties}\npayload:#{payload}\n"
n = JSON.parse payload
r = self.class.createNotification(n)
@x.publish(r.to_json, :routing_key => properties.reply_to, :correlation_id => properties.correlation_id)
end
end
def self.createNotification n
print n
puts
unless Notification.where(user_id: n["user_id"],owner_id: n["owner_id"],tweet_id: n["tweet_id"]).size.zero?
return
else
newNoti=Notification.new
if n["tweet_id"].nil?
newNoti.notitype='follow'
newNoti.content="#{n["username"]} followed you"
else
newNoti.notitype='like'
newNoti.tweet_id=n["tweet_id"]
newNoti.content="#{n["username"]} liked your tweet"
end
newNoti.user_id=n["user_id"]
newNoti.owner_id=n["owner_id"]
newNoti.readmark=false
newNoti.create_time=Time.now
newNoti.save
end
end
end
EventMachine.run do
conn = Bunny.new(ENV["RABBITMQ_BIGWIG_RX_URL"],automatically_recover: false)
conn.start
puts "bunny is running"
ch = conn.create_channel
begin
server=NotificationServer.new(ch)
puts " [x] Awaiting RPC requests"
server.start("rpc_queue")
rescue Interrupt => _
ch.close
conn.close
end
end