Class: Raykit::Command
- Inherits:
-
Object
- Object
- Raykit::Command
- Defined in:
- lib/raykit/command.rb
Constant Summary collapse
- @@enable_logging =
true- @@logger =
nil
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#elapsed ⇒ Object
Returns the value of attribute elapsed.
-
#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
Returns the value of attribute start_time.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #run ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
41 42 43 44 45 46 47 |
# File 'lib/raykit/command.rb', line 41 def initialize() @timeout=0 @directory = Dir.pwd @output = '' @error = '' @exitstatus = 0 end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
39 40 41 |
# File 'lib/raykit/command.rb', line 39 def command @command end |
#directory ⇒ Object
Returns the value of attribute directory.
38 39 40 |
# File 'lib/raykit/command.rb', line 38 def directory @directory end |
#elapsed ⇒ Object
Returns the value of attribute elapsed.
38 39 40 |
# File 'lib/raykit/command.rb', line 38 def elapsed @elapsed end |
#error ⇒ Object
Returns the value of attribute error.
39 40 41 |
# File 'lib/raykit/command.rb', line 39 def error @error end |
#exitstatus ⇒ Object
Returns the value of attribute exitstatus.
39 40 41 |
# File 'lib/raykit/command.rb', line 39 def exitstatus @exitstatus end |
#output ⇒ Object
Returns the value of attribute output.
39 40 41 |
# File 'lib/raykit/command.rb', line 39 def output @output end |
#start_time ⇒ Object
Returns the value of attribute start_time.
38 39 40 |
# File 'lib/raykit/command.rb', line 38 def start_time @start_time end |
#timeout ⇒ Object
Returns the value of attribute timeout.
38 39 40 |
# File 'lib/raykit/command.rb', line 38 def timeout @timeout end |
Class Method Details
.enable_logging ⇒ Object
11 12 13 |
# File 'lib/raykit/command.rb', line 11 def self.enable_logging @@enable_logging end |
.enable_logging=(enable) ⇒ Object
14 15 16 |
# File 'lib/raykit/command.rb', line 14 def self.enable_logging=(enable) @@enable_logging = enable end |
.log(command) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/raykit/command.rb', line 18 def self.log(command) if(@@enable_logging) if(@@logger.nil?) log_dir = "#{Raykit::Environment.log_dir}/Raykit/Command" if(!Dir.exist?(log_dir)) FileUtils.mkdir_p(log_dir) end Dir.chdir(log_dir) do # start the log over whenever the log exceeds 100 megabytes in size @@logger = Logger.new('log.txt',0,100*1024*1024) end end if(command.exitstatus == 0) @@logger.info JSON.generate(command.to_hash) else @@logger.error JSON.generate(command.to_hash) end end end |
Instance Method Details
#run ⇒ Object
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 81 82 83 84 85 86 87 |
# File 'lib/raykit/command.rb', line 49 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 Command::log(self) end |
#to_hash ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/raykit/command.rb', line 89 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 |