Class: Preforker::Worker

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

Defined Under Namespace

Classes: WorkerAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_block, master) ⇒ Worker

Returns a new instance of Worker.



37
38
39
40
41
# File 'lib/preforker/worker.rb', line 37

def initialize(worker_block, master)
  @worker_block = worker_block
  @master = master
  @tmp = Util.tmpio
end

Instance Attribute Details

#pidObject

Returns the value of attribute pid.



35
36
37
# File 'lib/preforker/worker.rb', line 35

def pid
  @pid
end

#tmpObject (readonly)

Returns the value of attribute tmp.



34
35
36
# File 'lib/preforker/worker.rb', line 34

def tmp
  @tmp
end

Instance Method Details

#handle_signalsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/preforker/worker.rb', line 62

def handle_signals
  [:TERM, :INT].each do |sig|
    trap(sig) { exit!(0) }
  end

  [:EXIT, :QUIT].each do |sig|
     trap(sig){ @master_api.kill }
  end

  #what usr1 does
  trap(:USR1) { @read_pipe.close rescue nil }
  trap(:CHLD, 'DEFAULT')
end

#initObject



55
56
57
58
59
60
# File 'lib/preforker/worker.rb', line 55

def init
  init_self_pipe!
  handle_signals
  tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
  @master_api = WorkerAPI.new(self, @master)
end

#init_self_pipe!Object



43
44
45
46
# File 'lib/preforker/worker.rb', line 43

def init_self_pipe!
  @read_pipe, @write_pipe = IO.pipe
  [@read_pipe, @write_pipe].each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
end

#log(message) ⇒ Object



81
82
83
84
# File 'lib/preforker/worker.rb', line 81

def log(message)
  full_message = proc_message(message)
  @master.logger.info full_message
end

#proc_message(message) ⇒ Object



76
77
78
79
# File 'lib/preforker/worker.rb', line 76

def proc_message(message)
  full_message = ["#{@master.app_name} Child #{$$}", message].join(": ")
  $0 = full_message
end

#workObject



48
49
50
51
52
53
# File 'lib/preforker/worker.rb', line 48

def work
  log "Created"
  init

  @worker_block.call(@master_api) if @master_api.wants_me_alive?
end