Class: Akane::Receivers::Stream

Inherits:
AbstractReceiver show all
Defined in:
lib/akane/receivers/stream.rb

Defined Under Namespace

Classes: CustomSSLSocketFactory, TimeoutError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractReceiver

#on, #on_delete, #on_event, #on_message, #on_tweet

Constructor Details

#initializeStream

Returns a new instance of Stream.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/akane/receivers/stream.rb', line 10

def initialize(*)
  super
  @thread = nil

  if @config["method"]
    @stream_method = @config["method"].to_sym
  else
    @stream_method = :user
  end

  if @config["options"]
    @stream_options = Hash[@config["options"].map do |k,v|
      [k.to_sym, v]
    end]
  else
    @stream_options = {}
  end
end

Instance Attribute Details

#lastObject

Returns the value of attribute last.



48
49
50
# File 'lib/akane/receivers/stream.rb', line 48

def last
  @last
end

#threadObject (readonly)

Returns the value of attribute thread.



47
48
49
# File 'lib/akane/receivers/stream.rb', line 47

def thread
  @thread
end

Instance Method Details

#nameObject



29
30
31
32
33
# File 'lib/akane/receivers/stream.rb', line 29

def name
  # For backward compatibility, user stream returns only account name if
  # config.name not specified.
  @name ||= @config['name'] || @account[:name]
end

#running?Boolean

Returns:

  • (Boolean)


35
# File 'lib/akane/receivers/stream.rb', line 35

def running?() !!(@thread && @thread.alive?) end

#startObject



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
# File 'lib/akane/receivers/stream.rb', line 50

def start
  @logger.info "Stream : Starting"

  @last = Time.now
  @retry_count = 0
  @thread = Thread.new do
    begin
      stream.send(@stream_method, @stream_options) do |obj|
        @retry_count = 0

        case obj
        when Twitter::Tweet
          invoke(:tweet, obj)
        when Twitter::DirectMessage
          invoke(:message, obj)
        when Twitter::Streaming::DeletedTweet
          invoke(:delete, obj.user_id, obj.id)
        when Twitter::Streaming::Event
          invoke(:event,
                 'event' => obj.name, 'source' => obj.source,
                 'target' => obj.target, 'target_object' => obj.target_object)
        else
          next
        end
      end
    rescue Exception => e
      raise e if defined?(Twitter::Streaming::MockClient)
      @logger.error 'Error on stream'
      @logger.error e.inspect
      @logger.error e.backtrace

      @retry_count += 1

      # reconnecting https://dev.twitter.com/streaming/overview/connecting
      case e
      when Twitter::Error::EnhanceYourCalm # 420
        interval = 60 * (2 ** (@retry_count - 1))
      when Twitter::Error
        interval = [320, 5 * (2 ** (@retry_count - 1))].min
      else
        interval = [16, 0.25 * @retry_count].min
      end

      @logger.info "stream will reconnect after #{interval} sec (retry_count=#{@retry_count})"
      sleep interval
      @logger.info 'stream reconnecting'
      retry
    end
  end

  @watchdog = Thread.new do
    th = @thread
    begin
      loop do
        break unless @thread
        # @logger.debug "watchdog last #{@last} #{Time.now - @last}"
        if (Time.now - @last) > 90
          @last = Time.now
          @logger.error 'watchdog timeout'
          th.raise(TimeoutError)
        end
        sleep 1
      end
      @logger.info 'watchdog stop'
    rescue Exception => e
      @logger.error 'Error on watchdog'
      @logger.error e.inspect
      @logger.error e.backtrace

      sleep 5
      @logger.info 'watchdog restarting'
      retry
    end
  end

  @thread.abort_on_exception = true

  self
end

#stopObject



130
131
132
133
134
# File 'lib/akane/receivers/stream.rb', line 130

def stop
  @thread.tap(&:kill).join
  @thread = nil
  self
end

#streamObject



37
38
39
40
41
42
43
44
45
# File 'lib/akane/receivers/stream.rb', line 37

def stream
  @stream ||= Twitter::Streaming::Client.new(
    consumer_key: @consumer[:token],
    consumer_secret: @consumer[:secret],
    access_token: @account[:token],
    access_token_secret: @account[:secret],
    ssl_socket_class: CustomSSLSocketFactory.new(self),
  )
end