opengotham_resque 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. data/.gitignore +2 -0
  2. data/.kick +26 -0
  3. data/HISTORY.md +142 -0
  4. data/LICENSE +20 -0
  5. data/README.markdown +794 -0
  6. data/Rakefile +112 -0
  7. data/bin/resque +57 -0
  8. data/bin/resque-web +23 -0
  9. data/config.ru +14 -0
  10. data/deps.rip +7 -0
  11. data/docs/HOOKS.md +121 -0
  12. data/docs/PLUGINS.md +93 -0
  13. data/examples/async_helper.rb +31 -0
  14. data/examples/demo/README.markdown +71 -0
  15. data/examples/demo/Rakefile +8 -0
  16. data/examples/demo/app.rb +38 -0
  17. data/examples/demo/config.ru +19 -0
  18. data/examples/demo/job.rb +22 -0
  19. data/examples/god/resque.god +53 -0
  20. data/examples/god/stale.god +26 -0
  21. data/examples/instance.rb +11 -0
  22. data/examples/monit/resque.monit +6 -0
  23. data/examples/simple.rb +30 -0
  24. data/init.rb +1 -0
  25. data/lib/resque.rb +287 -0
  26. data/lib/resque/errors.rb +10 -0
  27. data/lib/resque/failure.rb +66 -0
  28. data/lib/resque/failure/base.rb +61 -0
  29. data/lib/resque/failure/hoptoad.rb +132 -0
  30. data/lib/resque/failure/multiple.rb +48 -0
  31. data/lib/resque/failure/redis.rb +40 -0
  32. data/lib/resque/helpers.rb +63 -0
  33. data/lib/resque/job.rb +207 -0
  34. data/lib/resque/plugin.rb +46 -0
  35. data/lib/resque/server.rb +201 -0
  36. data/lib/resque/server/public/idle.png +0 -0
  37. data/lib/resque/server/public/jquery-1.3.2.min.js +19 -0
  38. data/lib/resque/server/public/jquery.relatize_date.js +95 -0
  39. data/lib/resque/server/public/poll.png +0 -0
  40. data/lib/resque/server/public/ranger.js +67 -0
  41. data/lib/resque/server/public/reset.css +48 -0
  42. data/lib/resque/server/public/style.css +81 -0
  43. data/lib/resque/server/public/working.png +0 -0
  44. data/lib/resque/server/test_helper.rb +19 -0
  45. data/lib/resque/server/views/error.erb +1 -0
  46. data/lib/resque/server/views/failed.erb +53 -0
  47. data/lib/resque/server/views/key_sets.erb +20 -0
  48. data/lib/resque/server/views/key_string.erb +11 -0
  49. data/lib/resque/server/views/layout.erb +44 -0
  50. data/lib/resque/server/views/next_more.erb +10 -0
  51. data/lib/resque/server/views/overview.erb +4 -0
  52. data/lib/resque/server/views/queues.erb +49 -0
  53. data/lib/resque/server/views/stats.erb +62 -0
  54. data/lib/resque/server/views/workers.erb +78 -0
  55. data/lib/resque/server/views/working.erb +69 -0
  56. data/lib/resque/stat.rb +53 -0
  57. data/lib/resque/tasks.rb +39 -0
  58. data/lib/resque/version.rb +3 -0
  59. data/lib/resque/worker.rb +478 -0
  60. data/tasks/redis.rake +159 -0
  61. data/tasks/resque.rake +2 -0
  62. data/test/job_hooks_test.rb +302 -0
  63. data/test/job_plugins_test.rb +209 -0
  64. data/test/plugin_test.rb +116 -0
  65. data/test/redis-test.conf +132 -0
  66. data/test/resque-web_test.rb +54 -0
  67. data/test/resque_test.rb +225 -0
  68. data/test/test_helper.rb +111 -0
  69. data/test/worker_test.rb +302 -0
  70. metadata +199 -0
