Skip to content

Instantly share code, notes, and snippets.

@inokappa
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inokappa/09da71518c6771e40e5c to your computer and use it in GitHub Desktop.
Save inokappa/09da71518c6771e40e5c to your computer and use it in GitHub Desktop.
涙の自腹課金検証シリーズ第二弾:Focuslight を試す + eye でプロセス監視してみる

涙の自腹課金検証シリーズ第二弾:Focuslight を試す + eye でプロセス監視してみる

はじめに

  • Growthforecast を調べていたら Focuslight に辿り着いたので試す
  • Growthforecast とのデータ互換もあるようだ(すばらしひ)
  • バックエンドのストレージとして MySQL の利用も可能だが今回は SQLite で試してみる
  • また、TODODaemonize support とありデーモン化は出来ていないようなので別の手段(eye)を使ってデーモン化を試みてみる

試した環境

OS

  • Amazon Linux(Amazon Linux AMI release 2014.03)
  • ほぼ素

Ruby

  • ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-linux]

準備

こちらに従い関連パッケージ及び focuslight 自身のインストール。途中、関連パッケージから要求されるパッケージがあったりで以下のようになった。

関連パッケージのインストール

sudo yum -y install gcc make gcc-g++
sudo yum -y install sqlite-devel
sudo yum -y install mysql-libs
sudo yum -y install mysql-devel
sudo yum -y install ruby20-devel.x86_64
sudo yum -y install rrdtool.x86_64
sudo yum -y install rrdtool-devel.x86_64
sudo yum -y install ImageMagick ImageMagick-devel
sudo yum -y install git
sudo yum -y install rubygems.noarch 
sudo gem install fileutils --no-ri --no-rdoc -V
sudo gem install io-console --no-ri --no-rdoc -V
sudo gem install bundler --no-ri --no-rdoc -V

インストールする必要が無いものもありそう。必要なパッケージについてはこちら

インストール

sudo gem install focuslight --no-ri --no-rdoc -V

error

focuslight help

を実行すると以下のようなエラーになった。

/usr/local/share/ruby/gems/2.0/gems/focuslight-0.1.3/lib/focuslight/cli.rb:39:in `<class:CLI>': undefined method `default_command' for Focuslight::CLI:Class (NoMethodError)
        from /usr/local/share/ruby/gems/2.0/gems/focuslight-0.1.3/lib/focuslight/cli.rb:7:in `<top (required)>'
        from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /usr/local/share/ruby/gems/2.0/gems/focuslight-0.1.3/bin/focuslight:5:in `<top (required)>'
        from /usr/local/bin/focuslight:23:in `load'
        from /usr/local/bin/focuslight:23:in `<main>'

結局、以下のように gem のインストールディレクトリに移動して bundle install すると...

$ sudo su -
# cd /usr/local/share/ruby/gems/2.0/gems/focuslight-0.1.3/
# bundle install
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle
as root will break this application for all non-root users on this machine.
fatal: Not a git repository (or any of the parent directories): .git
Fetching gem metadata from https://rubygems.org/........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Installing rake 10.3.2
Using i18n 0.6.9
Installing json 1.8.1
Using minitest 5.3.4
Using thread_safe 0.3.3
Using tzinfo 1.1.0
Using activesupport 4.1.1
Installing addressable 2.3.6
Using backports 3.6.0
Using bundler 1.6.2
Installing coderay 1.1.0
Installing diff-lcs 1.2.5
Using dotenv-deployment 0.0.2
Installing dotenv 0.11.1
Using erubis 2.7.0
Installing multipart-post 2.0.0
Installing faraday 0.9.0
Using ffi 1.9.3
Installing thor 0.19.1
Installing foreman 0.63.0
Using librrd 1.0.4
Using mysql2 0.3.16
Using rrd-ffi 0.2.14
Using sequel 4.10.0
Using rack 1.5.2
Using rack-protection 1.5.3
Using tilt 1.4.1
Using sinatra 1.4.5
Using multi_json 1.10.0
Using rack-test 0.6.2
Using sinatra-contrib 1.4.2
Using sqlite3 1.3.9
Using kgio 2.9.2
Using raindrops 0.13.0
Using unicorn 4.8.3
Using unicorn-worker-killer 0.4.2
Using focuslight 0.1.3 from source at .
Installing hub 1.12.0
Installing method_source 0.8.2
Installing sawyer 0.5.4
Installing octokit 3.1.0
Installing octorelease 0.0.6
Installing slop 3.5.0
Installing pry 0.9.12.6
Installing pry-nav 0.2.3
Installing rspec-core 2.14.8
Installing rspec-expectations 2.14.5
Installing rspec-mocks 2.14.6
Installing rspec 2.14.1
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

