resque 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of resque might be problematic. Click here for more details.

@@ -1,23 +1,28 @@
1
1
  * Chris Wanstrath
2
+ * gravis
2
3
  * John Barnette
4
+ * scotttam
3
5
  * Aaron Quint
4
6
  * Adam Cooke
7
+ * Ashley Martens
8
+ * Jason Amster
5
9
  * Mike Mangino
6
10
  * Rob Hanlon
7
- * Jason Amster
8
11
  * jgeiger
9
- * Daniel Ceballos
10
- * Matt Duncan
11
12
  * Roman Heinrich
13
+ * Thibaut Barrère
14
+ * Matt Duncan
15
+ * Daniel Ceballos
12
16
  * Ben VandenBos
13
17
  * Christos Trochalakis
14
- * Matt Palmer
15
- * Michael Dwan
16
- * Brian P O'Rourke
17
- * Karel Minarik
18
- * Arthur Zapparoli
18
+ * Roland Moriz
19
19
  * Simon Rozet
20
- * Dave Hoover
20
+ * Brian P O'Rourke
21
21
  * PJ Hyett
22
+ * Ben Marini
23
+ * Karel Minarik
22
24
  * Masatomo Nakano
23
- * gravis
25
+ * Matt Palmer
26
+ * Michael Dwan
27
+ * Dave Hoover
28
+ * Arthur Zapparoli
data/HISTORY.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 1.6.0 (2010-03-09)
2
+
3
+ * Added `before_first_fork`, `before_fork`, and `after_fork` hooks.
4
+ * Hoptoad: Added server_environment config setting
5
+ * Hoptoad bugfix: Don't depend on RAILS_ROOT
6
+ * 1.8.6 compat fixes
7
+
1
8
  ## 1.5.2 (2010-03-03)
2
9
 
3
10
  * Bugfix: JSON check was crazy.
@@ -648,6 +648,36 @@ this way we can tell our Sinatra app about the config file:
648
648
 
649
649
  Now everyone is on the same page.
650
650
 
651
+ If you wish to have a Proc called before the worker forks for the
652
+ first time, you can add it in the initializer like so:
653
+
654
+ Resque.before_first_fork do
655
+ puts "CALL ME ONCE BEFORE THE WORKER FORKS THE FIRST TIME"
656
+ end
657
+
658
+ You can also run a hook before _every_ fork:
659
+
660
+ Resque.before_fork do |job|
661
+ puts "CALL ME ONCE BEFORE THE WORKER FORKS THE FIRST TIME"
662
+ end
663
+
664
+ The `before_fork` hook will be run in the **parent** process. So, be
665
+ careful - any changes you make will be permanent for the lifespan of
666
+ the worker.
667
+
668
+ And after forking:
669
+
670
+ Resque.after_fork do |job|
671
+ puts "CALL ME ONCE BEFORE THE WORKER FORKS THE FIRST TIME"
672
+ end
673
+
674
+ The `after_fork` hook will be run in the child process and is passed
675
+ the current job. Any changes you make, therefor, will only live as
676
+ long as the job currently being processes.
677
+
678
+ All hooks can also be set using a setter, e.g.
679
+
680
+ Resque.after_fork = proc { puts "called" }
651
681
 
652
682
  Namespaces
653
683
  ----------
@@ -729,6 +759,9 @@ Once you've made your great commits:
729
759
  4. Create an [Issue][2] with a link to your branch
730
760
  5. That's it!
731
761
 
762
+ You might want to checkout our [Contributing][cb] wiki page for information
763
+ on coding standards, new features, etc.
764
+
732
765
 
733
766
  Mailing List
734
767
  ------------
@@ -764,3 +797,4 @@ Chris Wanstrath :: chris@ozmm.org :: @defunkt
764
797
  [2]: http://github.com/defunkt/resque/issues
765
798
  [sv]: http://semver.org/
766
799
  [rs]: http://github.com/defunkt/redis-namespace
800
+ [cb]: http://wiki.github.com/defunkt/resque/contributing
@@ -48,6 +48,53 @@ module Resque
48
48
  self.redis
49
49
  end
50
50
 
