scotttam-resque 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,6 +10,11 @@ module Resque
10
10
  def before_fork
11
11
  Resque.before_fork
12
12
  end
13
+
14
+ #Direct access to the after_fork proc
15
+ def after_fork
16
+ Resque.after_fork
17
+ end
13
18
 
14
19
  # Given a Ruby object, returns a string suitable for storage in a
15
20
  # queue.
@@ -1,3 +1,3 @@
1
1
  module Resque
2
- Version = '0.0.2'
2
+ Version = '0.0.3'
3
3
  end
data/lib/resque/worker.rb CHANGED
@@ -61,11 +61,6 @@ module Resque
61
61
  redis.sismember(:workers, worker_id)
62
62
  end
63
63
 
64
- #Sets the before_hook proc
65
- def self.before_fork=(before_fork)
66
- @@before_fork = before_fork
67
- end
68
-
69
64
  # Workers should be initialized with an array of string queue
70
65
  # names. The order is important: a Worker will check the first
71
66
  # queue given for a job. If none is found, it will check the
@@ -147,6 +142,7 @@ module Resque
147
142
  return unless job ||= reserve
148
143
 
149
144
  begin
145
+ call_after_fork
150
146
  working_on job
151
147
  job.perform
152
148
  rescue Object => e
@@ -317,6 +313,11 @@ module Resque
317
313
  before_fork.call if before_fork
318
314
  end
319
315
 
316
+ #Call any before_fork procs, if any
317
+ def call_after_fork
318
+ after_fork.call if after_fork
319
+ end
320
+
320
321
  # Unregisters ourself as a worker. Useful when shutting down.
321
322
  def unregister_worker
322
323
  redis.srem(:workers, self)
data/lib/resque.rb CHANGED
@@ -58,6 +58,16 @@ module Resque
58
58
  @before_fork
59
59
  end
60
60
 
61
+ #Set a proc that will be called after the worker forks
62
+ def after_fork=(after_fork)
63
+ @after_fork = after_fork
64
+ end
65
+
66
+ #Returns the after_fork proc
67
+ def after_fork
68
+ @after_fork
69
+ end
70
+
61
71
  def to_s
62
72
  "Resque Client connected to #{redis.server}"
63
73
  end
data/test/worker_test.rb CHANGED
@@ -119,7 +119,7 @@ context "Resque::Worker" do
119
119
  worker_id = Resque.workers[0].to_s
120
120
  Resque.remove_worker(worker_id)
121
121
  assert_equal [], Resque.workers
122
- end
122
+ end
123
123
  end
124
124
 
125
125
  test "records what it is working on" do
@@ -241,20 +241,29 @@ context "Resque::Worker" do
241
241
  assert_equal 1, Resque.info[:processed]
242
242
  end
243
243
 
244
- test "Will call a pre-fork proc when the worker starts if set in the initializer only once" do
244
+ test "Will call a before_fork proc when the worker starts if set in the initializer only once" do
245
+ Resque.redis.flush_all
245
246
  $BEFORE_FORK_CALLED = false
246
247
  Resque.before_fork = Proc.new { $BEFORE_FORK_CALLED = true }
247
- Resque::Worker.before_fork = Resque.before_fork
248
248
  workerA = Resque::Worker.new(:jobs)
249
249
  Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
250
-
250
+
251
251
  assert !$BEFORE_FORK_CALLED
252
252
  workerA.work(0)
253
253
  assert $BEFORE_FORK_CALLED
254
-
255
- $BEFORE_FORK_CALLED = false
254
+ Resque.before_fork = nil
255
+ end
256
+
257
+ test "Will call a after_fork proc after the worker has successfully forked" do
258
+ Resque.redis.flush_all
259
+ $AFTER_FORK_CALLED = false
260
+ Resque.after_fork = Proc.new { $AFTER_FORK_CALLED = true }
261
+ workerA = Resque::Worker.new(:jobs)
262
+
263
+ assert !$AFTER_FORK_CALLED
256
264
  Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
257
- assert !$BEFORE_FORK_CALLED
258
- Resque::Worker.send(:remove_class_variable, :@@before_fork)
265
+ workerA.work(0)
266
+ assert $AFTER_FORK_CALLED
267
+ Resque.after_fork = nil
259
268
  end
260
269
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scotttam-resque
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath, Scott Tamosunas