Jon's Tech Notes: Invoking a console from a deployed JRuby WAR file
Bundled WAR deployments in JRuby are one of the great things about a JRuby app. There’s something satisfying about creating everything your awesome web application needs to run in a single file and pushing that out to your app servers. No crazy C bindings to install, it’s all right there (and no late nights figuring out why a gem built on linux won’t compile on Solaris).
While the war file deployments are great for running the app, one of the things I’ve missed was the ability to invoke a production console, run scripts or rake (easily). I’ve hacked around this in the past by deploying copy of the code to another directory on the production machine, and running JRuby out of a whole separate install on that machine. While it works OK, it’s not ideal, and I’ve bent over backwards in my capistrano scripts trying to keep that remote directory in sync on deployments.
Plus, isn’t everything the app needs to run already in deployed WAR file anyway? It turns out you can, you just have to set up some paths correctly and invoke the org.jruby.Main class directly.
After some trial and error, I’ve adopted this technique for invoking a production console in a Rails 3 app deployed with Warbler on JRuby, directly from the Glassfish autodeploy directory:export APP_HOME=/usr/local/glassfishv3/glassfish/domains/test-app/applications/test-app/WEB-INFexport GEM_HOME=$APP_HOME/classpath:/META-INF/jruby.home/export GEM_PATH=$APP_HOME/gems/export APP_RUNNER="java -cp $APP_HOME/lib/jruby-core-1.5.3.jar:$APP_HOME/lib/jruby-stdlib-1.5.3.jar:$APP_HOME/lib/jffi-i386-SunOS.jar org.jruby.Main -r$APP_HOME/../META-INF/init"alias test-app="$APP_RUNNER"
That’s a bash script that sets up the necessary environment variables to run a production console.
I’ll generally source that file, and then run this to get a production console:source ~/test-app.bash cd $TEST_APP test-app script/rails c
To get your scripts in the production WAR, make sure to add this to your config/warble.rb file:config.dirs = %w(app config lib log vendor tmp script db/migrate)
For migrations, I take a similar approach, but it requires a little bootstrap script to invoke rake. I place this in scripts/rake:# Provides a way to run rake on deployed environments, straight out of the deployed war path require File.expand_path('../../config/application', __FILE__) require 'rake' Rake.application.run
Then I make sure to include the Rakefile in my config/warble.rb, along with my migrations:# Application directories to be included in the webapp. config.dirs = %w(app config lib log vendor tmp script db/migrate) # Additional files/directories to include, above those in config.dirs config.includes = FileList["Rakefile"]
Then migrations can be run on a production machine by:cd $TEST_APP test-app script/rake db:migrate
This can further be optimized by overriding the default deploy:migrate task in your capistrano script:namespace :deploy do desc "Runs the migrations using the deployed war" task :migrate, :roles => :db, :only => { :primary => true } do run "bash -c 'source /home/admin/test-app && cd $TEST_APP && $APP_RUNNER script/rake db:migrate'" end end
It’s not a perfect solution, but it seems to work pretty well. I’d love to hear what others have come up with for invoking scripts from deployed JRuby apps.