trinidad_threaded_resque_extension 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,17 +18,17 @@ Then configure it via config/trinidad.rb:
18
18
 
19
19
  require 'bundler/setup'
20
20
  Trinidad.configure |config|
21
+ config.jruby_max_runtimes = 1
21
22
  ...
22
23
  config.extensions = {
23
- threaded_resque = {
24
- setup: './config/setup_resque_workers', # will be required before starting the workers
24
+ threaded_resque: {
25
+ require: './config/setup_resque_workers', # will be required before starting the workers, setup Resque and require the jobs here
25
26
  queues: {
26
- # syntax: "queue name: number of workers"
27
- # the "all" queue is an alias for "*"
28
- # default is "all: 1"
29
- all: 3,
30
- high: 2,
31
- low: 1
27
+ # hash key is queue name
28
+ # hash value is the number of workers consuming that queue
29
+ # default: '*' => 1
30
+ io_bound: 20,
31
+ cpu_bound: 3,
32
32
  }
33
33
  }
34
34
  }
@@ -37,7 +37,7 @@ Then configure it via config/trinidad.rb:
37
37
 
38
38
  Start trinidad and enjoy the low memory usage while having high concurrency.
39
39
 
40
- Note that becasue JRuby can't fork you want to have well behaved workers, watch out for memory leakage..
40
+ Note that becasue JRuby can't fork you want to have well behaved workers, watch out for memory leakage. Naturally your workers have to be thread safe.
41
41
 
42
42
  ## Contributing
43
43
 
@@ -46,3 +46,4 @@ Note that becasue JRuby can't fork you want to have well behaved workers, watch
46
46
  3. Commit your changes (`git commit -am 'Added some feature'`)
47
47
  4. Push to the branch (`git push origin my-new-feature`)
48
48
  5. Create new Pull Request
49
+
@@ -2,7 +2,6 @@ require 'resque'
2
2
  require 'trinidad'
3
3
  require_relative "trinidad_threaded_resque_extension/version"
4
4
  require_relative "trinidad_threaded_resque_extension/lifecycle_listener"
5
- require_relative "trinidad_threaded_resque_extension/resque_disable_signal_handlers"
6
5
 
7
6
  module Trinidad
8
7
  module Extensions
@@ -1,3 +1,5 @@
1
+ require_relative "worker"
2
+
1
3
  module Trinidad
2
4
  module Extensions
3
5
  module ThreadedResque
@@ -7,7 +9,6 @@ module Trinidad
7
9
 
8
10
  def initialize(options = {})
9
11
  @options = options
10
- require @options[:setup] if @options[:setup]
11
12
  end
12
13
 
13
14
  def lifecycleEvent(event)
@@ -20,24 +21,30 @@ module Trinidad
20
21
  end
21
22
 
22
23
  def start_workers
24
+ pre_require
23
25
  @workers = create_workers
24
26
  @threads = create_threads
25
27
  end
26
28
 
27
29
  def stop_workers
28
- @workers.each { |w| w.shutdown }
29
- @threads.each { |t| t.join }
30
+ @workers.each { |w| w.shutdown } if @workers
31
+ @threads.each { |t| t.join } if @threads
30
32
  end
31
33
 
32
34
  private
35
+ def pre_require
36
+ if @options[:require]
37
+ require @options[:require]
38
+ else
39
+ raise "You probably want to require something before starting the workers"
40
+ end
41
+ end
42
+
33
43
  def create_workers
34
- queues = @options[:queues] || { :all => 1 }
44
+ queues = @options[:queues] || { '*' => 1 }
35
45
  queues.map do |queue, count|
36
- queue = '*' if queue.to_s == 'all'
37
46
  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
47
+ ThreadedResque::Worker.new(queue.to_s)
41
48
  end
42
49
  end.flatten
43
50
  end
@@ -1,3 +1,3 @@
1
1
  module TrinidadThreadedResqueExtension
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,23 @@
1
+ require 'resque'
2
+
3
+ module Trinidad
4
+ module Extensions
5
+ module ThreadedResque
6
+ class Worker < Resque::Worker
7
+ def initialize(*queues)
8
+ super(*queues)
9
+ @cant_fork = true
10
+ end
11
+
12
+ # we can't let resque trap signal
13
+ def register_signal_handlers
14
+ end
15
+
16
+ #override to_s and separate different workers by there thread id
17
+ def to_s
18
+ @to_s ||= "#{hostname}:#{Process.pid}-#{Thread.current.object_id}:#{@queues.join(',')}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
data/spec/dummy.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Dummy
2
+ end
@@ -1,34 +1,58 @@
1
1
  require './lib/trinidad_threaded_resque_extension'
2
- Resque.inline = true
3
2
 
4
3
  describe Trinidad::Extensions::ThreadedResque::LifecycleListener do
5
- it 'starts by default one worker' do
6
- subject.start_workers
7
- subject.workers.should have(1).things
8
- end
4
+ let(:opts) { { :require => './spec/dummy' } }
5
+ subject { Trinidad::Extensions::ThreadedResque::LifecycleListener.new opts }
9
6
 
10
- it 'can start many workers' do
11
- subject.options = { :queues => { "q1" => 2 } }
12
- subject.start_workers
13
- subject.workers.map{ |w| w.queues }.flatten.count('q1').should == 2
14
- end
7
+ context '#start_workers' do
8
+ it 'starts by default one worker' do
9
+ subject.start_workers
10
+ subject.workers.should have(1).things
11
+ end
15
12
 
16
- it 'can start many workers for different queues' do
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
22
- end
13
+ it 'can start many workers' do
14
+ subject.options[:queues] = { :q1 => 2 }
15
+ subject.start_workers
16
+ subject.workers.map{ |w| w.queues }.flatten.count('q1').should == 2
17
+ end
18
+
19
+ it 'can start many workers for different queues' do
20
+ subject.options[:queues] = { :q1 => 2, :q2 => 2 }
21
+ subject.start_workers
22
+ queues = subject.workers.map{ |w| w.queues }.flatten
23
+ queues.count('q1').should == 2
24
+ queues.count('q2').should == 2
25
+ end
26
+
27
+ it 'requires the "require" file' do
28
+ subject.start_workers
29
+ defined?(Dummy).should_not be_nil
30
+ end
23
31
 
24
- it 'can shutdown workers' do
25
- subject.start_workers
26
- subject.stop_workers
27
- subject.workers[0].shutdown?.should == true
28
- subject.threads[0].stop?.should == true
32
+ it 'raises if no "require" file is configured' do
33
+ subject.options[:require] = nil
34
+ lambda {
35
+ subject.start_workers
36
+ }.should raise_error RuntimeError
37
+ end
38
+
39
+ after do
40
+ subject.stop_workers
41
+ end
29
42
  end
30
-
31
- after do
32
- subject.stop_workers
43
+
44
+ context '#stop_workers' do
45
+ before do
46
+ subject.start_workers
47
+ subject.stop_workers
48
+ end
49
+
50
+ it 'shutdowns workers' do
51
+ subject.workers[0].shutdown?.should be_true
52
+ end
53
+
54
+ it 'stops threads' do
55
+ subject.threads[0].stop?.should be_true
56
+ end
33
57
  end
34
58
  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.2
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Carl Hörberg
@@ -80,8 +80,9 @@ files:
80
80
  - Rakefile
81
81
  - lib/trinidad_threaded_resque_extension.rb
82
82
  - lib/trinidad_threaded_resque_extension/lifecycle_listener.rb
83
- - lib/trinidad_threaded_resque_extension/resque_disable_signal_handlers.rb
84
83
  - lib/trinidad_threaded_resque_extension/version.rb
84
+ - lib/trinidad_threaded_resque_extension/worker.rb
85
+ - spec/dummy.rb
85
86
  - spec/resque_extension_spec.rb
86
87
  - trinidad_threaded_resque_extension.gemspec
87
88
  homepage: https://github.com/carlhoerberg/trinidad_threaded_resque_extension
@@ -109,5 +110,6 @@ signing_key:
109
110
  specification_version: 3
110
111
  summary: Runs Resque workers, threaded, within the Trinidad application server
111
112
  test_files:
113
+ - spec/dummy.rb
112
114
  - spec/resque_extension_spec.rb
113
115
  ...
@@ -1,14 +0,0 @@
1
- require 'resque'
2
-
3
- module Resque
4
- class Worker
5
- # if we let resque trap signals trinidad cannot ever be stopped
6
- alias :old_register_signal_handlers :register_signal_handlers
7
- def register_signal_handlers
8
- end
9
-
10
- def to_s
11
- @to_s ||= "#{hostname}:#{Process.pid}-#{Thread.current.object_id}:#{@queues.join(',')}"
12
- end
13
- end
14
- end