Class: Raykit::Command
- Inherits:
-
Object
- Object
- Raykit::Command
- Defined in:
- lib/raykit/command.rb
Overview
Functionality to support executing and logging system commands
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#directory ⇒ Object
The working directory, defaults the current directory if not specified.
-
#elapsed ⇒ Object
The execution time in seconds.
-
#error ⇒ Object
Returns the value of attribute error.
-
#exitstatus ⇒ Object
Returns the value of attribute exitstatus.
-
#output ⇒ Object
Returns the value of attribute output.
-
#start_time ⇒ Object
The start time.
-
#timeout ⇒ Object
The timeout in seconds, defaults to 0 if not specified.
Class Method Summary collapse
Instance Method Summary collapse
- #from_hash(hash) ⇒ Object
- #init_defaults ⇒ Object
-
#initialize(command, timeout = 0) ⇒ Command
constructor
A new instance of Command.
- #run ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(command, timeout = 0) ⇒ Command
Returns a new instance of Command.
28 29 30 31 32 33 34 35 36 |
# File 'lib/raykit/command.rb', line 28 def initialize(command,timeout=0) init_defaults @command=command @timeout=timeout if(@command.length > 0) run end self end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
18 19 20 |
# File 'lib/raykit/command.rb', line 18 def command @command end |
#directory ⇒ Object
The working directory, defaults the current directory if not specified
13 14 15 |
# File 'lib/raykit/command.rb', line 13 def directory @directory end |
#elapsed ⇒ Object
The execution time in seconds
17 18 19 |
# File 'lib/raykit/command.rb', line 17 def elapsed @elapsed end |
#error ⇒ Object
Returns the value of attribute error.
18 19 20 |
# File 'lib/raykit/command.rb', line 18 def error @error end |
#exitstatus ⇒ Object
Returns the value of attribute exitstatus.
18 19 20 |
# File 'lib/raykit/command.rb', line 18 def exitstatus @exitstatus end |
#output ⇒ Object
Returns the value of attribute output.
18 19 20 |
# File 'lib/raykit/command.rb', line 18 def output @output end |
#start_time ⇒ Object
The start time
15 16 17 |
# File 'lib/raykit/command.rb', line 15 def start_time @start_time end |
#timeout ⇒ Object
The timeout in seconds, defaults to 0 if not specified
11 12 13 |
# File 'lib/raykit/command.rb', line 11 def timeout @timeout end |
Class Method Details
.parse(json) ⇒ Object
106 107 108 109 110 |
# File 'lib/raykit/command.rb', line 106 def self.parse(json) cmd=Command.new('') cmd.from_hash(JSON.parse(json)) cmd end |
Instance Method Details
#from_hash(hash) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/raykit/command.rb', line 95 def from_hash(hash) @command = hash["command"] @directory=hash["directory"] @timeout=hash["timeout"] @start_time=hash["start_time"] @elapsed=hash["elapsed"] @output=hash["output"] @error=hash["error"] @exitstatus=hash["exitstatus"] end |
#init_defaults ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/raykit/command.rb', line 20 def init_defaults @timeout=0 @directory = Dir.pwd @output = '' @error = '' @exitstatus = 0 end |
#run ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/raykit/command.rb', line 38 def run() @start_time = Time.now timer = Timer.new if(@timeout == 0) @output,@error,process_status = Open3.capture3(@command) @exitstatus=process_status.exitstatus else Open3.popen3(@command, :chdir=>@directory) { |stdin,stdout,stderr,thread| tick=2 pid = thread.pid start = Time.now elapsed = Time.now-start while (elapsed) < @timeout and thread.alive? Kernel.select([stdout,stderr], nil, nil, tick) begin @output << stdout.read_nonblock(BUFFER_SIZE) @error << stderr.read_nonblock(BUFFER_SIZE) rescue IO::WaitReadable rescue EOFError @exitstatus=thread.value.exitstatus break end elapsed = Time.now-start end if thread.alive? if(Gem.win_platform?) `taskkill /f /pid #{pid}` else Process.kill("TERM", pid) end @exitstatus=5 else @exitstatus=thread.value.exitstatus end } end @elapsed = timer.elapsed if(@exitstatus == 0) LOG.log('Raykit.Command',Logger::Severity::INFO,JSON.generate(to_hash)) else LOG.log('Raykit.Command',Logger::Severity::ERROR,JSON.generate(to_hash)) end end |
#to_hash ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/raykit/command.rb', line 82 def to_hash() hash = Hash.new hash[:command] = @command hash[:directory] = @directory hash[:timeout] = @timeout hash[:start_time] = @start_time hash[:elapsed] = @elapsed hash[:output] = @output hash[:error] = @error hash[:exitstatus] = @exitstatus hash end |