Class: Headless::CliUtil
- Inherits:
-
Object
- Object
- Headless::CliUtil
- Defined in:
- lib/headless/cli_util.rb
Class Method Summary collapse
- .application_exists?(app) ⇒ Boolean
- .ensure_application_exists!(app, error_message) ⇒ Object
- .fork_process(command, pid_filename, log_filename = '/dev/null') ⇒ Object
- .kill_process(pid_filename, options = {}) ⇒ Object
-
.path_to(app) ⇒ Object
Credit: stackoverflow.com/a/5471032/6678.
- .process_mine?(pid) ⇒ Boolean
- .process_running?(pid) ⇒ Boolean
- .read_pid(pid_filename) ⇒ Object
Class Method Details
.application_exists?(app) ⇒ Boolean
3 4 5 |
# File 'lib/headless/cli_util.rb', line 3 def self.application_exists?(app) !!path_to(app) end |
.ensure_application_exists!(app, error_message) ⇒ Object
7 8 9 10 11 |
# File 'lib/headless/cli_util.rb', line 7 def self.ensure_application_exists!(app, ) if !self.application_exists?(app) raise Headless::Exception.new() end end |
.fork_process(command, pid_filename, log_filename = '/dev/null') ⇒ Object
42 43 44 45 46 47 |
# File 'lib/headless/cli_util.rb', line 42 def self.fork_process(command, pid_filename, log_filename='/dev/null') pid = Process.spawn(command, err: log_filename) File.open pid_filename, 'w' do |f| f.puts pid end end |
.kill_process(pid_filename, options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/headless/cli_util.rb', line 49 def self.kill_process(pid_filename, ={}) if pid = read_pid(pid_filename) begin Process.kill 'TERM', pid Process.wait pid if [:wait] rescue Errno::ESRCH # no such process; assume it's already killed rescue Errno::ECHILD # Process.wait tried to wait on a dead process end end unless [:preserve_pid_file] begin FileUtils.rm pid_filename rescue Errno::ENOENT # pid file already removed end end end |
.path_to(app) ⇒ Object
Credit: stackoverflow.com/a/5471032/6678
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/headless/cli_util.rb', line 14 def self.path_to(app) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each { |ext| exe = File.join(path, "#{app}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) } end return nil end |
.process_mine?(pid) ⇒ Boolean
25 26 27 28 29 |
# File 'lib/headless/cli_util.rb', line 25 def self.process_mine?(pid) Process.kill(0, pid) && true rescue Errno::EPERM, Errno::ESRCH false end |
.process_running?(pid) ⇒ Boolean
31 32 33 34 35 |
# File 'lib/headless/cli_util.rb', line 31 def self.process_running?(pid) Process.getpgid(pid) && true rescue Errno::ESRCH false end |
.read_pid(pid_filename) ⇒ Object
37 38 39 40 |
# File 'lib/headless/cli_util.rb', line 37 def self.read_pid(pid_filename) pid = (File.read(pid_filename) rescue "").strip pid.empty? ? nil : pid.to_i end |