Class: Jefe::EM
Defined Under Namespace
Classes: ProcessHandler
Instance Method Summary collapse
- #add(name, command) ⇒ Object
-
#initialize(printer) ⇒ EM
constructor
A new instance of EM.
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Emitter
#callbacks, #emit, included, #on
Constructor Details
#initialize(printer) ⇒ EM
Returns a new instance of EM.
9 10 11 12 13 14 |
# File 'lib/jefe/em.rb', line 9 def initialize(printer) @printer = printer @fsm = MicroMachine.new(:started).tap do |m| m.when(:stop, :started => :stopped) end end |
Instance Method Details
#add(name, command) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jefe/em.rb', line 31 def add name, command @printer.out name, "starting #{command}" PTY.spawn(command) do |output, input, pid| input.close @printer.out(name, "started with pid #{pid}") c = EM.attach output, ProcessHandler m = MicroMachine.new(:started).tap do |m| m.when(:stop, :started => :stopped) end c.on(:data) do |data| @printer.out(name, data) end c.on(:unbind) do @printer.out(name, "process terminated") m.trigger(:stop) self.stop end self.on(:stop) do if m.trigger(:stop) @printer.out("system", "killing #{name} in #{pid}") Process.kill("INT", pid) end end end end |
#start ⇒ Object
16 17 18 19 20 21 |
# File 'lib/jefe/em.rb', line 16 def start @printer.out "system", "starting" EM.run do yield self end end |
#stop ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/jefe/em.rb', line 23 def stop if @fsm.trigger(:stop) @printer.out "system", "stopping" emit(:stop) EM.stop end end |