• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
 
 

このブログシステムは長いことapache2/passengerで運用してきましたが、 頻繁にrubyプロセスが暴走してアクセスできなくなるということが発生していたので、apache2/mod_proxy_balancer/mongrel_clusterの構成に変えてみました。

しばらく様子見です。

posted by Png genki on Sat 3 Apr 2010 at 21:38

しばらく前から安定性が悪くなってきたのですが、ここに来て頻繁にPassengerのプロセスが暴走するようになってきました。

  • ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-linux]
  • Passengerのバージョンは2.2.8
  • Rails 2.3.5

プログラムやシステム構成はここしばらくほとんど変えておらず、トラフィックも大きな変動はないので、うわさのEC2のサービス劣化の影響でしょうか。

posted by Png genki on Thu 25 Feb 2010 at 02:11

しばらくGemを作ってなかったので気がつかなかったのですが、 Gem作りを取り巻く環境が結構変わっているようですね。

まず、newgemやcutagem, hoeのようにgemのひな形を作ってくれる jeweler というtoolが結構使われているようです。これはgithubにインテグレートされていて非常に便利です。

githubはちょっと前からgemをホストするgemリポジトリサービスをやめてしまったようで、代わりに Gemcutterを使うようになっています。 jewelerを使う事で、gemcutterやrubyforgeにgemをpushする事ができます。

posted by Png genki on Sat 17 Oct 2009 at 10:20

たまにRubyforgeが落ちていてgem installする事ができなくなっている場合がありますが、そんな場合には以下のミラーサーバが利用できるかもしれません。

ローカルにダウンロードしてから sudo gem install します。

posted by Png genki on Sat 10 Oct 2009 at 20:03

GAEなどでファイル数の多いライブラリを使うために、gemのパッケージから不要そうなファイルを消してjarファイルにまとめるために作ったスクリプトです。

   1  #!/usr/bin/env ruby
   2  
   3  def run(command)
   4    puts command
   5    system command
   6  end
   7  
   8  case ARGV[0]
   9  when 'install'
  10    run "jgem install -i ./gems --no-ri --no-rdoc #{ARGV[1]}"
  11  when 'uninstall'
  12    run "jgem uninstall -i ./gems #{ARGV[1]}"
  13  end
  14  
  15  Dir['gems/gems/*'].each do |dir|
  16    %w(
  17      Generaters TODO README.* README
  18      spec/**/* test/**/* examples/**/* tasks/**/*
  19      tutorial/**/* demo/**/* website/**/*
  20      CHANGES CHANGES.txt CHANGELOG
  21      LICENSE MIT-LICENSE Rakefile CONTRIBUTORS
  22      Manifest.txt History.txt install.rb setup.rb
  23    ).each do |pattern|
  24      Dir[File.join(dir, pattern)].each do |path|
  25        run "rm -rf #{path}"
  26      end
  27    end
  28  end
  29  
  30  Dir['gems/cache', 'gems/bin', 'gems/doc'].each do |path|
  31    run "rm -rf #{path}"
  32  end
  33  
  34  run "jar cf lib/merb.jar -C ./gems ."

posted by Png genki on Mon 7 Sep 2009 at 19:16 with 2 comments

Rub はHaml的なEndless Rubyを気軽にはじめるために作ったGemです。 GitHubをsourceに追加して以下のようにインストールできます。

   1  % sudo gem install genki-rub

ソースコード を見るとわかりますが、非常に短いので現時点では、if文の複数行にまたがる条件文などには対応していません。気が向けばそのうち対応するかもしれません。

Rubを使ってSinatraアプリを作るには以下のようにします。

config.ru

   1  require "rubygems"
   2  require "rub"
   3  rub "app.rb"
   4  run Sinatra::Application

app.rb

   1  require "sinatra"
   2  require "dm-core"
   3  require "haml"
   4  
   5  DataMapper::setup(:default, ENV['DATABASE_URL'] || 'sqlite3://db.sqlite3')
   6  
   7  class Post
   8    include DataMapper::Resource
   9    property :id, Serial
  10    property :content, Text
  11    auto_upgrade!
  12  
  13  get "/" do
  14    @posts = Post.all(:order => [:id.desc])
  15    haml :index
  16  
  17  post "/" do
  18    Post.create(params)
  19    redirect "/"
  20  
  21  __END__
  22  @@ index
  23  %h1 Hello, Sinatra!
  24  %ul
  25    - @posts.each do |post|
  26      %li= post.content
  27  %form{:method => :post}
  28    %textarea{:name => :content}
  29    %input{:type => :submit, :value => "Post"}

