herokuでTweetStream::ReconnectErrorが発生する
tweetstream を利用して特定ユーザーのtweetを監視するアプリをheroku上にデプロイしましたが、TweetStream::ReconnectError というエラーが発生しうまく動作していないようです。
ローカルで foreman を利用し動作を確認したところ問題ありませんでした。heroku上で動作させるためには何か必要なのでしょうか?
ソースコードとログをGithub上に公開しているので下記を参照ください。
ソースコード:
https://github.com/shts/nogi_tweet_observer
require 'net/http'
require 'uri'
require 'tweetstream'
require 'parse-ruby-client'
require 'uri'
Parse.init :application_id => ENV['PARSE_APP_ID'],
:api_key => ENV['PARSE_API_KEY']
TweetStream.configure do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.oauth_token = ENV['TWITTER_OAUTH_TOKEN']
config.oauth_token_secret = ENV['TWITTER_OAUTH_TOKEN_SECRET']
config.auth_method = :oauth
end
def push_notification(url) # must String
data = { :alert => "Push from ruby sample!" + url.to_s, :url => url.to_s }
push = Parse::Push.new(data)
push.where = { :deviceType => "android" }
p push.save
end
def replace_uri(s)
str = s.dup
uri_reg = URI.regexp(%w[http https])
str.gsub!(uri_reg) {"#{$&}"}
"#{$&}"
end
NOGIZAKA_BLOG = "http://blog.nogizaka46.com"
def expand_url(url)
uri = url.kind_of?(URI) ? url : URI.parse(url)
Net::HTTP.start(uri.host, uri.port) { |io|
r = io.head(uri.path)
r['Location'] || uri.to_s
}
end
EM.run do
client = TweetStream::Client.new
# 1084091587 -> 練習用
# 317684165 -> 本番
client.follow(1084091587) do |status|
puts "#{status.user.screen_name}: #{status.text}"
url = replace_uri(status.text)
if (!url.to_s.include?("http://t.co/") && !url.to_s.include?("http://bit.ly/"))
puts "url may be not nogizaka blog domain : " + url.to_s
else
url = expand_url(url)
if (url.include?(NOGIZAKA_BLOG))
push_notification(url)
else
url = expand_url(url)
if (url.include?(NOGIZAKA_BLOG))
push_notification(url)
else
puts "url may be not nogizaka blog domain"
end
end
end
end
client.on_error do |message|
puts "error: #{message}\n"
end
client.on_reconnect do |timeout, retries|
puts "reconnecting in: #{timeout} seconds\n"
end
end
エラーログの要点部分:
https://github.com/shts/nogi_tweet_observer/issues/1
2015-02-21T09:50:47.443193+00:00 heroku[observer.1]: State changed from crashed to starting
2015-02-21T09:50:47.425787+00:00 heroku[observer.1]: Process exited with status 1
2015-02-21T09:50:49.806923+00:00 heroku[observer.1]: Starting process with command `bundle exec ruby observer.rb`
2015-02-21T09:50:50.414194+00:00 heroku[observer.1]: State changed from starting to up
2015-02-21T10:01:22.966632+00:00 app[observer.1]: /app/vendor/bundle/ruby/2.0.0/gems/tweetstream-2.6.1/lib/tweetstream/client.rb:449:in `block in connect': Failed to reconnect after 6 tries. (TweetStream::ReconnectError)
2015-02-21T10:01:22.966647+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/em-twitter-0.3.5/lib/em-twitter/connection.rb:297:in `call'
2015-02-21T10:01:22.966648+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/em-twitter-0.3.5/lib/em-twitter/connection.rb:297:in `invoke_callback'
2015-02-21T10:01:22.966650+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/em-twitter-0.3.5/lib/em-twitter/connection.rb:269:in `rescue in schedule_reconnect'
2015-02-21T10:01:22.966657+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/eventmachine-1.0.7/lib/eventmachine.rb:1457:in `event_callback'
2015-02-21T10:01:22.966652+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/em-twitter-0.3.5/lib/em-twitter/connection.rb:264:in `schedule_reconnect'
2015-02-21T10:01:22.966656+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/em-twitter-0.3.5/lib/em-twitter/connection.rb:92:in `unbind'
2015-02-21T10:01:22.966660+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run'
2015-02-21T10:01:22.966658+00:00 app[observer.1]: from /app/vendor/bundle/ruby/2.0.0/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run_machine'
2015-02-21T10:01:22.966661+00:00 app[observer.1]: from observer.rb:42:in `<main>'
また、rubyについては始めたばかりなのでソースコードなどにおかしなところがあれば、知見をお持ちの方に指摘いただきたいです。
よろしくお願いいたします。