Module: Autumn::Tool::Bin::Helpers

Included in:
Cmd
Defined in:
lib/autumn/tool/bin.rb

Overview

Helper methods {{{

Instance Method Summary collapse

Instance Method Details

#check_running?(pid_file) ⇒ Boolean

}}}

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/autumn/tool/bin.rb', line 45

def check_running?(pid_file) # {{{
  return false unless File.file?(pid_file)
  is_running?(File.read(pid_file).to_i)
end

#default_pidfileObject

{{{



11
12
13
14
15
16
# File 'lib/autumn/tool/bin.rb', line 11

def default_pidfile # {{{
  return @default_pidfile if @default_pidfile
  al_root
  pn = Pathname.new(AL_ROOT)
  @default_pidfile = (pn.expand_path.join("tmp", pn.basename.to_s + ".pid")).strip
end

#find_pid(pid_file) ⇒ Object

}}}



50
51
52
53
54
55
56
57
58
59
# File 'lib/autumn/tool/bin.rb', line 50

def find_pid(pid_file) # {{{
  if pid_file.nil? or not File.file?(pid_file)
    pid_file = default_pidfile
  end
  unless File.file?(pid_file)
    $stderr.puts "Could not find running process id."
    return false
  end
  pid_file
end

#is_running?(pid) ⇒ Boolean

}}}

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/autumn/tool/bin.rb', line 29

def is_running?(pid) # {{{
  if is_windows?
    wmi = WIN32OLE.connect("winmgmts://")
    processes, ours = wmi.ExecQuery("select * from win32_process where ProcessId = #{pid}"), []
    processes.each { |process| ours << process.Name }
    ours.first.nil?
  else
    begin
      prio = Process.getpriority(Process::PRIO_PROCESS, pid)
      true
    rescue Errno::ESRCH
      false
    end
  end
end

#is_windows?Boolean

We’re really only concerned about win32ole, so we focus our check on its ability to load that

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/autumn/tool/bin.rb', line 20

def is_windows?  # {{{
  return @is_win if @is_win
  begin
    require "win32ole"
  rescue LoadError
  end
  @is_win ||= Object.const_defined?("WIN32OLE")
end