Class: Raykit::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



12
13
14
15
16
17
18
# File 'lib/raykit/command.rb', line 12

def initialize()
    @timeout=0
    @directory = Dir.pwd
    @output = ''
    @error = ''
    @exitstatus = 0
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



10
11
12
# File 'lib/raykit/command.rb', line 10

def command
  @command
end

#directoryObject

Returns the value of attribute directory.



9
10
11
# File 'lib/raykit/command.rb', line 9

def directory
  @directory
end

#elapsedObject

Returns the value of attribute elapsed.



9
10
11
# File 'lib/raykit/command.rb', line 9

def elapsed
  @elapsed
end

#errorObject

Returns the value of attribute error.



10
11
12
# File 'lib/raykit/command.rb', line 10

def error
  @error
end

#exitstatusObject

Returns the value of attribute exitstatus.



10
11
12
# File 'lib/raykit/command.rb', line 10

def exitstatus
  @exitstatus
end

#outputObject

Returns the value of attribute output.



10
11
12
# File 'lib/raykit/command.rb', line 10

def output
  @output
end

#start_timeObject

Returns the value of attribute start_time.



9
10
11
# File 'lib/raykit/command.rb', line 9

def start_time
  @start_time
end

#timeoutObject

Returns the value of attribute timeout.



9
10
11
# File 'lib/raykit/command.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/raykit/command.rb', line 20

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_hashObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/raykit/command.rb', line 64

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