Module: Eye::Process::System

Included in:
ChildProcess, Eye::Process
Defined in:
lib/eye/process/system.rb

Instance Method Summary collapse

Instance Method Details

#clear_pid_fileObject


29
30
31
32
33
34
35
# File 'lib/eye/process/system.rb', line 29

def clear_pid_file
  info "delete pid_file: #{self[:pid_file_ex]}"
  File.unlink(self[:pid_file_ex])
  true
rescue
  nil
end

#execute(cmd, cfg = {}) ⇒ Object


75
76
77
# File 'lib/eye/process/system.rb', line 75

def execute(cmd, cfg = {})
  defer{ Eye::System::execute cmd, cfg }
end

#execute_async(cmd, opts = {}) ⇒ Object


85
86
87
88
89
# File 'lib/eye/process/system.rb', line 85

def execute_async(cmd, opts = {})
  res = Eye::System.daemonize(cmd, self.config.merge(opts))
  info "execute_async `#{cmd}` with res: #{res}"
  res
end

#execute_sync(cmd, opts = {:timeout => 1.second}) ⇒ Object


79
80
81
82
83
# File 'lib/eye/process/system.rb', line 79

def execute_sync(cmd, opts = {:timeout => 1.second})
  res = execute cmd, self.config.merge(opts)
  info "execute_sync `#{cmd}` with res: #{res}"
  res
end

#expand_path(path) ⇒ Object


111
112
113
# File 'lib/eye/process/system.rb', line 111

def expand_path(path)
  File.expand_path(path, self[:working_dir])
end

#failsafe_load_pidObject


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/eye/process/system.rb', line 91

def failsafe_load_pid
  pid = load_pid_from_file

  if !pid
    # this is can be symlink changed case
    sleep 0.1
    pid = load_pid_from_file
  end

  pid
end

#failsafe_save_pidObject


103
104
105
106
107
108
109
# File 'lib/eye/process/system.rb', line 103

def failsafe_save_pid
  save_pid_to_file
  true
rescue => ex
  log_ex(ex)
  false
end

#load_pid_from_fileObject


5
6
7
8
9
10
11
12
# File 'lib/eye/process/system.rb', line 5

def load_pid_from_file
  res = if File.exist?(self[:pid_file_ex])
    _pid = File.read(self[:pid_file_ex]).to_i
    _pid > 0 ? _pid : nil
  end

  res
end

#pid_file_ctimeObject


37
38
39
# File 'lib/eye/process/system.rb', line 37

def pid_file_ctime
  File.ctime(self[:pid_file_ex]) rescue Time.now
end

#process_pid_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
48
49
# File 'lib/eye/process/system.rb', line 45

def process_pid_running?(pid)
  res = Eye::System.check_pid_alive(pid)
  debug { "process_really_running?: <#{pid}> #{res.inspect}" }
  !!res[:result]
end

#process_really_running?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/eye/process/system.rb', line 41

def process_really_running?
  process_pid_running?(self.pid)
end

#save_pid_to_fileObject


18
19
20
21
22
23
24
25
26
27
# File 'lib/eye/process/system.rb', line 18

def save_pid_to_file
  if self.pid
    File.open(self[:pid_file_ex], 'w') do |f|
      f.write self.pid
    end
    true
  else
    false
  end
end

#send_signal(code) ⇒ Object


51
52
53
54
55
56
57
58
59
# File 'lib/eye/process/system.rb', line 51

def send_signal(code)
  res = Eye::System.send_signal(self.pid, code)

  msg = "send_signal #{code} to <#{self.pid}>"
  msg += ", error<#{res[:error]}>" if res[:error]
  info msg

  res[:result] == :ok
end

#set_pid_from_fileObject


14
15
16
# File 'lib/eye/process/system.rb', line 14

def set_pid_from_file
  self.pid = load_pid_from_file
end

#wait_for_condition(timeout, step = 0.1, &block) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/eye/process/system.rb', line 61

def wait_for_condition(timeout, step = 0.1, &block)
  res = nil
  sumtime = 0

  loop do
    tm = Time.now
    res = yield # note that yield can block actor here and timeout can be overhead
    return res if res
    sleep step.to_f
    sumtime += (Time.now - tm)
    return false if sumtime > timeout
  end
end