Tuesday, April 20, 2010

Exclusively require and load tasks for rake with bundler

Rails アプリケーションで Bundler の Gemfile を使う場合、
group :test do
gem :rspec-rails
end

のように Rails.env に応じたグルーピングができます。

rake でしか使わない gem とそのタスクをこの仕組でできないかなと思って、bundler の issue トラッカーでヒントを貰ってなんとかできました。

Gemfile では
group :tasks do
gem 'ar_fixtures'
gem 'yaml_waml'
end


Rakefile では
Bundler.require(:tasks)
def load_task(task_file)
load Dir[Bundler.bundle_path + "gems/*/{tasks,rails/tasks,lib/tasks}/#{task_file}.rake"].first
end
load_task "ar_fixtures"


これで、
$ rake -T db:fixtures:dump
...
rake db:fixtures:dump # Dump data to the test/fixtures/ directory.

Saturday, April 17, 2010

a rvm tips - install rubies as root, use them as a user

2010-07-30: 現状では Installing RVM System Wide に従ったほうがいいですね。たぶん。

rvm (Ruby Version Manager)を使う場合、ユーザーホーム以下に rubies から gems までインストールして使う方法が一般的なようですが、 rails のデプロイ先のサーバーや複数ユーザーで共有しているホストなど、それだと気持ち悪い。
root で ruby をインストールしてユーザーからそれ使いたいのですが、いまいち方法がよくわからなかったので試行錯誤して、とりあえず動くようになったので書いておきます。

まずは rvm gem をインストール
sudo gem install rvm

(rvm 自体はシェルスクリプトの集まりなので ruby がない状態からインストールできるようですが、やったことありません。Installing RVM を見てください。)

次に root に /usr/local/rvm ディレクトリを作成
sudo rvm-install


ユーザーの設定ファイル ~/.rvmrc に rubies と gems は /usr/local/rvm を見るように設定:
rvm_rubies_path=/usr/local/rvm/rubies
rvm_gems_path=/usr/local/rvm/gems


それからユーザーの ~/.rvm ディレクトリを作成
rvm-install

rvm-install の仰せの通りに source $HOME/.rvm/srcipt/rvm を設定、
いちどログインシェルを抜けてから、

いよいよ欲しい ruby をインストール。
rvmsudo rvm install ruby-head

通常の sudo だと sudoers に "Defaults setenv" を書いて -E しても PATH は渡されなかったりしていろいろ面倒なんですが、 rvmsudo を使うと PATH や GEM_HOME とかのruby関連の環境変数を引き渡せるので便利です。 rvm 以外にも gem, bundler などで root 権限が必要なときも rvmsudo が使えます。

先程 ~/.rvmrc ファイルに /usr/local/rvm を参照する設定を書きましたが、現状では ruby をインストールするごとに ~/.rvm/bin にシンボリックリンクを作成する必要があるようです。
(cd ~/.rvm/bin && ln -s /usr/local/rvm/bin/ruby-head ruby-head)


これで、ユーザーから通常通り
rvm ruby-head

とすれば switch できるようになりました。

もっといい方法がないか rvm google group にポストしたところ "it may never work" とは言われなかったので個人的にはこんな感じで使おうかと思ってます。

(ちなみに上記は rvm 0.1.27 で試しました)

Sunday, January 10, 2010

MacRuby now supports non-ASCII string in regular expressions

MacRuby には僕ら日本人にとって信じがたい、日本語(というかASCII以外)を含む文字列に対して正規表現が使えないという問題がありましたが、先日、修正されたようです。まだ試してませんが。

0.4 のコードベースのときに、日本人としてなんとかしないとと思い、修正しようとして挫折しましたが、r3189 changeset を見ると意外と差分が少ないですね。

MacRuby が Mac OS X 10.7 のシステムの一部として利用できるようになれば、重たい MacRuby.framework をアプリケーションに含めずにリリースできるようになることを期待しています。

最近、ブログ書くのが億劫になってましたが、 ソフトウェアの i18n に関心がある身としてちょっと嬉しかったので...。

Sunday, November 8, 2009

Google's OAuth doesn't seem to support wave as a scope yet?

After releasing Unofficial Google Wave Notifier, many people have claimed it may steal gogle password.
They are right. you should not trust me nor the app. From beginning I know the issue, I'm sorry, but I'v neglected the security concern as I hasten to release the app.
(A poor execuse to prove my innocence. I open the source. Suspicious users should review the source and build a binary from reviwed source.)

Current version (0.4) of the app uses ClientLogin for Installed Applications for authentication. In the process, apps handles passwords immediatly.

By contrast with ClientLogin, apps which uses OAuth for Installed Application handles oauth tokens instead of precisous passwords.

