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_file(check_content = false) ⇒ Object


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

def clear_pid_file(check_content = false)
  return if check_content && self.pid && load_pid_from_file != self.pid
  info "delete pid_file: #{self[:pid_file_ex]}"
  File.unlink(self[:pid_file_ex])
  true
rescue
  nil
end

#compare_identity(pid = self.pid) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eye/process/system.rb', line 39

def compare_identity(pid = self.pid)
  return :ok unless self[:check_identity]
  return :no_pid unless pid
  id = get_identity
  return :no_pid_file unless id
  st = Eye::SystemResources.start_time(pid)
  return :no_start_time unless st
  st1 = st.to_i
  id1 = id.to_i
  if (id1 - st1).abs > self[:check_identity_grace]
    args = Eye::SystemResources.args(pid)
    msg = "pid_file: '#{Eye::Utils.human_time2(id)}', process: '#{Eye::Utils.human_time2(st)}' (#{args})"
    res = id1 < st1 ? :fail : :touched
    warn "compare_identity: #{res}, #{msg}"
    res
  else
    :ok
  end
end

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


97
98
99
# File 'lib/eye/process/system.rb', line 97

def daemonize(cmd, cfg = {})
  Eye::System.daemonize(cmd, cfg)
end

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


91
92
93
94
95
# File 'lib/eye/process/system.rb', line 91

def execute(cmd, cfg = {})
  defer { Eye::System.execute cmd, cfg }.tap do |res|
    notify(:debug, "Bad exit status of command #{cmd.inspect}(#{res[:exitstatus].inspect})") if res[:exitstatus] != 0
  end
end

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


107
108
109
110
111
# File 'lib/eye/process/system.rb', line 107

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

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


101
102
103
104
105
# File 'lib/eye/process/system.rb', line 101

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

#expand_path(path) ⇒ Object


132
133
134
# File 'lib/eye/process/system.rb', line 132

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

#failsafe_load_pidObject


113
114
115
116
117
118
119
120
121
122
# File 'lib/eye/process/system.rb', line 113

def failsafe_load_pid
  pid = load_pid_from_file

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

  pid if pid && pid > 0
end

#failsafe_save_pidObject


124
125
126
127
128
129
130
# File 'lib/eye/process/system.rb', line 124

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

#get_identityObject


33
34
35
36
37
# File 'lib/eye/process/system.rb', line 33

def get_identity
  File.mtime(self[:pid_file_ex])
rescue Errno::ENOENT
  nil
end

#load_pid_from_fileObject


5
6
7
# File 'lib/eye/process/system.rb', line 5

def load_pid_from_file
  File.read(self[:pid_file_ex]).to_i rescue nil
end

#pid_file_ctimeObject


29
30
31
# File 'lib/eye/process/system.rb', line 29

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

#process_pid_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/eye/process/system.rb', line 63

def process_pid_running?(pid)
  Eye::System.pid_alive?(pid)
end

#process_really_running?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'lib/eye/process/system.rb', line 59

def process_really_running?
  process_pid_running?(self.pid)
end

#save_pid_to_fileObject


9
10
11
12
13
14
15
16
17
18
# File 'lib/eye/process/system.rb', line 9

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


67
68
69
70
71
72
73
74
75
# File 'lib/eye/process/system.rb', line 67

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

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


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/eye/process/system.rb', line 77

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