Module: ProcTools

Defined in:
lib/proctools.rb

Class Method Summary collapse

Class Method Details

.execSync(aCmd, aTimeout = 0, doKill = true) ⇒ Object

Executes an external command



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
# File 'lib/proctools.rb', line 33

def self.execSync aCmd, aTimeout = 0, doKill = true
    puts "Spawning process #{aCmd}"
    pid = fork {
        exec aCmd
    }
    puts "Waiting on process pid=#{pid} timeout = #{aTimeout}..."
    exitcode = nil
    if aTimeout>0
        begin
            timeout(aTimeout){
                return_pid, exitcode = Process.wait2
                puts "Process #{pid} finished exitcode: #{exitcode}, cmd: #{aCmd}"
            }
        rescue Timeout::Error
            puts "Process #{pid} timeuot"
            if doKill
                puts "Killing-9 the process #{pid}  - #{aCmd}"
                Process.kill 9, pid
            end
        end
    else
        return_pid, exitcode  = Process.wait2
        puts "Process #{pid} finished exitcode: #{exitcode}, cmd: #{aCmd}"
        STDOUT.flush
    end
    exitcode
end

.existsPath?(aPath) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/proctools.rb', line 83

def self.existsPath? aPath
    File.exist? File.dirname(aPath)
end

.Intern(aHash) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/proctools.rb', line 87

def self.Intern(aHash)
    return {} unless aHash
    
    hash={}
    aHash.each_pair do
        |n,v| 
        v=Intern(v) if v.kind_of? Hash
        hash[n.to_sym]=v
    end
    hash
end

.withPidFile(aFilename = nil, opts = {}, &code) ⇒ Object

Creates a pid file while running a block Pid file name is teken from ENV or aFilename



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/proctools.rb', line 63

def self.withPidFile aFilename = nil, opts={}, &code
    # should we use pid file at all?
    if opts[:nopid]
        yield
    else
        pid_file = ENV['PID_FILE'] ||   aFilename
        raise "Process may already run PID-file exists (#{pid_file}). Use :forcepid to run anyway" if File.exist?(pid_file) and not opts[:forcepid]
        File.open(pid_file, 'w') {|f| f.puts $$}
        begin            
            yield
        rescue StandardError=>e
            #rethrow
            raise e
            #puts e, e.backtrace
        ensure
            File.delete(pid_file) if pid_file
        end            
    end
end