Module: RQ::Util

Included in:
JobQueue, MainHelper, QDB
Defined in:
lib/rq/util.rb

Overview

of course - all the rest goes here

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(c) ⇒ Object

–}}}



24
25
26
27
28
29
# File 'lib/rq/util.rb', line 24

def append_features c
#--{{{
  super
  c.extend Util 
#--}}}
end

.export(sym) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/rq/util.rb', line 17

def export sym
#--{{{
  sym = "#{ sym }".intern
  module_function sym 
  public sym 
#--}}}
end

Instance Method Details

#alive?(pid) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rq/util.rb', line 75

def alive? pid
#--{{{
  pid = Integer("#{ pid }")
  begin
    Process.kill 0, pid
    true
  rescue Errno::ESRCH
    false
  end
#--}}}
end

#btrace(e) ⇒ Object



203
204
205
206
207
# File 'lib/rq/util.rb', line 203

def btrace e
#--{{{
  (e.backtrace or []).join("\n")
#--}}}
end

#columnize(buf, width = 80, indent = 0) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/rq/util.rb', line 252

def columnize buf, width = 80, indent = 0
#--{{{
  column = []
  words = buf.split %r/\s+/o
  row = ' ' * indent
  while((word = words.shift))
    if((row.size + word.size) < (width - 1))
      row << word
    else
      column << row
      row = ' ' * indent
      row << word
    end
    row << ' ' unless row.size == (width - 1)
  end
  column << row unless row.strip.empty?
  column.join "\n"
#--}}}
end

#defval(var, default = nil) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rq/util.rb', line 272

def defval var, default = nil
#--{{{
  v = "#{ var }"
  c = "DEFAULT_#{ v }".upcase
  begin
    klass.send(v) || klass.const_get(c)
  rescue NameError
    default
  end
#--}}}
end

#emsg(e) ⇒ Object



197
198
199
200
201
# File 'lib/rq/util.rb', line 197

def emsg e
#--{{{
  "#{ e.message } - (#{ e.class })"
#--}}}
end

#erreq(a, b) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/rq/util.rb', line 215

def erreq a, b
#--{{{
  a.class == b.class and
  a.message == b.message and
  a.backtrace == b.backtrace
#--}}}
end

#errmsg(e) ⇒ Object



209
210
211
212
213
# File 'lib/rq/util.rb', line 209

def errmsg e
#--{{{
  emsg(e) << "\n" << btrace(e)
#--}}}
end

#escape(s, char, esc) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/rq/util.rb', line 141

def escape s, char, esc
#--{{{
  ss = "#{ s }"
  escape! ss, char, esc
  ss
#--}}}
end

#escape!(s, char, esc) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/rq/util.rb', line 132

