Class: Wukong::Local::StdioDriver

Inherits:
EM::P::LineAndTextProtocol
  • Object
show all
Includes:
DriverMethods, Wukong::Logging
Defined in:
lib/wukong/local/stdio_driver.rb

Overview

A class for driving processors over the STDIN/STDOUT protocol.

Relies on EventMachine's LineAndTextProtocol.

Direct Known Subclasses

Source::SourceDriver

Instance Attribute Summary

Attributes included from DriverMethods

#dataflow, #label, #settings

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Wukong::Logging

included

Methods included from DriverMethods

#construct_dataflow, #finalize, #finalize_and_stop_dataflow, #finalize_dataflow, #send_through_dataflow, #setup_dataflow, #stop

Constructor Details

#initialize(label, settings) ⇒ StdioDriver

:nodoc:



25
26
27
28
# File 'lib/wukong/local/stdio_driver.rb', line 25

def initialize(label, settings)
  super
  construct_dataflow(label, settings)
end

Class Method Details

.add_signal_trapsObject

Adds signal traps for SIGINT and SIGTERM to Ensure we capture C-c and friends, stop the EventMachine reactor, &c.



37
38
39
40
# File 'lib/wukong/local/stdio_driver.rb', line 37

def self.add_signal_traps
  Signal.trap('INT')  { log.info 'Received SIGINT. Stopping.'  ; EM.stop }
  Signal.trap('TERM') { log.info 'Received SIGTERM. Stopping.' ; EM.stop }
end

.start(label, settings = {}) ⇒ Object

Start a new StdioDriver.

Parameters:

  • the (Symbol)

    name of the processor or dataflow to drive

  • settings (Configliere::Param) (defaults to: {})

    the settings to use



20
21
22
# File 'lib/wukong/local/stdio_driver.rb', line 20

def self.start(label, settings = {})
  EM.attach($stdin, self, label, settings)
end

Instance Method Details

#post_initObject

Called by EventMachine framework after successfully attaching to $stdin.

Adds signal handlers and calls the #setup_dataflow method.



46
47
48
49
# File 'lib/wukong/local/stdio_driver.rb', line 46

def post_init      
  self.class.add_signal_traps
  setup_dataflow
end

#process(record) ⇒ Object

Writes a record to $stdout.

Parameters:

  • record (#to_s)


86
87
88
# File 'lib/wukong/local/stdio_driver.rb', line 86

def process(record)
  $stdout.puts record
end

#receive_line(line) ⇒ Object

Called by EventMachine framework after successfully reading a line from $stdin.

Parameters:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wukong/local/stdio_driver.rb', line 59

def receive_line line
  send_through_dataflow(line)
rescue => e
  error = Wukong::Error.new(e)
  # EM.stop
  
  # We'd like to *raise* `error` here and have it be handled by
  # Wukong::Runner.run but we are fighting with EventMachine.run
  # which executes in the middle.
  #
  # It seems no matter what we do, EventMachine.run will swallow
  # any Exception raised here (including SystemExit) and exit
  # the Ruby process with a return code of 0.
  #
  # Instead we just log the message that *would* have gotten
  # logged by Wukong::Runner.run and leave it to EventMachine to
  # exit very unnaturally.
  log.error(error.message)
end

#setupObject

Ensures that $stdout is synced.



31
32
33
# File 'lib/wukong/local/stdio_driver.rb', line 31

def setup()
  $stdout.sync
end

#unbindObject

Called by EventMachine framework after EOF from $stdin.

Calls #finalize_and_stop_dataflow method and stops the EventMachine reactor.



98
99
100
101
# File 'lib/wukong/local/stdio_driver.rb', line 98

def unbind
  finalize_and_stop_dataflow
  EM.stop
end