Class: Theine::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/theine/worker.rb

Constant Summary collapse

COMMANDS =
{
  rails: proc {
    require 'rails/commands'
  },
  rake: proc {
    ::Rails.application.load_tasks
    ARGV.each do |task|
      ::Rake::Task[task].invoke
    end
  },
  rspec: proc {
    require 'rspec/core'
    RSpec::Core::Runner.autorun
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, balancer) ⇒ Worker

Returns a new instance of Worker.



25
26
27
28
29
# File 'lib/theine/worker.rb', line 25

def initialize(port, balancer)
  @port = port
  @balancer = balancer
  @command_proc = proc { }
end

Instance Attribute Details

#balancerObject (readonly)

Returns the value of attribute balancer.



7
8
9
# File 'lib/theine/worker.rb', line 7

def balancer
  @balancer
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/theine/worker.rb', line 7

def port
  @port
end

Instance Method Details

#pidObject



52
53
54
# File 'lib/theine/worker.rb', line 52

def pid
  ::Process.pid
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/theine/worker.rb', line 31

def run
  boot
  begin
    DRb.thread.join
    screen_move_to_bottom
    sleep 0.1 while !screen_attached?

    puts "command: #{@command_name} #{argv_to_s}"
    @command_proc.call
  ensure
    balancer.worker_done(port)
  end
end

#screen_attached?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/theine/worker.rb', line 60

def screen_attached?
  !system("screen -ls | grep theine#{@port} | grep Detached > /dev/null")
end

#screen_move_to_bottomObject



64
65
66
# File 'lib/theine/worker.rb', line 64

def screen_move_to_bottom
  puts "\033[22B"
end

#stop!Object



56
57
58
# File 'lib/theine/worker.rb', line 56

def stop!
  exit(1)
end