Class: Wukong::Local::StdioDriver
- Inherits:
-
EM::P::LineAndTextProtocol
- Object
- EM::P::LineAndTextProtocol
- Wukong::Local::StdioDriver
- 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
Instance Attribute Summary
Attributes included from DriverMethods
Class Method Summary collapse
-
.add_signal_traps ⇒ Object
Adds signal traps for SIGINT and SIGTERM to Ensure we capture C-c and friends, stop the EventMachine reactor, &c.
-
.start(label, settings = {}) ⇒ Object
Start a new StdioDriver.
Instance Method Summary collapse
-
#initialize(label, settings) ⇒ StdioDriver
constructor
:nodoc:.
-
#post_init ⇒ Object
Called by EventMachine framework after successfully attaching to $stdin.
-
#process(record) ⇒ Object
Writes a record to $stdout.
-
#receive_line(line) ⇒ Object
Called by EventMachine framework after successfully reading a line from $stdin.
-
#setup ⇒ Object
Ensures that $stdout is synced.
-
#unbind ⇒ Object
Called by EventMachine framework after EOF from $stdin.
Methods included from Wukong::Logging
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_traps ⇒ Object
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.
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_init ⇒ Object
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.
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.
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.) end |
#setup ⇒ Object
Ensures that $stdout is synced.
31 32 33 |
# File 'lib/wukong/local/stdio_driver.rb', line 31 def setup() $stdout.sync end |
#unbind ⇒ Object
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 |