What have you found for these years?

2010-11-25

bundle exec rainbows

add ber (bundle exec rainbows ...), which reads configs from .config/ruby
速記,所以用中文好了。最早我也跟其他人用 ./script/server 來跑 rails,
因為 mongrel_rails 覺得不是很方便。後來有 thin 之後,就都改用
thin start 了,這肯定比 ./script/server 來得好用多了。

接著我開始改用 unicorn_rails, 這比 thin 更好 (command line interface-wise)
本來是想用 unicorn 就好,無奈 rails 不肯給 config.ru, 也懶得自己做。
就這樣又用了一段時間,引入 bundler 後又有困擾了。因為
unicorn 沒有被加到 Gemfile 裡,所以無法被 bundler 照顧到,
也就是說,他會讀到最新的 rack, 然後跟 (stupid) rails 衝突。

之前我的解決方法是,在 Gemfile 裡面加上這一段:

path = File.expand_path('~/.config/ruby/Gemfile')
eval(File.read(path)) if File.exist?(path)

用這種方式,在不干擾 Gemfile 的情況下,插入我自己的 Gemfile,
裡面就寫著 gem 'unicorn' 這樣。然後我另外寫了一個叫 gemfile 的
command line tool, 會自動註解掉 gem 'unicorn' 然後重新產生
Gemfile.lock, 避免把 unicorn 加到裡面去,因而影響他人。

這也很煩,因為我會一直看到我的 git repository 是 dirty 的,
因為 Gemfile.lock 裡面多了個 unicorn. 我需要不斷打 gemfile 整理
Gemfile.lock, 才能讓 working copy 變得 clean.

然後剛剛受不了 unicorn 只跑一個 worker, 有時候會整個卡住,
原因不明。可能是某個 facebook api 又掛了之類的..? 我也懶得追究了。
總之,現在我想跑兩個 worker, 那我就需要一個 unicorn config,
裡面寫 worker_processes 2. 也就是說我又需要額外一個檔案了...
既然都要處理了,何不乾脆弄到完美算了?不如就用 rainbows 吧!

於是我需要 rails 的 config.ru, 因為沒有 rainbows_rails..
抄襲了 unicorn_rails 裡面的 code, 我做出了 rails_config.ru
然後做了一個 rainbows.rb 放他的 config.
最後用 ber 告訴 rainbows 這些設定檔放在哪裡。
當然,bundle exec 是少不了的,不然又會碰上老問題。

最後的問題是,我不想再手動打 gemfile 整理 Gemfile.lock 了..
所以乾脆在 server 啟動時自動呼叫 gemfile. 而我又不希望這造成
server 啟動速度下降,又用 Thread 包了一層:

Thread.new{ load '~/bin/gemfile' }

最後就變成這樣了,感覺好像滿複雜的.. 不過運作很順利。
現在我可以在任何 rails project 下跑 ber 則自動跑 rainbows
兩個 worker 和 eventmachine backend. 未來需要動到什麼
設定,也只要改這所有 project 共用的 config 即可。
可喜可賀,可喜可賀...

p.s. 噢,我現在才注意到,這方法搞不好不能在一般 mac 上跑,
因為我的 filesystem 是 HFS+ case-sensitive, 不是預設的
HFS+ case-insensitive. 我猜預設的會把 gemfile 跟 Gemfile 搞混吧..

p.s.2 然後我還有偷改 ./script/server, 因為他預設是跑 mongrel,
或是 webrick, 但不會跑 thin. 我硬把他改成 default to thin...
畢竟我可不希望在 Gemfile 裡面寫 mongrel, 但我願意寫入 thin,
heroku 也本來就是用 thin, 這樣讓 server 也一致絕對是好事。

0 retries:

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0