Rails preloader for JRuby (similar to Zeus, Spring and Spork)
Switch branches/tags
Nothing to show
Clone or download
mrbrdo Merge pull request #21 from lakim/rake_env
Accept rake env variables as command parameters
Latest commit b5cfaaf Mar 15, 2016
Permalink
Failed to load latest commit information.
bin
lib
.gitignore
Gemfile
Gemfile.lock
MIT-LICENSE
README.rdoc
theine.gemspec

README.rdoc

Theine

Theine is a Rails application pre-loader designed to work on JRuby. It is similar to Zeus, Spring and Spork. The problem with Zeus and Spring is that they use `fork` which doesn't work on JRuby.

time rails runner "puts Rails.env"
48.31s user 1.96s system 242% cpu 20.748 total  # normal
time theine runner "puts Rails.env"
0.12s  user 0.02s system 32%  cpu 0.449  total  # Theine

Installing

You need to install screen on your system. For example on Ubuntu:

sudo apt-get install screen

Then install the gem.

gem install theine

You don't need to add it to your project's Gemfile.

Upgrading

If you want to use Ruby MRI for the client, you will probably need to re-run theine_set_ruby after upgrading.

Using

Start up the theine server in the root of your Rails project:

theine_server

You can also add `–debug` mode so that the spawned workers will be running in debug mode:

theine_server --debug

Then run any rails command using theine (don't use bundle exec):

# Rails commands
theine runner "puts 'Hello world'"
theine server
theine console

# Rake, rspec
theine rake db:migrate
theine rspec spec

Configuration

Theine will look for a `.theine` file in your Rails app's root directory and in your home directory. Both files are loaded if they exist, the one in your Rails app will overwrite settings from your home.

Example .theine file (YAML):

base_port: 11000
max_port: 11100
min_free_workers: 2
spawn_parallel: true

base_port and max_port

The Theine server will use the base_port TCP port. Workers use base_port + 1 to max_port TCP ports. Use this setting if you need to run multiple Rails apps at the same time (use a different base_port for each).

min_free_workers

The minimum amount of workers that Theine should try to keep around. If you set this to 2 for example, once Theine has loaded 2 workers, you can execute two commands immediately. When you execute a third command, you will have to wait until Theine has spawned new workers. Theine will spawn a new worker as soon as you execute a command.

spawn_parallel

When set to `true`, Theine will start min_free_workers all at once. When set to `false`, it will start one worker first, and when it is loaded, it will start the next worker. When `false`, the first worker should start just a little bit faster. If you have a high number of min_free_workers then I recommend setting this to `false`.

Speed up Theine

If you are using RVM or rbenv, you can tell theine to use Ruby 1.9 or Ruby 2.0 to run the client, which will make the Theine client start much faster.

If you have any problems in this mode, please revert back to using the same Ruby for the client.

Tell Theine to use Ruby MRI for the client

Figure out where your Ruby 1.9 or 2.0 binary is:

$ rvm ruby-2.0.0 do which ruby
/Users/mrbrdo/.rvm/rubies/ruby-2.0.0-p247/bin/ruby

Then where you plan to use Theine, do:

$ rvm use jruby
$ theine_set_ruby /Users/mrbrdo/.rvm/rubies/ruby-2.0.0-p247/bin/ruby

Enjoy mega fast client:

time theine runner "puts 'hello world'"
0.12s user 0.02s system 30% cpu 0.470 total

JRuby settings

I recommend you pass these options to JRuby

-Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -Xcompile.mode=OFF

It will speed up the startup time noticably for all JRuby apps (especially Rails). Example RVM hook `~/.rvm/hooks/after_use_jruby_custom' (chmod a+x):

#!/usr/bin/env bash

\. "${rvm_path}/scripts/functions/hooks/jruby"

if [[ "${rvm_ruby_string}" =~ "jruby" ]]
then
  jruby_options_append "-Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -Xcompile.mode=OFF"
else
  jruby_options_remove "${PROJECT_JRUBY_OPTS[@]}"
  jruby_clean_project_options
fi

Using with Foreman

Theine works with Foreman:

theine_server: theine_server
server: rails server

If you have problems, try adding theine to your Gemfile. If you want to use the theine client in foreman, you should use theine_current_ruby because Foreman uses bundle exec. But there is no point in doing that, since theine needs to spawn a process for each command anyway, so there is no benefit in comparison to just running the command (like rails server) directly.

How it works

Theine's server spawns processes in the background that load your Rails application. When you run a command through theine, it will be executed in one of these pre-loaded processes. I used to do IO redirection (similarly to pry-remote) but it ended up being very unreliable, so now I am using screen to take care of this. After your command is done, the process will exit. When you run a new command, it will run in another pre-loaded process.

Theine will automatically spawn additional processes as needed.

The client (`theine` command) does not need to run on JRuby (or the same Ruby that you use in your Rails application), because it is only used to connect to the server, all the code is then actually executed on the server.