| In: |
lib/rq-2.0.0/util.rb
|
of course - all the rest goes here
# File lib/rq-2.0.0/util.rb, line 24
24: def append_features c
25: #--{{{
26: super
27: c.extend Util
28: #--}}}
29: end
# File lib/rq-2.0.0/util.rb, line 17
17: def export sym
18: #--{{{
19: sym = "#{ sym }".intern
20: module_function sym
21: public sym
22: #--}}}
23: end
# File lib/rq-2.0.0/util.rb, line 75
75: def alive? pid
76: #--{{{
77: pid = Integer("#{ pid }")
78: begin
79: Process.kill 0, pid
80: true
81: rescue Errno::ESRCH
82: false
83: end
84: #--}}}
85: end
# File lib/rq-2.0.0/util.rb, line 203
203: def btrace e
204: #--{{{
205: (e.backtrace or []).join("\n")
206: #--}}}
207: end
# File lib/rq-2.0.0/util.rb, line 252
252: def columnize buf, width = 80, indent = 0
253: #--{{{
254: column = []
255: words = buf.split %/\s+/o
256: row = ' ' * indent
257: while((word = words.shift))
258: if((row.size + word.size) < (width - 1))
259: row << word
260: else
261: column << row
262: row = ' ' * indent
263: row << word
264: end
265: row << ' ' unless row.size == (width - 1)
266: end
267: column << row unless row.strip.empty?
268: column.join "\n"
269: #--}}}
270: end
# File lib/rq-2.0.0/util.rb, line 272
272: def defval var, default = nil
273: #--{{{
274: v = "#{ var }"
275: c = "DEFAULT_#{ v }".upcase
276: begin
277: klass.send(v) || klass.const_get(c)
278: rescue NameError
279: default
280: end
281: #--}}}
282: end
# File lib/rq-2.0.0/util.rb, line 197
197: def emsg e
198: #--{{{
199: "#{ e.message } - (#{ e.class })"
200: #--}}}
201: end
# File lib/rq-2.0.0/util.rb, line 215
215: def erreq a, b
216: #--{{{
217: a.class == b.class and
218: a.message == b.message and
219: a.backtrace == b.backtrace
220: #--}}}
221: end
# File lib/rq-2.0.0/util.rb, line 209
209: def errmsg e
210: #--{{{
211: emsg(e) << "\n" << btrace(e)
212: #--}}}
213: end
# File lib/rq-2.0.0/util.rb, line 141
141: def escape s, char, esc
142: #--{{{
143: ss = "#{ s }"
144: escape! ss, char, esc
145: ss
146: #--}}}
147: end
# File lib/rq-2.0.0/util.rb, line 132
132: def escape! s, char, esc
133: #--{{{
134: re = %/([#{0x5c.chr << esc}]*)#{char}/
135: s.gsub!(re) do
136: (($1.size % 2 == 0) ? ($1 << esc) : $1) + char
137: end
138: #--}}}
139: end
# File lib/rq-2.0.0/util.rb, line 161
161: def exec(*args, &block)
162: #--{{{
163: begin
164: verbose = $VERBOSE
165: $VERBOSE = nil
166: Kernel::exec(*args, &block)
167: ensure
168: $VERBOSE = verbose
169: end
170: #--}}}
171: end
# File lib/rq-2.0.0/util.rb, line 149
149: def fork(*args, &block)
150: #--{{{
151: begin
152: verbose = $VERBOSE
153: $VERBOSE = nil
154: Process::fork(*args, &block)
155: ensure
156: $VERBOSE = verbose
157: end
158: #--}}}
159: end
# File lib/rq-2.0.0/util.rb, line 60
60: def getopt opt, hash, default = nil
61: #--{{{
62: key = opt
63: return hash[key] if hash.has_key? key
64:
65: key = "#{ key }"
66: return hash[key] if hash.has_key? key
67:
68: key = key.intern
69: return hash[key] if hash.has_key? key
70:
71: return default
72: #--}}}
73: end
# File lib/rq-2.0.0/util.rb, line 54
54: def hashify(*hashes)
55: #--{{{
56: hashes.inject(accum={}){|accum,hash| accum.update hash}
57: #--}}}
58: end
# File lib/rq-2.0.0/util.rb, line 191
191: def host
192: #--{{{
193: @__host__ ||= Socket::gethostname.gsub(%/\..*$/o,'')
194: #--}}}
195: end
# File lib/rq-2.0.0/util.rb, line 185
185: def hostname
186: #--{{{
187: @__hostname__ ||= Socket::gethostname
188: #--}}}
189: end
# File lib/rq-2.0.0/util.rb, line 87
87: def maim(pid, opts = {})
88: #--{{{
89: sigs = getopt('signals', opts) || %(SIGTERM SIGQUIT SIGKILL)
90: suspend = getopt('suspend', opts) || 4
91: pid = Integer("#{ pid }")
92: sigs.each do |sig|
93: begin
94: Process.kill(sig, pid)
95: rescue Errno::ESRCH
96: return nil
97: end
98: sleep 0.2
99: unless alive?(pid)
100: break
101: else
102: sleep suspend
103: end
104: end
105: not alive?(pid)
106: #--}}}
107: end
# File lib/rq-2.0.0/util.rb, line 31
31: def mcp obj
32: #--{{{
33: Marshal.load(Marshal.dump(obj))
34: #--}}}
35: end
# File lib/rq-2.0.0/util.rb, line 43
43: def realpath path
44: #--{{{
45: path = File::expand_path "#{ path }"
46: begin
47: Pathname::new(path).realpath.to_s
48: rescue Errno::ENOENT, Errno::ENOTDIR
49: path
50: end
51: #--}}}
52: end
# File lib/rq-2.0.0/util.rb, line 117
117: def stamptime string, local = true
118: #--{{{
119: string = "#{ string }"
120: pat = %/^\s*(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d).(\d\d\d\d\d\d)\s*$/o
121: match = pat.match string
122: raise ArgumentError, "<#{ string.inspect }>" unless match
123: yyyy,mm,dd,h,m,s,u = match.to_a[1..-1].map{|m| m.to_i}
124: if local
125: Time.local yyyy,mm,dd,h,m,s,u
126: else
127: Time.gm yyyy,mm,dd,h,m,s,u
128: end
129: #--}}}
130: end
# File lib/rq-2.0.0/util.rb, line 173
173: def system(*args, &block)
174: #--{{{
175: begin
176: verbose = $VERBOSE
177: $VERBOSE = nil
178: Kernel::system(*args, &block)
179: ensure
180: $VERBOSE = verbose
181: end
182: #--}}}
183: end
# File lib/rq-2.0.0/util.rb, line 109
109: def timestamp time = Time.now
110: #--{{{
111: usec = "#{ time.usec }"
112: usec << ('0' * (6 - usec.size)) if usec.size < 6
113: time.strftime('%Y-%m-%d %H:%M:%S.') << usec
114: #--}}}
115: end
# File lib/rq-2.0.0/util.rb, line 223
223: def tmpnam dir = Dir.tmpdir, seed = File::basename($0)
224: #--{{{
225: pid = Process.pid
226: path = "%s_%s_%s_%s_%d" %
227: [Util::hostname, seed, pid, Util::timestamp.gsub(/\s+/o,'_'), rand(101010)]
228: File::join(dir, path)
229: #--}}}
230: end
# File lib/rq-2.0.0/util.rb, line 232
232: def uncache file
233: #--{{{
234: refresh = nil
235: begin
236: is_a_file = File === file
237: path = (is_a_file ? file.path : file.to_s)
238: stat = (is_a_file ? file.stat : File::stat(file.to_s))
239: refresh = tmpnam(File::dirname(path))
240: File::link path, refresh rescue File::symlink path, refresh
241: File::chmod stat.mode, path
242: File::utime stat.atime, stat.mtime, path
243: ensure
244: begin
245: File::unlink refresh if refresh
246: rescue Errno::ENOENT
247: end
248: end
249: #--}}}
250: end