resque-multi-job-forks 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  If you have very frequent and fast resque jobs, the overhead of forking and running your after_fork hook, might get too big. Using this resque plugin, you can have your workers perform more than one job, before terminating.
4
4
 
5
- You simple specify the number of jobs to perform using the JOBS_PER_FORK environment variable:
5
+ You simply specify the number of minutes you want each fork to run using the MINUTES_PER_FORK environment variable:
6
6
 
7
- QUEUE=* JOBS_PER_FORK=100 rake resque:work
7
+ QUEUE=* MINUTES_PER_FORK=5 rake resque:work
8
8
 
9
- This will have each fork process 100 jobs, before terminating. If less than 100 jobs are enqueued, the fork will terminate when the queue is empty.
9
+ This will have each fork process jobs for 5 minutes, before terminating.
10
10
 
11
11
  This plugin also defines a new hook, that gets called right before the fork terminates:
12
12
 
data/Rakefile CHANGED
@@ -5,14 +5,14 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "resque-multi-job-forks"
8
- gem.version = "0.1.2"
8
+ gem.version = "0.2.0"
9
9
  gem.summary = %Q{Have your resque workers process more that one job}
10
10
  gem.description = %Q{When your resque jobs are frequent and fast,
11
11
  the overhead of forking and running your after_fork might get too big.}
12
12
  gem.email = "mick@staugaard.com"
13
13
  gem.homepage = "http://github.com/staugaard/resque-multi-job-forks"
14
14
  gem.authors = ["Mick Staugaard"]
15
- gem.add_dependency "resque", ">= 1.6.0", "< 1.8.0"
15
+ gem.add_dependency "resque", "< 1.8.0"
16
16
  end
17
17
  Jeweler::GemcutterTasks.new
18
18
  rescue LoadError
@@ -21,26 +21,36 @@ module Resque
21
21
  attr_reader :jobs_processed
22
22
 
23
23
  unless method_defined?(:done_working_without_multi_job_forks)
24
+ def process_with_multi_job_forks(job = nil)
25
+ @jobs_processed ||= 0
26
+ @kill_fork_at ||= Time.now.to_i + (ENV['MINUTES_PER_FORK'].to_i * 60)
27
+ process_without_multi_job_forks(job)
28
+ end
29
+ alias_method :process_without_multi_job_forks, :process
30
+ alias_method :process, :process_with_multi_job_forks
31
+
24
32
  def done_working_with_multi_job_forks
25
33
  done_working_without_multi_job_forks
26
34
 
27
- self.jobs_per_fork ||= [ ENV['JOBS_PER_FORK'].to_i, 1 ].max
28
- @jobs_processed ||= 0
29
-
30
35
  @jobs_processed += 1
31
36
 
32
37
  if @jobs_processed == 1
33
38
  old_after_fork = Resque.after_fork
34
39
  Resque.after_fork = nil
35
-
36
- while @jobs_processed < jobs_per_fork && job = reserve
37
- process(job)
40
+
41
+ while Time.now.to_i < @kill_fork_at
42
+ if job = reserve
43
+ process(job)
44
+ else
45
+ sleep(1)
46
+ end
38
47
  end
39
48
 
40
49
  Resque.after_fork = old_after_fork
41
50
 
42
51
  run_hook :before_child_exit, self
43
- @jobs_processed = 0
52
+ @jobs_processed = nil
53
+ @kill_fork_at = nil
44
54
  end
45
55
  end
46
56
  alias_method :done_working_without_multi_job_forks, :done_working
@@ -3,6 +3,8 @@ require 'helper'
3
3
  class SomeJob
4
4
  def self.perform(i)
5
5
  $SEQUENCE << "work_#{i}".to_sym
6
+ puts 'working...'
7
+ sleep(25)
6
8
  end
7
9
  end
8
10
 
@@ -22,16 +24,17 @@ class TestResqueMultiJobForks < Test::Unit::TestCase
22
24
  def test_sequence_of_events
23
25
  Resque.redis.flush_all
24
26
 
25
- ENV['JOBS_PER_FORK'] = '2'
27
+ ENV['MINUTES_PER_FORK'] = '1'
26
28
 
27
29
  worker = Resque::Worker.new(:jobs)
28
30
 
29
31
  Resque::Job.create(:jobs, SomeJob, 1)
30
32
  Resque::Job.create(:jobs, SomeJob, 2)
31
33
  Resque::Job.create(:jobs, SomeJob, 3)
34
+ Resque::Job.create(:jobs, SomeJob, 4)
32
35
 
33
36
  worker.work(0)
34
37
 
35
- assert_equal([:after_fork, :work_1, :work_2, :before_child_exit_2, :after_fork, :work_3, :before_child_exit_1], $SEQUENCE)
38
+ assert_equal([:after_fork, :work_1, :work_2, :work_3, :before_child_exit_3, :after_fork, :work_4, :before_child_exit_1], $SEQUENCE)
36
39
  end
37
40
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
7
  - 2
9
- version: 0.1.2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mick Staugaard
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-07 00:00:00 -07:00
17
+ date: 2010-04-22 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -22,13 +22,6 @@ dependencies:
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 6
30
- - 0
31
- version: 1.6.0
32
25
  - - <
33
26
  - !ruby/object:Gem::Version
34
27
  segments: