Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Sunday, August 15, 2010

a way to specify a ruby with rvm for new windows of a screen session

$ rvm use --default ruby-1.9.2

の状態で
$ rvm use system

で利用する ruby を切り替えても screen の window の環境は新規のシェルセッションになるので ruby-1.9.2 に戻ってしまう。
screen が新規 window を作成するときに利用する shell のスタートアップスクリプトが実行され初期化されてしまうので当たり前といえば当たり前。

そこで、シェルスクリプトの source rvm の直後に以下のおまじないを書いて、
if [[ -n "$rvm_use" ]] ; then
rvm use $rvm_use
fi


環境変数 rvm_use を与えて screen を起動すると、
rvm_use=system screen

screen の新規 window が作成されるたびに rvm use system が実行されるようになります。

もっといい方法があるかと思うので、知ってる人は教えてください。
RVM inside Screen に描いてあることは、 screen の新規 window で script/rvm が読み込まれるようにするだけだったので残念。

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 で試しました)

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 からダウンロードできます。

Thursday, July 30, 2009

Passenger: Keeping ApplicationSpawner alive speeds up spawning an instance (updated)

前回 passenger プロセスの起動時間について書いたとき passenger のバージョンは 2.0.3 でしたが、あれから 9ヶ月、 2009-07-29 現在の最新バージョンは 2.2.4 になってます。

そのときは passenger の constants.rb を直接変更するという荒技で FrameworkSpawner と ApplicationSpawner のタイムアウト時間を長くしてましたが、いつのまにか RailsFrameworkSpawnerIdleTimeRailsAppSpawnerIdleTime という Apache の設定で変更できるようになってます。こいつらを 0 にすれば FrameworkSpawner は Apache を再起動するまで、 ApplicationSpawner は Apache を再起動するか、 touch restart.txt するまで持続します。

また、前回の記事 では Capistrano でデプロイしたときに (タイムアウトを延ばしているときは特に) ApplicationSpawner を KILL しないと古いデプロイの ApplicationSpawner がずっと残ると書いたんですけど、 2.2.0 の "Support for Capistrano-style deployments" により、 Capistrano 使ってても touch restart.txt で ApplicationSpawner が再起動するので KILL する必要は無くなりました。
(でも、2.2.0 から 2.2.2 までは 2.2.3 の "Fixed restarting of the ApplicationSpawner server" が修正されてなかったので worker process がみんなタイムアウトしている間に restart すると古い ApplicationSpawner が残っちゃってたみたいです。)

ちなみに、Apache 再起動直後の FrameworkSpawner も ApplicationSpawner もいないときと、 worker process だけ fork するときの速度をブラウザのページロード時間で測ると、(僕の環境では)5.6秒が1.0秒になるので、デフォルト設定で FrameworkSpawner がタイムアウトする時間30分に一度アクセスがあるかどうかわからないようなサービスではこの設定は必須かと思われます。

2009-7-30 10:00 追記


結論だけ言うと、 passenger-install-apache2-module を実行した直後に追加する apache の設定に1行追加すればOKです。

PassengerRoot /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.4
PassengerRuby /opt/ruby-enterprise-1.8.6-20090610/bin/ruby
+ RailsAppSpawnerIdleTime 0

ApplicationSpawner が常駐することによるメモリ消費は僕の小さなアプリケーションREE を使っていれば private メモリで 25M 程度でした。これだけのコストでアプリケーションのレスポンスタイムの5秒ぐらいのロスが無くなるなら大歓迎でしょう。 Passenger のデフォルト設定でもいいかと思うぐらい。

Friday, July 17, 2009

FeedKraft prototype version


I just announce that my new application service, FeedKraft, is available. As you notice from the name, it deals RSS and Atom feeds. I said it deals feeds, but for now, there is one function, feed filtering.

As for the filter function, we know there are many other filter services out there. I think FeedKraft is easier to use than Yahoo! Pipes, and little bit more powerful than FeedRinse.

The source code, implemented in Ruby on Rails, is also available here on GitHub.