51
+ # The `before_first_fork` hook will be run in the **parent** process
52
+ # only once, before forking to run the first job. Be careful- any
53
+ # changes you make will be permanent for the lifespan of the
54
+ # worker.
55
+ #
56
+ # Call with a block to set the hook.
57
+ # Call with no arguments to return the hook.
58
+ def before_first_fork(&block)
59
+ block ? (@before_first_fork = block) : @before_first_fork
60
+ end
61
+
62
+ # Set a proc that will be called in the parent process before the
63
+ # worker forks for the first time.
64
+ def before_first_fork=(before_first_fork)
65
+ @before_first_fork = before_first_fork
66
+ end
67
+
68
+ # The `before_fork` hook will be run in the **parent** process
69
+ # before every job, so be careful- any changes you make will be
70
+ # permanent for the lifespan of the worker.
71
+ #
72
+ # Call with a block to set the hook.
73
+ # Call with no arguments to return the hook.
74
+ def before_fork(&block)
75
+ block ? (@before_fork = block) : @before_fork
76
+ end
77
+
78
+ # Set the before_fork proc.
79
+ def before_fork=(before_fork)
80
+ @before_fork = before_fork
81
+ end
82
+
83
+ # The `after_fork` hook will be run in the child process and is passed
84
+ # the current job. Any changes you make, therefor, will only live as
85
+ # long as the job currently being processes.
86
+ #
87
+ # Call with a block to set the hook.
88
+ # Call with no arguments to return the hook.
89
+ def after_fork(&block)
90
+ block ? (@after_fork = block) : @after_fork
91
+ end
92
+
93
+ # Set the after_fork proc.
94
+ def after_fork=(after_fork)
95
+ @after_fork = after_fork
96
+ end
97
+
51
98
  def to_s
52
99
  "Resque Client connected to #{redis.server}"
53
100
  end
@@ -15,6 +15,9 @@ module Resque
15
15
  # # optional proxy support
16
16
  # config.proxy_host = 'x.y.z.t'
17
17
  # config.proxy_port = 8080
18
+ #
19
+ # # server env support, defaults to RAILS_ENV or RACK_ENV
20
+ # config.server_environment = "test"
18
21
  # end
19
22
  class Hoptoad < Base
20
23
  # From the hoptoad plugin
@@ -22,6 +25,7 @@ module Resque
22
25
 
23
26
  class << self
24
27
  attr_accessor :secure, :api_key, :proxy_host, :proxy_port
28
+ attr_accessor :server_environment
25
29
  end
26
30
 
27
31
  def self.count
@@ -96,7 +100,7 @@ module Resque
96
100
  end
97
101
  end
98
102
  x.tag!("server-environment") do
99
- x.tag!("environment-name",RAILS_ENV)
103
+ x.tag!("environment-name",server_environment)
100
104
  end
101
105
 
102
106
  end
@@ -105,7 +109,7 @@ module Resque
105
109
  def fill_in_backtrace_lines(x)
106
110
  exception.backtrace.each do |unparsed_line|
107
111
  _, file, number, method = unparsed_line.match(INPUT_FORMAT).to_a
108
- x.line :file=>file,:number=>number
112
+ x.line :file => file,:number => number
109
113
  end
110
114
  end
111
115
 
@@ -116,6 +120,11 @@ module Resque
116
120
  def api_key
117
121
  self.class.api_key
118
122
  end
123
+
124
+ def server_environment
125
+ return self.class.server_environment if self.class.server_environment
126
+ defined?(RAILS_ENV) ? RAILS_ENV : (ENV['RACK_ENV'] || 'development')
127
+ end
119
128
  end
120
129
  end
121
130
  end
@@ -33,7 +33,7 @@ module Resque
33
33
  end
34
34
 
35
35
  def class_if_current(path = '')
36
- 'class="current"' if current_page.start_with?(path.to_s)
36
+ 'class="current"' if current_page[0, path.size] == path
37
37
  end
38
38
 
39
39
  def tab(name)
@@ -1,3 +1,3 @@
1
1
  module Resque
2
- Version = '1.5.2'
2
+ Version = '1.6.0'
3
3
  end
