aboutsummaryrefslogtreecommitdiffstats
path: root/client/worker.rb
blob: 1ed34142dcd5c54c40ed401984e7e28f0c3579f6 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
require "em-twitter"
require "yajl"
require "msgpack"
require "./settings"
require "./logger"

class Worker
  class DBProxyClient < EM::Connection
    def send_object(data)
      send_data(data.to_msgpack)
    end

    def initialize
      @clients = {}
      @pac = MessagePack::Unpacker.new
    end

    def format_text(status)
      chars = status[:text].to_s.split(//)

      entities = status[:entities].values.flatten.sort_by{|entity| entity[:indices].first}

      result = []
      last_index = entities.inject(0) do |last_index, entity|
        result << chars[last_index...entity[:indices].first]
        result << if entity[:url]
                    "<url:#{CGI.escape(entity[:expanded_url])}:#{CGI.escape(entity[:display_url])}>"
                  elsif entity[:text]
                    "<hashtag:#{CGI.escape(entity[:text])}>"
                  elsif entity[:screen_name]
                    "<mention:#{CGI.escape(entity[:screen_name])}>"
                  elsif entity[:cashtag]
                    "<cashtag:#{CGI.escape(entity[:cashtag])}>"
                  end
        entity[:indices].last
      end
      result << chars[last_index..-1]

      result.flatten.join
    end

    def format_source(status)
      if status[:source].index("<a")
        url = status[:source].scan(/href="(.+?)"/).flatten.first
        name = status[:source].scan(/>(.+?)</).flatten.first
        "<url:#{CGI.escape(url)}:#{CGI.escape(name)}>"
      else
        status[:source]
      end
    end

    def receive_account(msg)
      user_id = msg["user_id"]
      account_id = msg["id"]
      if @clients[account_id]
        @clients[account_id].connection.stop
        @clients.delete(account_id)
      end
      @clients[account_id] = client = EM::Twitter::Client.new({
        :host => "userstream.twitter.com",
        :path => "/1.1/user.json",
        :oauth => {
          :consumer_key => Settings.consumer[msg["consumer_version"].to_i].key,
          :consumer_secret => Settings.consumer[msg["consumer_version"].to_i].secret,
          :token => msg["oauth_token"],
          :token_secret => msg["oauth_token_secret"]},
        :method => "GET"})

      send_user = -> user do
        out = {:type => "user",
               :id => user[:id],
               :screen_name => user[:screen_name],
               :name => user[:name],
               :profile_image_url => user[:profile_image_url_https],
               :protected => user[:protected]}
        send_object(out)
        $logger.debug("User(##{account_id}/#{user_id}): #{user[:id]} = #{user[:screen_name]}")
      end

      send_tweet = -> status do
        send_user.call(status[:user])
        out = {:type => "tweet",
               :id => status[:id],
               :text => format_text(status),
               :source => format_source(status),
               :tweeted_at => status[:created_at],
               :user_id => status[:user][:id]}
        send_object(out)
        $logger.debug("Tweet(##{account_id}/#{user_id}): #{status[:id]}")
      end

      send_favorite = -> source, target_object do
        send_tweet.call(target_object)
        send_user.call(source)
        out = {:type => "favorite",
               :tweet_id => target_object[:id],
               :user_id => source[:id]}
        send_object(out)
        $logger.debug("Favorite(##{account_id}/#{user_id}): #{source[:id]} => #{target_object[:id]}")
      end

      send_unfavorite = -> source, target_object do
        out = {:type => "delete",
               :tweet_id => target_object[:id],
               :user_id => source[:id]}
        send_object(out)
        $logger.debug("Unfavorite(##{account_id}/#{user_id}): #{source[:id]} => #{target_object[:id]}")
      end

      send_retweet = -> status do
        send_tweet.call(status[:retweeted_status])
        send_user.call(status[:user])
        out = {:type => "retweet",
               :id => status[:id],
               :tweet_id => status[:retweeted_status][:id],
               :user_id => status[:user][:id]}
        send_object(out)
        $logger.debug("Retweet(##{account_id}/#{user_id}): #{status[:user][:id]} => #{status[:retweeted_status][:id]}")
      end

      send_delete = -> deleted_status_id, deleted_user_id do
        out = {:type => "delete",
               :id => deleted_status_id,
               :user_id => deleted_user_id}
        send_object(out)
        $logger.debug("Delete(##{account_id}/#{user_id}): #{deleted_user_id} => #{deleted_status_id}")
      end

      client.on_error do |message|
        $logger.warn("Unknown Error(##{account_id}/#{user_id}): #{message}")
      end

      client.on_unauthorized do
        # revoked?
        $logger.warn("Unauthorized(##{account_id}/#{user_id})")
        out = {:type => "unauthorized", :user_id => user_id, :id => account_id}
        send_object(out)
        client.connection.stop
        @clients.delete(account_id)
      end

      client.on_enhance_your_calm do
        # limit?
        $logger.warn("Enhance your calm(##{account_id}/#{user_id})")
      end

      client.on_no_data_received do
        # (?)
        $logger.warn("No data received(##{account_id}/#{user_id})")
        client.close_connection
      end

      client.each do |chunk|
        begin
          hash = Yajl::Parser.parse(chunk, :symbolize_keys => true)
        rescue Yajl::ParseError
          $logger.warn("Unexpected chunk(##{account_id}/#{user_id}): #{chunk}")
          next
        end

        if hash[:warning]
          $logger.info("Stall warning(##{account_id}/#{user_id}): #{hash[:warning]}")
        elsif hash[:delete] && hash[:delete][:status]
          deleted_status_id = hash[:delete][:status][:id]
          deleted_user_id = hash[:delete][:status][:user_id]
          send_delete.call(deleted_status_id, deleted_user_id)
        elsif hash[:limit]
          $logger.warn("UserStreams Limit Event(##{account_id}/#{user_id}): #{hash[:limit][:track]}")
        elsif hash[:event]
          case hash[:event]
          when "favorite"
            source = hash[:source]
            target_object = hash[:target_object]
            if !target_object[:user][:protected] ||
                target_object[:user][:id] == user_id
              send_favorite.call(source, target_object)
            end
          when "unfavorite"
            send_unfavorite.call(hash[:source], hash[:target_object])
          end
        elsif hash[:user]
          # tweet
          if hash[:retweeted_status]
            if hash[:retweeted_status][:user][:id] == user_id ||
                hash[:user][:id] == user_id
              send_retweet.call(hash)
            end
          elsif hash[:user][:id] == user_id
            send_tweet.call(hash)
          end
        elsif hash[:friends]
          # monyo
        else
          $logger.debug("??(##{account_id}/#{user_id})")
        end
      end

      client.on_reconnect do |timeout, retries|
        $logger.warn("Reconnected(##{account_id}/#{user_id}): #{retries}")
      end

      client.on_max_reconnects do |timeout, retries|
        $logger.warn("Max reconnects(##{account_id}/#{user_id}): #{retries}")
        client.connection.stop
        @clients.delete(account_id)
      end

      client.connect
      $logger.info("Connected(##{account_id}/#{user_id})")
    end

    def post_init
      out = {:type => "init",
             :secret_key => Settings.secret_key,
             :worker_number => Settings.worker_number}
      send_object(out)
    end

    def unbind
      $logger.info("Connection closed")
      EM.add_timer(10) do
        reconnect(Settings.db_proxy_host, Settings.db_proxy_port)
        post_init
      end
    end

    def receive_data(data)
      @pac.feed_each(data) do |msg|
        unless msg.is_a?(Hash) && msg["type"]
          $logger.warn("Unknown data: #{msg}")
          return
        end

        case msg["type"]
        when "ok"
          $logger.info("ok: #{msg["message"]}")
        when "error"
          $logger.info("error: #{msg["message"]}")
        when "fatal"
          $logger.info("fatal: #{msg["message"]}")
        when "bye"
          $logger.info("bye: #{msg["message"]}")
        when "account"
          begin
            receive_account(msg)
          rescue
            $logger.error($!)
            $logger.error($@)
          end
        else
          $logger.info("Unknown message type: #{msg}")
        end
      end
    end

    def stop_all
      @clients.map{|k, v| v.connection.stop}
      send_object({:type => "quit", :reason => "stop_all"})
    end
  end

  def initialize
    $logger = Aclog::Logger.new(:info)
  end

  def start
    $logger.info("Worker ##{Settings.worker_number} started")
    EM.run do
      connection = EM.connect(Settings.db_proxy_host, Settings.db_proxy_port, DBProxyClient)

      stop = Proc.new do
        connection.stop_all
        EM.stop
      end
      Signal.trap(:INT, &stop)
      Signal.trap(:TERM, &stop)
    end
  end
end