def escape! s, char, esc
#--{{{
  re = %r/([#{0x5c.chr << esc}]*)#{char}/
  s.gsub!(re) do
    (($1.size % 2 == 0) ? ($1 << esc) : $1) + char 
  end
#--}}}
end

#exec(*args, &block) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rq/util.rb', line 161

def exec(*args, &block)
#--{{{
  begin
    verbose = $VERBOSE
    $VERBOSE = nil
    Kernel::exec(*args, &block)
  ensure
    $VERBOSE = verbose
  end
#--}}}
end

#fork(*args, &block) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rq/util.rb', line 149

def fork(*args, &block)
#--{{{
  begin
    verbose = $VERBOSE
    $VERBOSE = nil
    Process::fork(*args, &block)
  ensure
    $VERBOSE = verbose
  end
#--}}}
end

#getopt(opt, hash, default = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rq/util.rb', line 60

def getopt opt, hash, default = nil
#--{{{
  key = opt
  return hash[key] if hash.has_key? key

  key = "#{ key }"
  return hash[key] if hash.has_key? key

  key = key.intern 
  return hash[key] if hash.has_key? key

  return default
#--}}}
end

#hashify(*hashes) ⇒ Object



54
55
56
57
58
# File 'lib/rq/util.rb', line 54

def hashify(*hashes)
#--{{{
  hashes.inject(accum={}){|accum,hash| accum.update hash}
#--}}}
end

#hms(s) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/rq/util.rb', line 284

def hms s
#--{{{
  h, s = s.divmod 3600
  m, s = s.divmod 60
  [h.to_i, m.to_i, s]
#--}}}
end

#hostObject



191
192
193
194
195
# File 'lib/rq/util.rb', line 191

def host
#--{{{
  @__host__ ||= Socket::gethostname.gsub(%r/\..*$/o,'')
#--}}}
end

#hostnameObject



185
186
187
188
189
# File 'lib/rq/util.rb', line 185

def hostname
#--{{{
  @__hostname__ ||= Socket::gethostname
#--}}}
end

#klassObject



37
38
39
40
41
# File 'lib/rq/util.rb', line 37

def klass
#--{{{
  self.class
#--}}}
end

#maim(pid, opts = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rq/util.rb', line 87

def maim(pid, opts = {})
#--{{{
  sigs = getopt('signals', opts) || %w(SIGTERM SIGQUIT SIGKILL) 
  suspend = getopt('suspend', opts) || 4
  pid = Integer("#{ pid }")
  sigs.each do |sig|
    begin
      Process.kill(sig, pid)
    rescue Errno::ESRCH
      return nil
    end
    sleep 0.2
    unless alive?(pid)
      break
    else
      sleep suspend
    end
  end
  not alive?(pid)
#--}}}
end

#mcp(obj) ⇒ Object



31
32
33
34
35
# File 'lib/rq/util.rb', line 31

def mcp obj
#--{{{
  Marshal.load(Marshal.dump(obj))
#--}}}
end

#realpath(path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rq/util.rb', line 43

def realpath path
#--{{{
  path = File::expand_path "#{ path }"
  begin
    Pathname::new(path).realpath.to_s
  rescue Errno::ENOENT, Errno::ENOTDIR
    path
  end
#--}}}
end

#stamptime(string, local = true) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rq/util.rb', line 117

def stamptime string, local = true 
#--{{{
  string = "#{ string }"
  pat = %r/^\s*(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d).(\d\d\d\d\d\d)\s*$/o
  match = pat.match string
  raise ArgumentError, "<#{ string.inspect }>" unless match
  yyyy,mm,dd,h,m,s,u = match.to_a[1..-1].map{|m| m.to_i}
  if local
    Time.local yyyy,mm,dd,h,m,s,u
  else
    Time.gm yyyy,mm,dd,h,m,s,u
  end
#--}}}
end

#system(*args, &block) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rq/util.rb', line 173

def system(*args, &block)
#--{{{
  begin
    verbose = $VERBOSE
    $VERBOSE = nil
    Kernel::system(*args, &block)
  ensure
    $VERBOSE = verbose
  end
#--}}}
end

#timestamp(time = Time.now) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/rq/util.rb', line 109

def timestamp time = Time.now
#--{{{
  usec = "#{ time.usec }"
  usec << ('0' * (6 - usec.size)) if usec.size < 6 
  time.strftime('%Y-%m-%d %H:%M:%S.') << usec
#--}}}
end

#tmpnam(dir = Dir.tmpdir, seed = File::basename($0)) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/rq/util.rb', line 223

def tmpnam dir = Dir.tmpdir, seed = File::basename($0)
#--{{{
  pid = Process.pid
  path = "%s_%s_%s_%s_%d" % 
    [Util::hostname, seed, pid, Util::timestamp.gsub(/\s+/o,'_'), rand(101010)]
  File::join(dir, path)
#--}}}
end

#uncache(file) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rq/util.rb', line 232

def uncache file 
#--{{{
  refresh = nil
  begin
    is_a_file = File === file
    path = (is_a_file ? file.path : file.to_s) 
    stat = (is_a_file ? file.stat : File::stat(file.to_s)) 
    refresh = tmpnam(File::dirname(path))
    File::link path, refresh rescue File::symlink path, refresh
    File::chmod stat.mode, path
    File::utime stat.atime, stat.mtime, path
  ensure 
    begin
      File::unlink refresh if refresh
    rescue Errno::ENOENT
    end
  end
#--}}}
end

#which_rubyObject



292
293
294
295
296
297
# File 'lib/rq/util.rb', line 292

def which_ruby
#--{{{
  c = ::Config::CONFIG
  realpath( File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT'] )
#--}}}
end