2018年以降の記事はGitHub Pagesに移行しました

Windows版Redmineをサービスに登録してブート時に起動させる(宿題あり)

あらすじ

タイトル通り、サービスに登録してよろしく起動してほしい。mongrelを使えばサービスから起動できる……らしいが、名前は聞いたことあるけど、mongrelが何かは知らない……。

Ruby + C(拡張ライブラリ) で書かれた httpd

http://d.hatena.ne.jp/keyword/mongrel

なるほど。

環境

手順

前提
  • Redmineの環境構築が完了している事
    • bundle exec ruby script/server -e production で起動できる事
インストールから起動まで
  • まずはRedmineデフォルトのGemfileにmongrelを追加し、bundle install
gem "mongrel"
$ bundle install
Fetching source index for http://rubygems.org/
Using rake (0.9.2.2)
Using activesupport (2.3.14)
Using rack (1.1.3)
Using actionpack (2.3.14)
Using actionmailer (2.3.14)
Using activerecord (2.3.14)
Using activeresource (2.3.14)
Installing cgi_multipart_eof_fix (2.5.0)
Using coderay (1.0.6)
Installing gem_plugin (0.2.3)
Using i18n (0.4.2)
Installing mongrel (1.1.5)
Using mysql2 (0.2.18)
Using net-ldap (0.3.1)
Using pg (0.13.2)
Using rails (2.3.14)
Using ruby-openid (2.1.8)
Using sqlite3 (1.3.6)
Using tzinfo (0.3.33)
Using bundler (1.0.21)
Your bundle is complete! It was installed into ./vendor/bundle
  • bundle exec ruby ...で起動…しようとするとエラー
msvcrt-ruby18.dll が見つからなかったため、このアプリケーションを開始できませんでした。アプリケーションをインストールし直すとこの問題は解決される場合があります。
$ gem list
...
json (1.5.4)
...
$ gem uninstall json
Successfully uninstalled json-1.5.4
-gem "mongrel"
+gem "mongrel", ">= 1.2.0.pre2"
$ bundle install
...
Installing mongrel (1.2.0.pre2)
  • 起動! 起動した! WEBrickで起動していたのがMongrelに変わった!
=> Booting WEBrick
=> Booting Mongrel

に変わった!

サービスに登録

  • mongrel_serviceをGemfileに登録
gem "mongrel_service"
  • インストールできたらmongrel_railsというコマンドが追加される(今回はbundlerでインストールしたので、Redmineのrootまで行ってbundle exec mongrel_rails)
[C:\redmine-1.4.0]
$ be mongrel_rails
Usage: mongrel_rails <command> [options]
Available commands are:

 - restart
 - start
 - stop
 - service::install
 - service::remove
  • Serviceに追加
オプション 意味
-N サービス名
-c Redmineのルート
-p 起動ポート
-e Railsの起動モード
$ be mongrel_rails service::install -N "Redmine" -c C:\redmine-1.4.0 -p 3000 -e production
** Copying native mongrel_service executable...
Redmine service created.
  • 登録に成功したのでWin+rからservices.mscを呼び出して確認……
  • あった!
    • スタートアップの種類が手動になっていたので自動に変更
  • マシン再起動!

起動しない……だと……?

サービスから起動できなかった
  • Redmineフォルダ内のlogにmongrel.logがあるので見てみると
C:\RUBY_ROOT\bin\ruby.exe: No such file or directory -- C:/RUBY_ROOT/bin/mongrel_rails (LoadError)

うーん。RUBY_ROOTのbin下にmongrel_railsを探しに行ってる? ……でも今回はbundlerで入れたからREDMINE_ROOT/vendor/bundle下を見に行ってほしいんだけどなぁ。

ちょっと試しにbundlerからmongrel, mongrel_serviceをはずして、gemで直接Mongrelをインストールしてみる。

$ gem install mongrel --pre
$ gem install mongrel_service

これで

ということになり、RUBY_ROOTにMongrelがあるので、再起動すればサービスから起動してくれるはず……。

再々起動…起動した!

まとめ
  • 一応、サービスから起動させる事はできた
  • ただし、直接gem installしたものに限る。サービスから起動させるとRUBY_ROOTを見にいってしまうようなのでbundlerで入れるとLoadErrorになってしまう
宿題

mongrel_railsコマンドのオプションとかでbundlerから起動するように変えられないかな?