面白い点として、 Endless RubyでもVimやこのブログのSyntaxハイライトはちゃんと機能してくれます(Emacs使いのレポート求む)

必要なファイルはこれだけです。あとはおもむろにshotgunしましょう。

   1  % shotgun
   2  == Shotgun starting Rack::Handler::Mongrel on localhost:9393

あとはhttp://localhost:9393にアクセスするだけです。 app.rbを適当に書き換えてリロードすると、変更が反映される事が確認できます。

shotgunは

   1  % sudo gem install shotgun

でインストールできます。

posted by Png genki on Mon 20 Jul 2009 at 05:09 with 2 comments

Gemを作るのが面倒になってきたので、githubから直接requireできたら楽になるかもしれないと思い、試してみました。

   1  def git(uri, sha1, options = {})
   2    require "tmpdir"
   3    basename = File.basename(uri)
   4    outdir = File.join(Dir.tmpdir, basename, sha1)
   5    unless File.exist?(outdir)
   6      sh = proc{|command| IO.popen("#{command} 2>&1"){|io| io.read}}
   7      sh["git clone #{uri} #{outdir}"]
   8      sh["cd #{outdir}; git checkout #{sha1}"]
   9    end
  10    $:.unshift File.join(outdir, 'lib')
  11    require options[:require_as] || basename.split(/\.git$/)[0]
  12  end

Dir.tmpdirを使ってOSのテンポラリディレクトリの下にリポジトリをクローンしてきて、指定したリビジョンをcheckoutし、LOAD_PATHに"lib"を加えて、リポジトリ名のファイルをrequireしています。

意外と普通にgemの代わりに使える感じです。

posted by Png genki on Thu 16 Jul 2009 at 17:40
14th Tue

Endless Ruby

HAMLを使うようになってから、endの無いrubyが欲しい今日この頃。 あわや自作する直前、先人の仕事を見つけました。

[ANN] Endless Ruby 0.0.2

endless.rb is a pre-processor for ruby which allows you to use python-ish indentation to delimit scopes, instead of having to type 'end' every time.

良いですね。残念ながら、現時点ではrubylexerのrequireでエラーが出るようですが、一定の制約付きでもかまわないので動くようになるとありがたいです。 JRubyのCharles Nutterも割と 肯定的なコメント を寄せていますね。

Oh dear god no!

Seriously though...neat :)

-- Charlie

JRubyのオプションに--endlessがつく?

posted by Png genki on Tue 14 Jul 2009 at 17:41

RGB値と三刺激値(tristimulus values)XYZの相互変換を行う方法のメモ。

   1  def xyz2rgb(x, y, z)
   2    r =  3.240479 * x - 1.53715  * y - 0.498535 * z;
   3    g = -0.969256 * x + 1.875991 * y + 0.041556 * z;
   4    b =  0.055648 * x - 0.204043 * y + 1.057311 * z;
   5    [r, g, b]
   6  end
   7  
   8  def rgb2xyz(r, g, b)
   9    x = 0.412453 * r + 0.35758  * g + 0.180423 * b;
  10    y = 0.212671 * r + 0.71516  * g + 0.072169 * b;
  11    z = 0.019334 * r + 0.119193 * g + 0.950227 * b;
  12    [x, y, z]
  13  end

posted by Png genki on Mon 6 Jul 2009 at 03:44

RubyGemsを使うときに、たびたびGemのバージョンを指定する機会があります。 '=', '>=', '>' などの等号、不等号は、対応するRubyのオペレータと同じような機能なのでわかりやすいのですが、'~>'についてはどういう意味なのか若干わかりにくい感じです。一応マニュアルにはこんな感じに書かれています。

  • = Equals version
  • != Not equal to version
  • > Greater than version
  • < Less than version
  • >= Greater than or equal to
  • <= Less than or equal to
  • ~> Approximately greater than (see "Pessimistic Version Constraint" below)

'~>' は、悲観的(pessimistic)なバージョン指定です。 例えば、'~> 1.2.0' は、'1.2.0', '1.2.1.1' などにはマッチしますが、 '1.3'にはマッチしません。 最後の数字の桁だけがGrater than or equal toで、それ以外は厳密一致のようです。

posted by Png genki on Sat 6 Jun 2009 at 02:04