aboutsummaryrefslogtreecommitdiffstats
path: root/lib/collector/event_queue.rb
blob: 3851d82bea09922574b8eae1d7d604150b097e35 (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
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

      tweet_ids = favorites.map {|f| f[:target_object][:id] }
      if tweet_ids.size > 0
        Tweet.where(id: tweet_ids).each do |tweet|
          Notification.try_notify_favorites(tweet)
        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]
        unless @dalli.get(id)
          @dalli.set(id, true)
          yield
        end
      else
        yield
      end
    end
  end
end