@@ -0,0 +1,69 @@
1
+ <% if params[:id] && (worker = Resque::Worker.find(params[:id])) && worker.job %>
2
+ <h1><%= worker %>'s job</h1>
3
+
4
+ <table>
5
+ <tr>
6
+ <th>&nbsp;</th>
7
+ <th>Where</th>
8
+ <th>Queue</th>
9
+ <th>Started</th>
10
+ <th>Class</th>
11
+ <th>Args</th>
12
+ </tr>
13
+ <tr>
14
+ <td><img src="<%=u 'working.png' %>" alt="working" title="working"></td>
15
+ <% host, pid, _ = worker.to_s.split(':') %>
16
+ <td><a href="<%=u "/workers/#{worker}" %>"><%= host %>:<%= pid %></a></td>
17
+ <% data = worker.job %>
18
+ <% queue = data['queue'] %>
19
+ <td><a class="queue" href="<%=u "/queues/#{queue}" %>"><%= queue %></a></td>
20
+ <td><span class="time"><%= data['run_at'] %></span></td>
21
+ <td>
22
+ <code><%= data['payload']['class'] %></code>
23
+ </td>
24
+ <td><%=h data['payload']['args'].inspect %></td>
25
+ </tr>
26
+ </table>
27
+
28
+ <% else %>
29
+
30
+ <% workers = resque.working %>
31
+ <h1 class='wi'><%= workers.size %> of <%= resque.workers.size %> Workers Working</h1>
32
+ <p class='intro'>The list below contains all workers which are currently running a job.</p>
33
+ <table class='workers'>
34
+ <tr>
35
+ <th>&nbsp;</th>
36
+ <th>Where</th>
37
+ <th>Queue</th>
38
+ <th>Processing</th>
39
+ </tr>
40
+ <% if workers.empty? %>
41
+ <tr>
42
+ <td colspan="4" class='no-data'>Nothing is happening right now...</td>
43
+ </tr>
44
+ <% end %>
45
+
46
+ <% for worker in workers.sort_by { |w| w.job['run_at'] ? w.job['run_at'] : '' } %>
47
+ <% job = worker.job %>
48
+ <% next if worker.idle? %>
49
+
50
+ <tr>
51
+ <td class='icon'><img src="<%=u state = worker.state %>.png" alt="<%= state %>" title="<%= state %>"></td>
52
+ <% host, pid, queues = worker.to_s.split(':') %>
53
+ <td class='where'><a href="<%=u "/workers/#{worker}" %>"><%= host %>:<%= pid %></a></td>
54
+ <td class='queues queue'>
55
+ <a class="queue-tag" href="<%=u "/queues/#{job['queue']}" %>"><%= job['queue'] %></a>
56
+ </td>
57
+ <td class='process'>
58
+ <% if job['queue'] %>
59
+ <code><%= job['payload']['class'] %></code>
60
+ <small><a class="queue time" href="<%=u "/working/#{worker}" %>"><%= job['run_at'] %></a></small>
61
+ <% else %>
62
+ <span class='waiting'>Waiting for a job...</span>
63
+ <% end %>
64
+ </td>
65
+ </tr>
66
+ <% end %>
67
+ </table>
68
+
69
+ <% end %>
@@ -0,0 +1,53 @@
1
+ module Resque
2
+ # The stat subsystem. Used to keep track of integer counts.
3
+ #
4
+ # Get a stat: Stat[name]
5
+ # Incr a stat: Stat.incr(name)
6
+ # Decr a stat: Stat.decr(name)
7
+ # Kill a stat: Stat.clear(name)
8
+ module Stat
9
+ extend self
10
+ extend Helpers
11
+
12
+ # Returns the int value of a stat, given a string stat name.
13
+ def get(stat)
14
+ redis.get("stat:#{stat}").to_i
15
+ end
16
+
17
+ # Alias of `get`
18
+ def [](stat)
19
+ get(stat)
20
+ end
21
+
22
+ # For a string stat name, increments the stat by one.
23
+ #
24
+ # Can optionally accept a second int parameter. The stat is then
25
+ # incremented by that amount.
26
+ def incr(stat, by = 1)
27
+ redis.incrby("stat:#{stat}", by)
28
+ end
29
+
30
+ # Increments a stat by one.
31
+ def <<(stat)
32
+ incr stat
33
+ end
34
+
35
+ # For a string stat name, decrements the stat by one.
36
+ #
37
+ # Can optionally accept a second int parameter. The stat is then
38
+ # decremented by that amount.
39
+ def decr(stat, by = 1)
40
+ redis.decrby("stat:#{stat}", by)
41
+ end
42
+
43
+ # Decrements a stat by one.
44
+ def >>(stat)
45
+ decr stat
46
+ end
47
+
48
+ # Removes a stat from Redis, effectively setting it to 0.
49
+ def clear(stat)
50
+ redis.del("stat:#{stat}")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,39 @@
1
+ # require 'resque/tasks'
2
+ # will give you the resque tasks
3
+
4
+ namespace :resque do
5
+ task :setup
6
+
7
+ desc "Start a Resque worker"
8
+ task :work => :setup do
9
+ require 'resque'
10
+
11
+ worker = nil
12
+ queues = (ENV['QUEUES'] || ENV['QUEUE']).to_s.split(',')
13
+
14
+ begin
15
+ worker = Resque::Worker.new(*queues)
16
+ worker.verbose = ENV['LOGGING'] || ENV['VERBOSE']
17
+ worker.very_verbose = ENV['VVERBOSE']
18
+ rescue Resque::NoQueueError
19
+ abort "set QUEUE env var, e.g. $ QUEUE=critical,high rake resque:work"
20
+ end
21
+
22
+ puts "*** Starting worker #{worker}"
23
+
24
+ worker.work(ENV['INTERVAL'] || 5) # interval, will block
25
+ end
26
+
27
+ desc "Start multiple Resque workers. Should only be used in dev mode."
28
+ task :workers do
29
+ threads = []
30
+
31
+ ENV['COUNT'].to_i.times do
32
+ threads << Thread.new do
33
+ system "rake resque:work"
34
+ end
35
+ end
36
+
37
+ threads.each { |thread| thread.join }
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Resque
2
+ Version = '1.8.2'
3
+ end
@@ -0,0 +1,478 @@
1
+ module Resque
2
+ # A Resque Worker processes jobs. On platforms that support fork(2),
3
+ # the worker will fork off a child to process each job. This ensures
4
+ # a clean slate when beginning the next job and cuts down on gradual
5
+ # memory growth as well as low level failures.
6
+ #
7
+ # It also ensures workers are always listening to signals from you,
8
+ # their master, and can react accordingly.
9
+ class Worker
10
+ include Resque::Helpers
11
+ extend Resque::Helpers
12
+
13
+ # Whether the worker should log basic info to STDOUT
14
+ attr_accessor :verbose
15
+
16
+ # Whether the worker should log lots of info to STDOUT
17
+ attr_accessor :very_verbose
18
+
19
+ # Boolean indicating whether this worker can or can not fork.
20
+ # Automatically set if a fork(2) fails.
21
+ attr_accessor :cant_fork
22
+
23
+ attr_writer :to_s
24
+
25
+ # Returns an array of all worker objects.
26
+ def self.all
27
+ redis.smembers(:workers).map { |id| find(id) }
28
+ end
29
+
30
+ # Returns an array of all worker objects currently processing
31
+ # jobs.
32
+ def self.working
33
+ names = all
34
+ return [] unless names.any?
35
+ names.map! { |name| "worker:#{name}" }
36
+ redis.mapped_mget(*names).keys.map do |key|
37
+ find key.sub("worker:", '')
38
+ end.compact
39
+ end
40
+
41
+ # Returns a single worker object. Accepts a string id.
42
+ def self.find(worker_id)
43
+ if exists? worker_id
44
+ queues = worker_id.split(':')[-1].split(',')
45
+ worker = new(*queues)
46
+ worker.to_s = worker_id
47
+ worker
48
+ else
49
+ nil
50
+ end
51
+ end
52
+
53
+ # Alias of `find`
54
+ def self.attach(worker_id)
55
+ find(worker_id)
56
+ end
57
+
58
+ # Given a string worker id, return a boolean indicating whether the
59
+ # worker exists
60
+ def self.exists?(worker_id)
61
+ redis.sismember(:workers, worker_id)
62
+ end
63
+
64
+ # Workers should be initialized with an array of string queue
65
+ # names. The order is important: a Worker will check the first
66
+ # queue given for a job. If none is found, it will check the
67
+ # second queue name given. If a job is found, it will be
68
+ # processed. Upon completion, the Worker will again check the
69
+ # first queue given, and so forth. In this way the queue list
70
+ # passed to a Worker on startup defines the priorities of queues.
71
+ #
72
+ # If passed a single "*", this Worker will operate on all queues
73
+ # in alphabetical order. Queues can be dynamically added or
74
+ # removed without needing to restart workers using this method.
75
+ def initialize(*queues)
76
+ @queues = queues
77
+ validate_queues
78
+ end
79
+
80
+ # A worker must be given a queue, otherwise it won't know what to
81
+ # do with itself.
82
+ #
83
+ # You probably never need to call this.
84
+ def validate_queues
85
+ if @queues.nil? || @queues.empty?
86
+ raise NoQueueError.new("Please give each worker at least one queue.")
87
+ end
88
+ end
89
+
90
+ # This is the main workhorse method. Called on a Worker instance,
91
+ # it begins the worker life cycle.
92
+ #
93
+ # The following events occur during a worker's life cycle:
94
+ #
95
+ # 1. Startup: Signals are registered, dead workers are pruned,
96
+ # and this worker is registered.
97
+ # 2. Work loop: Jobs are pulled from a queue and processed.
98
+ # 3. Teardown: This worker is unregistered.
99
+ #
100
+ # Can be passed an integer representing the polling frequency.
101
+ # The default is 5 seconds, but for a semi-active site you may
102
+ # want to use a smaller value.
103
+ #
104
+ # Also accepts a block which will be passed the job as soon as it
105
+ # has completed processing. Useful for testing.
106
+ def work(interval = 5, &block)
107
+ $0 = "resque: Starting"
108
+ startup
109
+
110
+ loop do
111
+ break if @shutdown
112
+
113
+ if not @paused and job = reserve
114
+ log "got: #{job.inspect}"
115
+ run_hook :before_fork
116
+ working_on job
117
+
118
+ if @child = fork
119
+ rand # Reseeding
120
+ procline "Forked #{@child} at #{Time.now.to_i}"
121
+ Process.wait
122
+ else
123
+ procline "Processing #{job.queue} since #{Time.now.to_i}"
124
+ perform(job, &block)
125
+ exit! unless @cant_fork
126
+ end
127
+
128
+ done_working
129
+ @child = nil
130
+ else
131
+ break if interval.to_i == 0
132
+ log! "Sleeping for #{interval.to_i}"
133
+ procline @paused ? "Paused" : "Waiting for #{@queues.join(',')}"
134
+ sleep interval.to_i
135
+ end
136
+ end
137
+
138
+ ensure
139
+ unregister_worker
140
+ end
141
+
142
+ # DEPRECATED. Processes a single job. If none is given, it will
143
+ # try to produce one. Usually run in the child.
144
+ def process(job = nil, &block)
145
+ return unless job ||= reserve
146
+
147
+ working_on job
148
+ perform(job, &block)
149
+ ensure
150
+ done_working
151
+ end
152
+
153
+ # Processes a given job in the child.
154
+ def perform(job)
155
+ begin
156
+ run_hook :after_fork, job
157
+ job.perform
158
+ rescue Object => e
159
+ log "#{job.inspect} failed: #{e.inspect}"
160
+ job.fail(e)
161
+ failed!
162
+ else
163
+ log "done: #{job.inspect}"
164
+ ensure
165
+ yield job if block_given?
166
+ end
167
+ end
168
+
169
+ # Attempts to grab a job off one of the provided queues. Returns
170
+ # nil if no job can be found.
171
+ def reserve
172
+ queues.each do |queue|
173
+ log! "Checking #{queue}"
174
+ if job = Resque::Job.reserve(queue)
175
+ log! "Found job on #{queue}"
176
+ return job
177
+ end
178
+ end
179
+
180
+ nil
181
+ end
182
+
183
+ # Returns a list of queues to use when searching for a job.
184
+ # A splat ("*") means you want every queue (in alpha order) - this
185
+ # can be useful for dynamically adding new queues.
186
+ def queues
187
+ @queues[0] == "*" ? Resque.queues.sort : @queues
188
+ end
189
+
190
+ # Not every platform supports fork. Here we do our magic to
191
+ # determine if yours does.
192
+ def fork
193
+ @cant_fork = true if $TESTING
194
+
195
+ return if @cant_fork
196
+
197
+ begin
198
+ # IronRuby doesn't support `Kernel.fork` yet
199
+ if Kernel.respond_to?(:fork)
200
+ Kernel.fork
201
+ else
202
+ raise NotImplementedError
203
+ end
204
+ rescue NotImplementedError
205
+ @cant_fork = true
206
+ nil
207
+ end
208
+ end
209
+
210
+ # Runs all the methods needed when a worker begins its lifecycle.
211
+ def startup
212
+ enable_gc_optimizations
213
+ register_signal_handlers
214
+ prune_dead_workers
215
+ run_hook :before_first_fork
216
+ register_worker
217
+
218
+ # Fix buffering so we can `rake resque:work > resque.log` and
219
+ # get output from the child in there.
220
+ $stdout.sync = true
221
+ end
222
+
223
+ # Enables GC Optimizations if you're running REE.
224
+ # http://www.rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
225
+ def enable_gc_optimizations
226
+ if GC.respond_to?(:copy_on_write_friendly=)
227
+ GC.copy_on_write_friendly = true
228
+ end
229
+ end
230
+
231
+ # Registers the various signal handlers a worker responds to.
232
+ #
233
+ # TERM: Shutdown immediately, stop processing jobs.
234
+ # INT: Shutdown immediately, stop processing jobs.
235
+ # QUIT: Shutdown after the current job has finished processing.
236
+ # USR1: Kill the forked child immediately, continue processing jobs.
237
+ # USR2: Don't process any new jobs
238
+ # CONT: Start processing jobs again after a USR2
239
+ def register_signal_handlers
240
+ trap('TERM') { shutdown! }
241
+ trap('INT') { shutdown! }
242
+
243
+ begin
244
+ trap('QUIT') { shutdown }
245
+ trap('USR1') { kill_child }
246
+ trap('USR2') { pause_processing }
247
+ trap('CONT') { unpause_processing }
248
+ rescue ArgumentError
249
+ warn "Signals QUIT, USR1, USR2, and/or CONT not supported."
250
+ end
251
+
252
+ log! "Registered signals"
253
+ end
254
+
255
+ # Schedule this worker for shutdown. Will finish processing the
256
+ # current job.
257
+ def shutdown
258
+ log 'Exiting...'
259
+ @shutdown = true
260
+ end
261
+
262
+ # Kill the child and shutdown immediately.
263
+ def shutdown!
264
+ shutdown
265
+ kill_child
266
+ end
267
+
268
+ # Kills the forked child immediately, without remorse. The job it
269
+ # is processing will not be completed.
270
+ def kill_child
271
+ if @child
272
+ log! "Killing child at #{@child}"
273
+ if system("ps -o pid,state -p #{@child}")
274
+ Process.kill("KILL", @child) rescue nil
275
+ else
276
+ log! "Child #{@child} not found, restarting."
277
+ shutdown
278
+ end
279
+ end
280
+ end
281
+
282
+ # Stop processing jobs after the current one has completed (if we're
283
+ # currently running one).
284
+ def pause_processing
285
+ log "USR2 received; pausing job processing"
286
+ @paused = true
287
+ end
288
+
289
+ # Start processing jobs again after a pause
290
+ def unpause_processing
291
+ log "CONT received; resuming job processing"
292
+ @paused = false
293
+ end
294
+
295
+ # Looks for any workers which should be running on this server
296
+ # and, if they're not, removes them from Redis.
297
+ #
298
+ # This is a form of garbage collection. If a server is killed by a
299
+ # hard shutdown, power failure, or something else beyond our
300
+ # control, the Resque workers will not die gracefully and therefore
301
+ # will leave stale state information in Redis.
302
+ #
303
+ # By checking the current Redis state against the actual
304
+ # environment, we can determine if Redis is old and clean it up a bit.
305
+ def prune_dead_workers
306
+ all_workers = Worker.all
307
+ known_workers = worker_pids unless all_workers.empty?
308
+ all_workers.each do |worker|
309
+ host, pid, queues = worker.id.split(':')
310
+ next unless host == hostname
311
+ next if known_workers.include?(pid)
312
+ log! "Pruning dead worker: #{worker}"
313
+ worker.unregister_worker
314
+ end
315
+ end
316
+
317
+ # Registers ourself as a worker. Useful when entering the worker
318
+ # lifecycle on startup.
319
+ def register_worker
320
+ redis.sadd(:workers, self)
321
+ started!
322
+ end
323
+
324
+ # Runs a named hook, passing along any arguments.
325
+ def run_hook(name, *args)
326
+ return unless hook = Resque.send(name)
327
+ msg = "Running #{name} hook"
328
+ msg << " with #{args.inspect}" if args.any?
329
+ log msg
330
+
331
+ args.any? ? hook.call(*args) : hook.call
332
+ end
333
+
334
+ # Unregisters ourself as a worker. Useful when shutting down.
335
+ def unregister_worker
336
+ # If we're still processing a job, make sure it gets logged as a
337
+ # failure.
338
+ if (hash = processing) && !hash.empty?
339
+ job = Job.new(hash['queue'], hash['payload'])
340
+ # Ensure the proper worker is attached to this job, even if
341
+ # it's not the precise instance that died.
342
+ job.worker = self
343
+ job.fail(DirtyExit.new)
344
+ end
345
+
346
+ redis.srem(:workers, self)
347
+ redis.del("worker:#{self}")
348
+ redis.del("worker:#{self}:started")
349
+
350
+ Stat.clear("processed:#{self}")
351
+ Stat.clear("failed:#{self}")
352
+ end
353
+
354
+ # Given a job, tells Redis we're working on it. Useful for seeing
355
+ # what workers are doing and when.
356
+ def working_on(job)
357
+ job.worker = self
358
+ data = encode \
359
+ :queue => job.queue,
360
+ :run_at => Time.now.to_s,
361
+ :payload => job.payload
362
+ redis.set("worker:#{self}", data)
363
+ end
364
+
365
+ # Called when we are done working - clears our `working_on` state
366
+ # and tells Redis we processed a job.
367
+ def done_working
368
+ processed!
369
+ redis.del("worker:#{self}")
370
+ end
371
+
372
+ # How many jobs has this worker processed? Returns an int.
373
+ def processed
374
+ Stat["processed:#{self}"]
375
+ end
376
+
377
+ # Tell Redis we've processed a job.
378
+ def processed!
379
+ Stat << "processed"
380
+ Stat << "processed:#{self}"
381
+ end
382
+
383
+ # How many failed jobs has this worker seen? Returns an int.
384
+ def failed
385
+ Stat["failed:#{self}"]
386
+ end
387
+
388
+ # Tells Redis we've failed a job.
389
+ def failed!
390
+ Stat << "failed"
391
+ Stat << "failed:#{self}"
392
+ end
393
+
394
+ # What time did this worker start? Returns an instance of `Time`
395
+ def started
396
+ redis.get "worker:#{self}:started"
397
+ end
398
+
399
+ # Tell Redis we've started
400
+ def started!
401
+ redis.set("worker:#{self}:started", Time.now.to_s)
402
+ end
403
+
404
+ # Returns a hash explaining the Job we're currently processing, if any.
405
+ def job
406
+ decode(redis.get("worker:#{self}")) || {}
407
+ end
408
+ alias_method :processing, :job
409
+
410
+ # Boolean - true if working, false if not
411
+ def working?
412
+ state == :working
413
+ end
414
+
415
+ # Boolean - true if idle, false if not
416
+ def idle?
417
+ state == :idle
418
+ end
419
+
420
+ # Returns a symbol representing the current worker state,
421
+ # which can be either :working or :idle
422
+ def state
423
+ redis.exists("worker:#{self}") ? :working : :idle
424
+ end
425
+
426
+ # Is this worker the same as another worker?
427
+ def ==(other)
428
+ to_s == other.to_s
429
+ end
430
+
431
+ def inspect
432
+ "#<Worker #{to_s}>"
433
+ end
434
+
435
+ # The string representation is the same as the id for this worker
436
+ # instance. Can be used with `Worker.find`.
437
+ def to_s
438
+ @to_s ||= "#{hostname}:#{Process.pid}:#{@queues.join(',')}"
439
+ end
440
+ alias_method :id, :to_s
441
+
442
+ # chomp'd hostname of this machine
443
+ def hostname
444
+ @hostname ||= `hostname`.chomp
445
+ end
446
+
447
+ # Returns an array of string pids of all the other workers on this
448
+ # machine. Useful when pruning dead workers on startup.
449
+ def worker_pids
450
+ `ps -A -o pid,command | grep [r]esque`.split("\n").map do |line|
451
+ line.split(' ')[0]
452
+ end
453
+ end
454
+
455
+ # Given a string, sets the procline ($0) and logs.
456
+ # Procline is always in the format of:
457
+ # resque-VERSION: STRING
458
+ def procline(string)
459
+ $0 = "resque-#{Resque::Version}: #{string}"
460
+ log! $0
461
+ end
462
+
463
+ # Log a message to STDOUT if we are verbose or very_verbose.
464
+ def log(message)
465
+ if verbose
466
+ puts "*** #{message}"
467
+ elsif very_verbose
468
+ time = Time.now.strftime('%I:%M:%S %Y-%m-%d')
469
+ puts "** [#{time}] #$$: #{message}"
470
+ end
471
+ end
472
+
473
+ # Logs a very verbose message to STDOUT.
474
+ def log!(message)
475
+ log message if very_verbose
476
+ end
477
+ end
478
+ end