以下の通り focuslight help の結果が出力された。

Commands:
  focuslight help [COMMAND]  # Describe available commands or one specific command
  focuslight init            # Creates database schema
  focuslight longer          # Startup focuslight longer worker
  focuslight new             # Creates focuslight resource directory
  focuslight shorter         # Startup focuslight shorter worker
  focuslight start           # Sartup focuslight

良かった、良かった。

初期設定、起動

初期設定

focuslight new

するとカレントディレクトリに focuslight というディレクトリが作成される。

drwxrwxr-x 3 ec2-user ec2-user 4096  5月 16 21:22 focuslight

focuslight ディレクトリに移動して focuslight init を実行する。

cd focuslight
focuslight init

起動

focuslight start

以下のように表示された。

次にブラウザからアクセスしてみる。ホストの 5125 ポートで Listen しているのでアクセスしてみる。

おお、お見事。後は煮るなり、焼くなり...。

煮るなり焼くなり

Growthforecast っぽく以下のように以下のように API を叩いてみる。

* * * * * curl -F number=`echo $RANDOM` http://localhost:5125/api/hoge/huga/register

暫くすると...

ファンタスティック!


eye で Focuslight のプロセス監視をしてみよう

ここから本題。

Why ?

せっかく使うのだから Focuslight のプロセスを監視して落ちていたら勝手に上げてくれる仕組みを設定しておきたいということで monitsupervisord も良さそうだが、eye を使ってみたい。

What's eye ?

eye のインストール

既に Focuslight が動いている環境にインストールする。

sudo gem install eye --no-ri --no-rdoc -V 

意外に簡単。

eye help

eye help を叩くと以下のように出力される。

Commands:
  eye break MASK[,...]       # break chain executing
  eye check CONF             # check config file syntax
  eye delete MASK[,...]      # delete app,group or process
  eye explain CONF           # explain config tree
  eye help [COMMAND]         # Describe available commands or one specific command
  eye history [MASK,...]     # processes history
  eye info [MASK]            # processes info
  eye load [CONF, ...]       # load config (run eye-daemon if not) (-f foreground load)
  eye match MASK[,...]       # match app,group or process
  eye monitor MASK[,...]     # monitor app,group or process
  eye oinfo                  # onelined info
  eye quit                   # eye-daemon quit
  eye restart MASK[,...]     # restart app,group or process
  eye signal SIG MASK[,...]  # send signal to app,group or process
  eye start MASK[,...]       # start app,group or process
  eye status                 # processes info (deprecated)
  eye stop MASK[,...]        # stop app,group or process
  eye trace [MASK]           # tracing log(tail + grep) for app,group or process
  eye unmonitor MASK[,...]   # unmonitor app,group or process
  eye version                # version
  eye watch [MASK]           # interactive processes info
  eye xinfo                  # eye-deamon info (-c show current config)

eye による Focuslight の各種プロセスの監視の試行錯誤(1)

Focuslightfocuslight new した後に作成されるディレクトリ以下に Procfile というファイルがあるので気になっていたが...

web: unicorn -E production -p $PORT -o $HOST
worker1: focuslight longer
worker2: focuslight shorter

上記のようになっている。これを eye の設定ファイルにも落としこんでみた。

mkdir focuslight/eye
cd focuslight

としてから...

vim eye/focuslight.eye

を以下のように書いた。

#
RUBY = '/usr/bin/ruby' # ruby on the server

Eye.config do
  logger '/tmp/eye.log'
end

Eye.application "focuslight_unicorn" do

  env "PATH" => "#{File.dirname(RUBY)}:#{ENV['PATH']}"
  working_dir "#{ENV['HOME']}/focuslight/"

  process("unicorn") do
    pid_file "tmp/unicorn.pid"
    start_command "unicorn -E production -p 5125 -o 0.0.0.0"
    stdall "log/unicorn.log"

    stop_signals [:TERM, 10.seconds]
    stop_command "pkill unicorn"
  end
  #
  process("focuslight longer") do
    pid_file "tmp/focuslight_longer.pid"
    start_command "focuslight longer"
    stdall "log/focuslight.log"

    stop_signals [:TERM, 10.seconds]
    stop_command "pkill focuslight"
  end
  #
  process("focuslight shorter") do
    pid_file "tmp/focuslight_shorter.pid"
    start_command "focuslight shorter"
    stdall "log/focuslight.log"

    stop_signals [:TERM, 10.seconds]
    stop_command "pkill focuslight"
  end

end

以下、簡単に解説。

  • 監視させたいプロセスを process メソッド(?)にを書く
  • start_command にプロセスの起動する為のコマンドを書く
  • stop_command にはプロセスを止める為のコマンドを書く
  • stop_signals プロセスの落とし方を書く(上記の例は 10 秒後に停止)
  • pid_file の設定は必須(実際にファイルが無くてもとりあえずはエラーにはならない)

書いたら eye c を実行して設定ファイルのシンタックスをチェックする。

cd focuslight
eye c eye/focuslight.eye

以下のように結果が表示される。

ということで eyeFocuslight を起動してみる。

ps aux | grep focuslight
ps aux | grep eye

焦らして...起動!

eye l eye/focuslight.rb

ブラウザからも確認してみる。

eye の稼働状況は eye i で確認する。

eye i

以下のように表示される。

おお。ちゃんと eye の監視下に focuslight がいるようだが...ログに以下のように記録されてしまい unicornfocuslight のプロセスが繰り返し落ちては起動してしまう現象が発生...。unicornfocuslightPIDeye から捕捉出来る方法が解ればなんとかなりそうだが...。

17.05.2014 15:49:42 WARN  -- [Eye::System] [shorter] sending :KILL signal to <4064> due to timeout (15s)
17.05.2014 15:49:42 ERROR -- [focuslight_unicorn:focuslight:shorter] execution failed with #<Timeout::Error: execution expired>; try increasing the start_timeout value (the current value of 15s seems too short)
17.05.2014 15:49:42 ERROR -- [focuslight_unicorn:focuslight:shorter] process <> failed to start ("#<Timeout::Error: execution expired>")
17.05.2014 15:49:42 INFO  -- [focuslight_unicorn:focuslight:shorter] switch :crashed [:starting => :down] (reason: monitor by user)

eye による Focuslight の各種プロセスの監視の試行錯誤(2)

上記のような状態で試行錯誤を繰り返して結局は以下の様な対応で eye から Focuslight の起動と停止、unicorn プロセスの監視を行えるようになった。(focuslight longerfocuslight shorter のプロセス監視は eye からは現時点では難しかった...)

まず、Procfile を以下のように修正。(Procfilefocuslight start を実行した際に呼ばれる foreman start が利用するファイル)

--- Procfile.original   2014-05-17 16:15:15.461024180 +0000
+++ Procfile    2014-05-17 16:15:19.040940430 +0000
@@ -1,3 +1,3 @@
-web: unicorn -E production -p $PORT -o $HOST
+web: unicorn -E production -p $PORT -o $HOST -c unicorn.rb
 worker1: focuslight longer
 worker2: focuslight shorter

Procfile と同じディレクトリにて unicorn.rb を以下のように作成する。

pid "tmp/unicorn_focuslight.pid"

次に Procfile と同じディレクトリにて tmp ディレクトリを作成。

mkdir tmp

そして、eye/focuslight.eye を以下のように設定する。

#
RUBY = '/usr/bin/ruby' # ruby on the server

Eye.config do
  logger '/tmp/eye.log'
end

Eye.application "focuslight" do

  env "PATH" => "#{File.dirname(RUBY)}:#{ENV['PATH']}"
  working_dir "#{ENV['HOME']}/focuslight/"
  #File.open(pid_file, 'w'){|f| f.write $$.to_s}

  process("unicorn_focuslight") do
    pid_file "tmp/unicorn_focuslight.pid"
    #start_command "foreman start"
    start_command "focuslight start"
    stdall "log/unicorn.log"
    stop_signals [:TERM, 10.seconds]
    stop_command "kill -9 {PID} && pkill focuslight"
  end
  #
end

この状態から eye を起動してみる。

cd ~/focuslight
eye l eye/focuslight.eye

暫くしてから eye i を実行すると以下のようにステータスが up となっている。

ブラウザからアクセスしてみる。

起動している。

また、eye stopfocuslight のプロセス全てを停止させてから改めて起動させたりしたのが以下の状態。


まとまってないけど

やったこと

  • Focuslight をざくっと試した
  • Focuslight のプロセスを eye で監視してみたけど期待した通りには動作させることが出来なかった

eye で出来たこと

  • Focuslight 全体の起動と停止
  • Focuslightunicorn プロセスの監視(落ちたら上げる)

eye で出来なかったこと

  • Focuslightfocuslight プロセスの監視(PID を取得する方法を模索する)
@inokappa
Copy link
Author

memo

Ruby でプロセス ID を取得して pid に書き出す

pid_file "path/to/file.pid"
File.open(pid_file, 'w'){|f| f.write $$.to_s}

ポイントは $$ でプロセス ID が取得出来る。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment