Module: RQ::Util

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(c) ⇒ Object

}}}



21
22
23
24
25
26
# File 'lib/rq-0.1.7/util.rb', line 21

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

.export(sym) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rq-0.1.7/util.rb', line 14

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

Instance Method Details

#alive?(pid) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rq-0.1.7/util.rb', line 72

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

#btrace(e) ⇒ Object



200
201
202
203
204
# File 'lib/rq-0.1.7/util.rb', line 200

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

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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rq-0.1.7/util.rb', line 249

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



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rq-0.1.7/util.rb', line 269

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



194
195
196
197
198
# File 'lib/rq-0.1.7/util.rb', line 194

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

#erreq(a, b) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/rq-0.1.7/util.rb', line 212

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

#errmsg(e) ⇒ Object



206
207
208
209
210
# File 'lib/rq-0.1.7/util.rb', line 206

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

#escape(s, char, esc) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/rq-0.1.7/util.rb', line 138

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

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



129
130
131
132
133
134
135
136
# File 'lib/rq-0.1.7/util.rb', line 129

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rq-0.1.7/util.rb', line 158

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

#fork(*args, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rq-0.1.7/util.rb', line 146

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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rq-0.1.7/util.rb', line 57

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



51
52
53
54
55
# File 'lib/rq-0.1.7/util.rb', line 51

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

#hostObject



188
189
190
191
192
# File 'lib/rq-0.1.7/util.rb', line 188

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

#hostnameObject



182
183
184
185
186
# File 'lib/rq-0.1.7/util.rb', line 182

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

#klassObject



34
35
36
37
38
# File 'lib/rq-0.1.7/util.rb', line 34

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

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rq-0.1.7/util.rb', line 84

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



28
29
30
31
32
# File 'lib/rq-0.1.7/util.rb', line 28

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

#realpath(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/rq-0.1.7/util.rb', line 40

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)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rq-0.1.7/util.rb', line 114

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



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rq-0.1.7/util.rb', line 170

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

#timestamp(time = Time.now) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/rq-0.1.7/util.rb', line 106

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



220
221
222
223
224
225
226
227
# File 'lib/rq-0.1.7/util.rb', line 220

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rq-0.1.7/util.rb', line 229

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