Anyway, give it a try.


FeedKraft という RSS や Atom フィードを扱うサービスを公開しました。
が、今のところは外部のフィードにフィルターをかける機能しかありません。

フィードのフィルターに関しては、同様の機能を提供するサービスはいくつかありますが、Yahoo Pipe より簡単で、 FeedRinse よりちょっと強力かと思います。

特徴としては、
* 入力フィードを解析してファイルター可能な候補を選択できる (例: このブログの Atom フィードをカテゴリ Rails でフィルター)
* 作成したフィルターは公開され、他のユーザーが作成したフィルターも利用できる
* フィルター結果の feed はログイン状態での購読(subscribe)により生成されるURLにより参照する必要があるので、何人購読しているか分かる

どんだけ意味があるかわかりませんが、フィルター結果などのフィードが公開されることで、フィード提供側と利用側によるコミュニティ的な要素が広がるといいかなと思ったりもしてます。

ちなみに、Rails で実装してます。ソースも GitHub にありますので興味があったらどうぞ。

Friday, July 10, 2009

pony += attachment files + TLS

2010-07-30: My fork of pony is merged into benprew's pony - GitHub. Try it instead.
$ sudo gem install pony



I don't have any other servers, I mean real hardwares, apart from the server my web applications reside. However, I need to keep backup files of database dump or something precious safe from destructions. So I decide to send backup files to a gmail account.

pony makes easy to write a script sending mails, but it can't attach files nor use TLS transport.

Using Tmail, it's easy attaching some files to a mail. And Net::SMTP, included in ruby 1.8.7 or latter, can use TLS/SSL transport without hustle (for ruby 1.8.6 or earlier, there is the smtp_tls gem). So I forked and patched pony.

I sent a pull request to the author of pony, but for now, you can use my fork:

Web アプリケーションの DB のバックアップをとりたいけど、サーバーが1台しかないのでバックアップファイルをメールに添付して gmail のアカウントに送ることにしました。

pony を使うと簡単に送信することができます。 Pony はSMTP 経由で送信もできるんですけど、今回必要なファイル添付と gmail の SMTP サーバーが要求する TLS/SSL 通信も対応してません。

ファイル添付は TMail で簡単にできるし、TLS/SSL も ruby 1.8.7 以降の Net::SMTP なら実装され、 1.8.6 以前でも smtp_tls gem をインストールすることにより利用できるので、それらに対応できるよう Pony を fork して変更してみました。

Pull リクエストを pony の作者に送ったんですが、マージしてくれるかわからないのでこれを使って下さい。



$ gem sources -a http://gems.github.com
$ sudo gem install hiroshi-pony



Here is a sample:

メールを送るスクリプトはこんな感じ




require "rubygems"
require "pony"
Pony.mail(
:to => "example@gmail.com",
:subject => "DB backup",
:via => :smtp,
:smtp => {
:host => "smtp.gmail.com",
:port => "587",
:auth => :plain,
:user => "example",
:password => "********",
:tls => true
},
:attachments => {"database_name.sql.gz" => `pg_dump database_name | gzip`}
)

Monday, November 24, 2008

mysql_xml2google_notebook_atom

Google Notebook を使うようになってから、以前に作ったメモを溜める web application が必要なくなったけれど、メモの内容は参照したいことがあったので、Google Notebook にインポートしてみました。

その web application は mysql を使っていたので、 mysql コマンドの --xml オプションで出力した結果の XML を Google Notebook がインポートできる Atom フォーマットの XML に変換するスクリプト mysql_xml2google_notebook_atom.rb を書き、変換してからインポート。

余談ですが、Google Notebook には Atom フォーマットでエクスポートする機能もあります。しかし、残念なことに XML の encoding を iso-8859-1 (latin-1) から変更する方法が分からないので、日本語のノートは Atom ではエクスポートできない。
どこにフィードバックを送ればよいのかわからないので、Google Notebook Official Blog の記事にコメントしました。オープンソースでないとこういうところがもどかしい。