It is obvious that I should choose OAuth. However I can't get it so far. At OAuth Playground, I tried to get oauth_token. The paramerters I specified are:
scope: https://wave.google.com/wave/
oauth_signature_method: HMAC-SHA1
oauth_consumer_key: anonymous
consumer secret: anonymous
And the response is:
Invalid scope: https://wave.google.com/wave/


By choosing other scope, I can get a oauth_toke.... I missed something? or Google doesn' support wave as a oauth scope (yet)?

Tuesday, November 3, 2009

Unofficial Google Wave Notifier for mac 0.4 and the notification wave



I'v just released version 0.4 of Unofficail Google Wave Notifier for mac.
Also, if you wanna be notified about new releases, join The "unoffical google wave notifier for mac" notification wave.

CHANGELOG
* Open a specific wave immediately by clicking a wave in the menu
* Display "Checked N min ago" biside "Check Now" like "Google Notifier"

Friday, October 23, 2009

Queuing ruby background processes with Ernie and BERT-RPC

僕のようにインフラ系が弱い Rails プログラマにとっては、 GitHub のインフラ担当? Tom Preston-Werner (aka mojombo) の GitHub ブログポスト How We Made GitHub Fast - GitHub は非常に参考になります。なかでも興味を引いたのは続くポスト Introducing BERT and BERT-RPC で紹介されている BERT-RPC とその実装の Ernie ですね。

過去には Rails アプリケーションのバックグラウンド処理で苦労した思い出があるので今度ツールを選ぶときは以下の課題を解決してくれるやつがほしいなと思ってました。
1. 重たい処理が連続してリクエストされたときに並列で実行せずに1タスクごとキューイングして実行してほしい
2. worker が死んでも自動で代わりを起動してほしい
3. 非同期も同期(終了するまでブロック)も両方OK

1 は Ernie の -n オプションで同時実行される handler の数は制限できるので -n 1 にすれば queue になるはず。

2 は BERT-RPC は call (同期), cast (非同期), info (コールバック情報などの送付) 3つの request が定義されていて、 ernie (v0.1) では call と cast の両方をサポートしているので同期/非同期両方OK。

3 はうろ覚えだけど Erlang でふつうにサーバーを実装すれば wroker (handler) の再起動とかはかってにやってくれるはず。実際に handler の ruby プロセスを kill してもすぐ代わりが起動するので大丈夫そうだ。

実際に queue になるか試してみた。 erlang ernie をインストール、簡単な handler を書いて、サーバーを handler 1つで起動。

$ sudo port install erlang +ssl # 時間かかります
...
$ sudo gem install ernie -s http://gemcutter.org

$ cat > sleep.rb
require 'rubygems'
require 'ernie'
mod(:sleep) do
fun(:wait) do |idx, sec|
sleep sec
print "#{idx}: awake from #{sec} sec.\r\n"
end
end
^d

$ ernie -p 9999 -n 1 -h sleep.rb
erl -boot start_sasl -detached +Bc +K true -smp enable -pz /Library/Ruby/Gems/1.8/gems/ernie-1.0.0/bin/../ebin \
-ernie_server_app port 9999 -ernie_server_app handler '"sleep.rb"' -ernie_server_app number 10 -ernie_server_app log_level 2 -run ernie_server_app boot
...

別 shell の irb で 2秒スリープ指定 で 10回 cast。

$ irb -r bertrpc
>> svc = BERTRPC::Service.new('localhost', 9999)
=> #
>> 10.times{|i| svc.cast.sleep.wait(i, 2) }
=> 10

$ ernie を実行した erlang shell では

0: awake from 2 sec.
1: awake from 2 sec.
2: awake from 2 sec.
3: awake from 2 sec.
4: awake from 2 sec.
5: awake from 2 sec.
6: awake from 2 sec.
7: awake from 2 sec.
8: awake from 2 sec.
9: awake from 2 sec.

実際に見てると2秒ごとに出力されていたので、ちゃんと queuing されてますね。

Sunday, October 18, 2009

Unofficial Google Wave Notifier for mac

2009-11-17: If you have any idea, feel free to issue a feature request, or vote them (the small triangle at bottom-left corner of a item)
2009-11-3: version 0.4 released



I wolud like to know whether I have unread messages at Google Wave or not, like Google Notifier for Mac for GMail and Google Calendar. I could find Google Wave Add-on for Firefox at That Smith. So I implemented one as referencing the add-on. Thank you Chad at That Smith.

You can download it form Downloads for hiroshi's Unofficial-Google-Wave-Notifier - GitHub. Before download it, be sure that is makes NO WARRANTY.

Google Wave の新着通知も Google Notifier for Mac のように通知されたいのですが、探しても Google Wave Add-on for Firefox at That Smith しか見つからなかったので、その Add-on のソースを参考にして作ってみました。

Downloads for hiroshi's Unofficial-Google-Wave-Notifier - GitHub からダウンロードできます。