blob: 2686cdd6386cd814ccaf9696ddaea8c5f3e3f09a (
plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
module Collector
class EventQueue
def initialize
@dalli = Dalli::Client.new(Settings.cache.memcached, namespace: "aclog-collector:")
@queue_mutex = Mutex.new
@queue_user = Queue.new
@queue_tweet = Queue.new
@queue_favorite = Queue.new
@queue_retweet = Queue.new
@queue_unfavorite = Queue.new
@queue_delete = Queue.new
@queue_unauthorized = Queue.new
end
def flush
users = tweets = favorites = retweets = unfavorites = deletes = unauthorizeds = nil
@queue_mutex.synchronize do
users = @queue_user.size.times.map { @queue_user.deq }
tweets = @queue_tweet.size.times.map { @queue_tweet.deq }
favorites = @queue_favorite.size.times.map { @queue_favorite.deq }
retweets = @queue_retweet.size.times.map { @queue_retweet.deq }
unfavorites = @queue_unfavorite.size.times.map { @queue_unfavorite.deq }
deletes = @queue_delete.size.times.map { @queue_delete.deq }
unauthorizeds = @queue_unauthorized.size.times.map { @queue_unauthorized.deq }
end
User.create_or_update_bulk_from_json(users)
Tweet.create_bulk_from_json(tweets)
Favorite.create_bulk_from_json(favorites)
Retweet.create_bulk_from_json(retweets)
Favorite.delete_bulk_from_json(unfavorites)
if deletes.size > 0
Tweet.destroy_bulk_from_json(deletes)
Retweet.delete_bulk_from_json(deletes)
end
if Settings.notification.enabled?
tweet_ids = favorites.map {|f| f[:target_object][:id] }
if tweet_ids.size > 0
Tweet.where(id: tweet_ids).each do |tweet|
TweetResponseNotificationJob.perform_later(tweet)
end
end
end
unauthorizeds.each do |a|
account = Account.find(a[:id])
account.verify_token!
end
end
def push_user(msg)
cache(msg) do
@queue_user << msg[:data]
end
end
def push_tweet(msg)
cache(msg) do
@queue_tweet << msg[:data]
end
end
def push_retweet(msg)
cache(msg) do
@queue_retweet << msg[:data]
end
end
def push_favorite(msg)
cache(msg) do
@queue_favorite << msg[:data]
end
end
def push_unfavorite(msg)
cache(msg) do
@queue_unfavorite << msg[:data]
end
end
def push_delete(msg)
cache(msg) do
@queue_delete << msg[:data]
end
end
def push_unauthorized(unauthorized)
@queue_unauthorized << unauthorized[:data]
end
private
def cache(object)
if id = object[:identifier]
key, val = id.split("#", 2)
cur = @dalli.get(id)
if !cur || (val && (cur <=> val) == -1) # not found or new
@dalli.set(key, true || value)
yield
end
else
yield
end
end
end
end
|