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 55
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 82
def pid
  ::Process.pid
end
run() click to toggle source
# File lib/theine/worker.rb, line 61
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 90
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 94
def screen_move_to_bottom
  puts "\033[22B"
end
stop!() click to toggle source
# File lib/theine/worker.rb, line 86
def stop!
  exit(1)
end

Private Instance Methods

argv_to_s() click to toggle source
# File lib/theine/worker.rb, line 126
def argv_to_s
  ARGV.map { |arg|
    if !arg.nil? && arg.include?(" ")
      "\"#{arg}\""
    else
      arg
    end
  }.join(' ')
end
boot() click to toggle source
# File lib/theine/worker.rb, line 154
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 106
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

    if defined? SequelRails
      Sequel::Model.db = SequelRails.setup env
    end
  end
end
rails_reload!() click to toggle source
# File lib/theine/worker.rb, line 141
def rails_reload!
  if defined? ActiveSupport::Reloader
    Rails.application.reloader.reload!
  else
    ActionDispatch::Reloader.cleanup!
    ActionDispatch::Reloader.prepare!
  end 
end
set_argv(argv) click to toggle source
# File lib/theine/worker.rb, line 136
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 99
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 150
def start_service
  DRb.start_service("druby://localhost:#{@port}", self)
end