Twitter監視するスクリプト作ったぞー
そんな時に神に降臨してほしい。
godのスタートアップをやってみた - mosson | Qiita
うまく動いてくれない
なんだかgodの設定例に従うとうまく動いてくれるのに、いざ自分のrubyスクリプトを神様に監視していただこうと思ってもうまくいかない。
require 'twitter'
#...
# 認証とか済ませてから
#...
stream = account.stream_api # Twitter::Streaming::Clientオブジェクト
stream.user do |object|
if obj == Twitter::Tweet
p "ツイートを取得しました"
end
end
FILE_PATH = "#{Dir.pwd}/streamtest.rb" # あなたの常駐化させたいアプリ
God.watch do |w|
w.name = "streamtest"
w.start = "ruby #{Dir.pwd}.rb"
w.keepalive
end
これで god -c ./streamtest.god -D
しても
I [2016-01-25 04:11:58] INFO: streamtest [trigger] process is not running (ProcessRunning)
I [2016-01-25 04:11:58] INFO: streamtest move 'up' to 'start'
I [2016-01-25 04:11:58] INFO: streamtest start: ruby /home/me/streamtest.rb
I [2016-01-25 04:11:58] INFO: streamtest moved 'up' to 'up'
I [2016-01-25 04:12:03] INFO: streamtest [trigger] process is not running (ProcessRunning)
I [2016-01-25 04:12:03] INFO: streamtest move 'up' to 'start'
I [2016-01-25 04:12:03] INFO: streamtest start: ruby /home/me/streamtest.rb
I [2016-01-25 04:12:03] INFO: streamtest moved 'up' to 'up'
I [2016-01-25 04:12:08] INFO: streamtest [trigger] process is not running (ProcessRunning)
I [2016-01-25 04:12:08] INFO: streamtest move 'up' to 'start'
I [2016-01-25 04:12:08] INFO: streamtest start: ruby /home/me/streamtest.rb
...
って感じで永遠に起動してくれない。
30分くらい触った結果、loop do ... end
以外のループ文(というかブロック文)とgodの相性が悪いのではないかと思った。
File.open do ... end
とか Twitter::Streaming::Client.user do ... end
があるとgodでは起動してくれない(のかすぐ終了してしまうかどちらか)っぽい。
改善策
- それ自体がループしてなさそうで
-
streamtest.rb
が死んだ時に終了してくれる
そんなプログラムを間に挟めばいいのではないだろうか
=> foreman
があるじゃないかー
foreman で アプリケーションを動かす。- 7kaji | Qiita
というわけで、
まずProcfile
をstreamtest.rb
と同じフォルダに。
web: ruby ./streamtest.rb
なににするか迷ったけどとりあえずwebに。
そしてstreamtest.god
も書き換え。
God.watch do |w|
w.name = "streamtest"
w.start = "foreman start -d #{Dir.pwd}"
w.keepalive
end
-d
オプションでProcfileのロケーションを渡します。
#{Dir.pwd}
としてるところはちゃんと絶対パスを指定したほうがいいです。本当は。これはまだ作りかけでファイルの位置がコロコロ変わるのでパスを取得するようにしています。
結果
動いた! ばんざい!
5秒毎に [trigger] process is not running (ProcessRunning)
と出ることがなくなりました。