@@ -112,6 +112,7 @@ module Resque
112
112
 
113
113
  if not @paused and job = reserve
114
114
  log "got: #{job.inspect}"
115
+ run_hook :before_fork
115
116
 
116
117
  if @child = fork
117
118
  rand # Reseeding
@@ -142,6 +143,7 @@ module Resque
142
143
  return unless job ||= reserve
143
144
 
144
145
  begin
146
+ run_hook :after_fork, job
145
147
  working_on job
146
148
  job.perform
147
149
  rescue Object => e
@@ -202,6 +204,7 @@ module Resque
202
204
  enable_gc_optimizations
203
205
  register_signal_handlers
204
206
  prune_dead_workers
207
+ run_hook :before_first_fork
205
208
  register_worker
206
209
  end
207
210
 
@@ -306,6 +309,16 @@ module Resque
306
309
  started!
307
310
  end
308
311
 
312
+ # Runs a named hook, passing along any arguments.
313
+ def run_hook(name, *args)
314
+ return unless hook = Resque.send(name)
315
+ msg = "Running #{name} hook"
316
+ msg << " with #{args.inspect}" if args.any?
317
+ log msg
318
+
319
+ args.any? ? hook.call(*args) : hook.call
320
+ end
321
+
309
322
  # Unregisters ourself as a worker. Useful when shutting down.
310
323
  def unregister_worker
311
324
  redis.srem(:workers, self)
@@ -4,6 +4,10 @@ context "Resque::Worker" do
4
4
  setup do
5
5
  Resque.redis.flush_all
6
6
 
7
+ Resque.before_first_fork = nil
8
+ Resque.before_fork = nil
9
+ Resque.after_fork = nil
10
+
7
11
  @worker = Resque::Worker.new(:jobs)
8
12
  Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
9
13
  end
@@ -119,7 +123,7 @@ context "Resque::Worker" do
119
123
  worker_id = Resque.workers[0].to_s
120
124
  Resque.remove_worker(worker_id)
121
125
  assert_equal [], Resque.workers
122
- end
126
+ end
123
127
  end
124
128
 
125
129
  test "records what it is working on" do
@@ -240,4 +244,45 @@ context "Resque::Worker" do
240
244
  @worker.work(0)
241
245
  assert_equal 1, Resque.info[:processed]
242
246
  end
247
+
248
+ test "Will call a before_first_fork hook only once" do
249
+ Resque.redis.flush_all
250
+ $BEFORE_FORK_CALLED = 0
251
+ Resque.before_first_fork = Proc.new { $BEFORE_FORK_CALLED += 1 }
252
+ workerA = Resque::Worker.new(:jobs)
253
+ Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
254
+
255
+ assert_equal 0, $BEFORE_FORK_CALLED
256
+
257
+ workerA.work(0)
258
+ assert_equal 1, $BEFORE_FORK_CALLED
259
+
260
+ # TODO: Verify it's only run once. Not easy.
261
+ # workerA.work(0)
262
+ # assert_equal 1, $BEFORE_FORK_CALLED
263
+ end
264
+
265
+ test "Will call a before_fork hook before forking" do
266
+ Resque.redis.flush_all
267
+ $BEFORE_FORK_CALLED = false
268
+ Resque.before_fork = Proc.new { $BEFORE_FORK_CALLED = true }
269
+ workerA = Resque::Worker.new(:jobs)
270
+
271
+ assert !$BEFORE_FORK_CALLED
272
+ Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
273
+ workerA.work(0)
274
+ assert $BEFORE_FORK_CALLED
275
+ end
276
+
277
+ test "Will call an after_fork hook after forking" do
278
+ Resque.redis.flush_all
279
+ $AFTER_FORK_CALLED = false
280
+ Resque.after_fork = Proc.new { $AFTER_FORK_CALLED = true }
281
+ workerA = Resque::Worker.new(:jobs)
282
+
283
+ assert !$AFTER_FORK_CALLED
284
+ Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
285
+ workerA.work(0)
286
+ assert $AFTER_FORK_CALLED
287
+ end
243
288
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-03 00:00:00 -08:00
12
+ date: 2010-03-09 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency