先介绍一下god做什么用的,google一下‘rails god’,第一条搜索就是“God - A Process Monitoring Framework in Ruby”,我就不翻译了,进入正题。
作为是rails里很常用的一个进程监控框架,god在rails项目里是很容易配置,很容易使用的,学习成本几乎为零,至少在我接触到的项目里,基本上都是用它,可能还有其他更好用的框架,原谅笔者知识太窄了。
下面先以一个小例子来看看rails里怎么引入god框架。
搭建rails:
rails new demo_app
gem 'rails_config', '~> 0.3.1'
gem 'god', '~> 0.13.4', :require => false
lib/tasks下新增一个文件:
lib/tasks/god_test.rake
内容如下:
# encoding: utf-8
namespace :god_dome do
desc "god_dome"
task run: :environment do
file = File.join(Rails.root, 'log', "god_dome_run.log")
logger = Logger.new(file)
loop do
logger.info "now is: #{Time.now}" puts "now is: #{Time.now}" sleep(2) end
end
end
[chenyafei@develop demo_app]$ bundle exec rake god_dome:run
now is: 2016-04-09 13:57:32 +0800
now is: 2016-04-09 13:57:34 +0800
now is: 2016-04-09 13:57:36 +0800
now is: 2016-04-09 13:57:38 +0800
now is: 2016-04-09 13:57:40 +0800
now is: 2016-04-09 13:57:42 +0800
新建一个god的目录:
mkdir god
新建一个文件 god/first.god
文件内容如下:
# encoding: utf-8
# run with: RAILS_ENV=development bundle exec god -c god/activity_mall.god -D
RAILS_ENV = ENV['RAILS_ENV'] || 'production' unless defined?(RAILS_ENV)
RAILS_ROOT = ENV['RAILS_ROOT'] || File.expand_path('../../', __FILE__) unless defined?(RAILS_ROOT)
God.watch do |w|
w.dir = RAILS_ROOT
w.name = "god_demo"
w.start = "bundle exec rake god_dome:run"
w.keepalive
end
god -c god/first.god
tail -f log/god_dome_run.log
哈哈,看到文件输出了吧:
[chenyafei@develop demo_app]$ tail -f log/god_dome_run.log
I, [2016-04-09T14:03:51.527280 #5830] INFO -- : now is: 2016-04-09 14:03:51 +0800
I, [2016-04-09T14:03:53.527568 #5830] INFO -- : now is: 2016-04-09 14:03:53 +0800
I, [2016-04-09T14:03:55.527927 #5830] INFO -- : now is: 2016-04-09 14:03:55 +0800
I, [2016-04-09T14:03:57.528300 #5830] INFO -- : now is: 2016-04-09 14:03:57 +0800
I, [2016-04-09T14:03:59.528679 #5830] INFO -- : now is: 2016-04-09 14:03:59 +0800
I, [2016-04-09T14:04:01.529079 #5830] INFO -- : now is: 2016-04-09 14:04:01 +0800
I, [2016-04-09T14:04:03.529460 #5830] INFO -- : now is: 2016-04-09 14:04:03 +0800
I, [2016-04-09T14:04:05.529829 #5830] INFO -- : now is: 2016-04-09 14:04:05 +0800
I, [2016-04-09T14:04:07.530254 #5830] INFO -- : now is: 2016-04-09 14:04:07 +0800
I, [2016-04-09T14:04:09.530639 #5830] INFO -- : now is: 2016-04-09 14:04:09 +0800
我们把rake进程kill 掉看看,输出还在进行不:
[chenyafei@develop demo_app]$ ps -ef | grep god
root 1381 1 0 Feb15 ? 05:42:31 mongod --config=/etc/mongod.conf
502 3525 3200 0 10:37 pts/5 00:00:00 tail -f log/god_test.log
502 4893 1656 0 13:46 pts/1 00:00:04 vim lib/tasks/god_test.rake
502 5957 5840 0 14:04 pts/8 00:00:00 tail -f log/god_dome_run.log
502 5970 1 0 14:08 pts/2 00:00:00 ruby /home/chenyafei/.rvm/gems/ruby-2.1.1/bin/god -c god/first.god
502 5986 1 12 14:08 ? 00:00:01 ruby /home/chenyafei/.rvm/gems/ruby-2.1.1/bin/rake god_dome:run
502 5992 2695 0 14:08 pts/2 00:00:00 grep --color god
[chenyafei@develop demo_app]$ ps -ef | grep god
root 1381 1 0 Feb15 ? 05:42:31 mongod --config=/etc/mongod.conf
502 3525 3200 0 10:37 pts/5 00:00:00 tail -f log/god_test.log
502 4893 1656 0 13:46 pts/1 00:00:04 vim lib/tasks/god_test.rake
502 5957 5840 0 14:04 pts/8 00:00:00 tail -f log/god_dome_run.log
502 5970 1 0 14:08 pts/2 00:00:00 ruby /home/chenyafei/.rvm/gems/ruby-2.1.1/bin/god -c god/first.god
502 5986 1 12 14:08 ? 00:00:01 ruby /home/chenyafei/.rvm/gems/ruby-2.1.1/bin/rake god_dome:run
502 5992 2695 0 14:08 pts/2 00:00:00 grep --color god
[chenyafei@develop demo_app]$ kill 5986
[chenyafei@develop demo_app]$ ps -ef | grep god
root 1381 1 0 Feb15 ? 05:42:31 mongod --config=/etc/mongod.conf
502 3525 3200 0 10:37 pts/5 00:00:00 tail -f log/god_test.log
502 4893 1656 0 13:46 pts/1 00:00:04 vim lib/tasks/god_test.rake
502 5957 5840 0 14:04 pts/8 00:00:00 tail -f log/god_dome_run.log
502 5970 1 0 14:08 pts/2 00:00:00 ruby /home/chenyafei/.rvm/gems/ruby-2.1.1/bin/god -c god/first.god
502 5996 1 13 14:08 ? 00:00:01 ruby /home/chenyafei/.rvm/gems/ruby-2.1.1/bin/rake god_dome:run
502 6004 2695 0 14:09 pts/2 00:00:00 grep --color god
那么god进程和它启动的进程是什么关系呢?是不是父进程和子进程的关系呢?我们kil掉god进程看看:
[chenyafei@develop demo_app]$ kill -9 5970
[chenyafei@develop demo_app]$ ps -ef | grep god
root 1381 1 0 Feb15 ? 05:42:31 mongod --config=/etc/mongod.conf
502 3525 3200 0 10:37 pts/5 00:00:00 tail -f log/god_test.log
502 4893 1656 0 13:46 pts/1 00:00:04 vim lib/tasks/god_test.rake
502 5957 5840 0 14:04 pts/8 00:00:00 tail -f log/god_dome_run.log
502 5996 1 1 14:08 ? 00:00:01 ruby /home/chenyafei/.rvm/gems/ruby-2.1.1/bin/rake god_dome:run
后续空了,源码研究之后,再补充吧。
god还有很多相关的配置,详细的见官方文档吧,这里就不详细说了:http://godrb.com/