Class: MongoOplogBackup::Command
- Inherits:
-
Object
- Object
- MongoOplogBackup::Command
- Defined in:
- lib/mongo_oplog_backup/command.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#standard_error ⇒ Object
readonly
Returns the value of attribute standard_error.
-
#standard_output ⇒ Object
readonly
Returns the value of attribute standard_output.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command, options = {}) ⇒ Command
constructor
command must be an array containing the command and arguments.
- #log_output(logger) ⇒ Object
- #on_stderr(&block) ⇒ Object
- #on_stderr_line(&block) ⇒ Object
- #on_stdout(&block) ⇒ Object
- #on_stdout_line(&block) ⇒ Object
- #raise! ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(command, options = {}) ⇒ Command
command must be an array containing the command and arguments
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongo_oplog_backup/command.rb', line 25 def initialize(command, ={}) @command = command @standard_output = '' @standard_error = '' @out_blocks = [] @err_blocks = [] logger = [:logger] || Command.logger if logger log_output(logger) end on_stdout do |data| @standard_output << data end on_stderr do |data| @standard_error << data end end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
15 16 17 |
# File 'lib/mongo_oplog_backup/command.rb', line 15 def command @command end |
#standard_error ⇒ Object (readonly)
Returns the value of attribute standard_error.
17 18 19 |
# File 'lib/mongo_oplog_backup/command.rb', line 17 def standard_error @standard_error end |
#standard_output ⇒ Object (readonly)
Returns the value of attribute standard_output.
16 17 18 |
# File 'lib/mongo_oplog_backup/command.rb', line 16 def standard_output @standard_output end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
18 19 20 |
# File 'lib/mongo_oplog_backup/command.rb', line 18 def status @status end |
Class Method Details
.execute(command, options = {}) ⇒ Object
20 21 22 |
# File 'lib/mongo_oplog_backup/command.rb', line 20 def self.execute(command, ={}) Command.new(command, ).run end |
.logger ⇒ Object
11 12 13 |
# File 'lib/mongo_oplog_backup/command.rb', line 11 def self.logger @logger end |
.logger=(logger) ⇒ Object
7 8 9 |
# File 'lib/mongo_oplog_backup/command.rb', line 7 def self.logger= logger @logger = logger end |
Instance Method Details
#log_output(logger) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/mongo_oplog_backup/command.rb', line 62 def log_output(logger) on_stdout_line do |line| logger.debug(line) end on_stderr_line do |line| logger.error(line) end end |
#on_stderr(&block) ⇒ Object
54 55 56 |
# File 'lib/mongo_oplog_backup/command.rb', line 54 def on_stderr &block @err_blocks << block end |
#on_stderr_line(&block) ⇒ Object
50 51 52 |
# File 'lib/mongo_oplog_backup/command.rb', line 50 def on_stderr_line &block on_stderr(&lines_proc(&block)) end |
#on_stdout(&block) ⇒ Object
58 59 60 |
# File 'lib/mongo_oplog_backup/command.rb', line 58 def on_stdout &block @out_blocks << block end |
#on_stdout_line(&block) ⇒ Object
46 47 48 |
# File 'lib/mongo_oplog_backup/command.rb', line 46 def on_stdout_line &block on_stdout(&lines_proc(&block)) end |
#raise! ⇒ Object
103 104 105 106 107 108 |
# File 'lib/mongo_oplog_backup/command.rb', line 103 def raise! unless status.success? raise "Command failed with exit code #{status.exitstatus}" end self end |
#run ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mongo_oplog_backup/command.rb', line 71 def run @status = Open3.popen3(*command) do |stdin, stdout, stderr, wait_thr| stdin.close_write # until all_eof([stdout, stderr]) still_open = [stdout, stderr] until still_open.empty? handles = IO.select(still_open, nil, nil, 0.001) unless handles.nil? read_available_data(stdout) do |data| @out_blocks.each do |block| block.call(data) end end if handles[0].include?(stdout) read_available_data(stderr) do |data| @err_blocks.each do |block| block.call(data) end end if handles[0].include?(stderr) end still_open.delete_if { |s| s.closed? } sleep 0.001 end wait_thr.value end raise! self end |