class Theine::Worker

Constants

COMMANDS

Attributes

balancer[R]
port[R]

Public Class Methods

new(port, balancer) click to toggle source
# File lib/theine/worker.rb, line 36
def initialize(port, balancer)
  @port = port
  @balancer = balancer
  @command_proc = proc { }
end

Public Instance Methods

pid() click to toggle source
# File lib/theine/worker.rb, line 63
def pid
  ::Process.pid
end
run() click to toggle source
# File lib/theine/worker.rb, line 42
def run
  boot
  begin
    DRb.thread.join
    screen_move_to_bottom
    sleep 0.1 while !screen_attached?

    puts "command: #{@command_name} #{argv_to_s}"
    instance_exec(&@command_proc)
  ensure
    balancer.worker_done(port)
  end
end
screen_attached?() click to toggle source
# File lib/theine/worker.rb, line 71
def screen_attached?
  !system("screen -ls | grep theine#{@port} | grep Detached > /dev/null")
end
screen_move_to_bottom() click to toggle source
# File lib/theine/worker.rb, line 75
def screen_move_to_bottom
  puts "\033[22B"
end
stop!() click to toggle source
# File lib/theine/worker.rb, line 67
def stop!
  exit(1)
end

Private Instance Methods

argv_to_s() click to toggle source
# File lib/theine/worker.rb, line 103
def argv_to_s
  ARGV.map { |arg|
    if arg.include?(" ")
      "\"#{arg}\""
    else
      arg
    end
  }.join(' ')
end
boot() click to toggle source
# File lib/theine/worker.rb, line 127
def boot
  balancer.set_worker_pid(port, pid)

  require "#{RAILS_ROOT_PATH}/config/boot"
  require "#{RAILS_ROOT_PATH}/config/environment"
  start_service

  balancer.worker_boot(port)
end
change_rails_env_to(env) click to toggle source
# File lib/theine/worker.rb, line 87
def change_rails_env_to(env)
  ENV['RAILS_ENV'] = env
  ENV['RACK_ENV'] = env
  if defined? ::Rails
    ::Rails.env = env

    # load config/environments/test.rb
    test_env_rb = ::Rails.root.join("config/environments/#{env}.rb")
    load(test_env_rb) if File.exist?(test_env_rb)

    if defined? ActiveRecord
      ActiveRecord::Base.establish_connection rescue nil
    end
  end
end
rails_reload!() click to toggle source
# File lib/theine/worker.rb, line 118
def rails_reload!
  ActionDispatch::Reloader.cleanup!
  ActionDispatch::Reloader.prepare!
end
set_argv(argv) click to toggle source
# File lib/theine/worker.rb, line 113
def set_argv(argv)
  ARGV.clear
  ARGV.concat(argv)
end
set_command(command_name, &block) click to toggle source
# File lib/theine/worker.rb, line 80
def set_command(command_name, &block)
  rails_reload!
  @command_name = command_name
  @command_proc = block
  DRb.stop_service
end
start_service() click to toggle source
# File lib/theine/worker.rb, line 123
def start_service
  DRb.start_service("druby://localhost:#{@port}", self)
end