trinidad_threaded_resque_extension 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ module Trinidad
2
+ module Extensions
3
+ module ThreadedResque
4
+ class LifecycleListener
5
+ include Trinidad::Tomcat::LifecycleListener
6
+ attr_accessor :options, :workers, :threads
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ require @options[:setup] if @options[:setup]
11
+ end
12
+
13
+ def lifecycleEvent(event)
14
+ case event.type
15
+ when Trinidad::Tomcat::Lifecycle::BEFORE_START_EVENT
16
+ start_workers
17
+ when Trinidad::Tomcat::Lifecycle::BEFORE_STOP_EVENT
18
+ stop_workers
19
+ end
20
+ end
21
+
22
+ def start_workers
23
+ @workers = create_workers
24
+ @threads = create_threads
25
+ end
26
+
27
+ def stop_workers
28
+ @workers.each { |w| w.shutdown }
29
+ @threads.each { |t| t.join }
30
+ end
31
+
32
+ private
33
+ def create_workers
34
+ queues = @options[:queues] || { :all => 1 }
35
+ queues.map do |queue, count|
36
+ queue = '*' if queue.to_s == 'all'
37
+ count.to_i.times.map do
38
+ worker = Resque::Worker.new(queue)
39
+ worker.cant_fork = true # fork is a noop in jruby
40
+ worker
41
+ end
42
+ end.flatten
43
+ end
44
+
45
+ def create_threads
46
+ @workers.map do |w|
47
+ Thread.new do
48
+ w.work
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -6,5 +6,9 @@ module Resque
6
6
  alias :old_register_signal_handlers :register_signal_handlers
7
7
  def register_signal_handlers
8
8
  end
9
+
10
+ def to_s
11
+ @to_s ||= "#{hostname}:#{Process.pid}-#{Thread.current.object_id}:#{@queues.join(',')}"
12
+ end
9
13
  end
10
14
  end
@@ -1,3 +1,3 @@
1
1
  module TrinidadThreadedResqueExtension
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,45 +1,14 @@
1
1
  require 'resque'
2
2
  require 'trinidad'
3
3
  require_relative "trinidad_threaded_resque_extension/version"
4
+ require_relative "trinidad_threaded_resque_extension/lifecycle_listener"
4
5
  require_relative "trinidad_threaded_resque_extension/resque_disable_signal_handlers"
5
6
 
6
7
  module Trinidad
7
8
  module Extensions
8
- module ThreadedResque
9
- class ThreadedResqueLifecycleListener
10
- include Trinidad::Tomcat::LifecycleListener
11
- def initialize(options = {})
12
- @options = options
13
- require @options[:setup] if @options[:setup]
14
- end
15
-
16
- def lifecycleEvent(event)
17
- case event.type
18
- when Trinidad::Tomcat::Lifecycle::BEFORE_START_EVENT
19
- start_workers
20
- when Trinidad::Tomcat::Lifecycle::BEFORE_STOP_EVENT
21
- stop_workers
22
- end
23
- end
24
-
25
- def start_workers
26
- queues = @options[:queues] || { 'all' => 1 }
27
- @workers = queues.map do |queue, count|
28
- queue = '*' if queue == 'all'
29
- count.to_i.times.map do
30
- worker = Resque::Worker.new(queue)
31
- worker.cant_fork = true # fork is a noop in jruby
32
- Thread.new do
33
- worker.work
34
- end
35
- worker
36
- end
37
- end.flatten
38
- end
39
-
40
- def stop_workers
41
- @workers.each { |w| w.shutdown! }
42
- end
9
+ class ThreadedResqueServerExtension < ServerExtension
10
+ def configure(tomcat)
11
+ tomcat.host.add_lifecycle_listener(ThreadedResque::LifecycleListener.new(@options))
43
12
  end
44
13
  end
45
14
  end
@@ -1,35 +1,34 @@
1
1
  require './lib/trinidad_threaded_resque_extension'
2
2
  Resque.inline = true
3
3
 
4
- describe Trinidad::Extensions::ThreadedResque::ThreadedResqueLifecycleListener do
5
- subject { Trinidad::Extensions::ThreadedResque::ThreadedResqueLifecycleListener.new }
6
-
4
+ describe Trinidad::Extensions::ThreadedResque::LifecycleListener do
7
5
  it 'starts by default one worker' do
8
- workers = subject.start_workers
9
- workers.should have(1).things
6
+ subject.start_workers
7
+ subject.workers.should have(1).things
10
8
  end
11
9
 
12
10
  it 'can start many workers' do
13
- subject.instance_variable_set(:@options, {:queues => {"q1" => 2 }})
14
- workers = subject.start_workers
15
- workers.should have(2).things
16
- workers[0].queues.should == ['q1']
17
- workers[1].queues.should == ['q1']
11
+ subject.options = { :queues => { "q1" => 2 } }
12
+ subject.start_workers
13
+ subject.workers.map{ |w| w.queues }.flatten.count('q1').should == 2
18
14
  end
19
15
 
20
16
  it 'can start many workers for different queues' do
21
- subject.instance_variable_set(:@options, {:queues => {"q1" => 2, "q2" => 2 }})
22
- workers = subject.start_workers
23
- workers.should have(4).things
24
- workers[0].queues.should == ['q1']
25
- workers[1].queues.should == ['q1']
26
- workers[2].queues.should == ['q2']
27
- workers[3].queues.should == ['q2']
17
+ subject.options = { :queues => {"q1" => 2, "q2" => 2 } }
18
+ subject.start_workers
19
+ queues = subject.workers.map{ |w| w.queues }.flatten
20
+ queues.count('q1').should == 2
21
+ queues.count('q2').should == 2
28
22
  end
29
23
 
30
24
  it 'can shutdown workers' do
31
25
  subject.start_workers
32
- workers = subject.stop_workers
33
- workers[0].shutdown?.should == true
26
+ subject.stop_workers
27
+ subject.workers[0].shutdown?.should == true
28
+ subject.threads[0].stop?.should == true
29
+ end
30
+
31
+ after do
32
+ subject.stop_workers
34
33
  end
35
34
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: trinidad_threaded_resque_extension
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Carl Hörberg
@@ -79,6 +79,7 @@ files:
79
79
  - README.md
80
80
  - Rakefile
81
81
  - lib/trinidad_threaded_resque_extension.rb
82
+ - lib/trinidad_threaded_resque_extension/lifecycle_listener.rb
82
83
  - lib/trinidad_threaded_resque_extension/resque_disable_signal_handlers.rb
83
84
  - lib/trinidad_threaded_resque_extension/version.rb
84
85
  - spec/resque_extension_spec.rb