trinidad_worker_extension 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,15 +27,16 @@ module Trinidad
27
27
 
28
28
  def configure_worker(context, name, config)
29
29
  config = config.dup
30
+ params = {}
30
31
  if script = config.delete(:script)
31
- context.add_parameter 'jruby.worker.script', script
32
+ params['jruby.worker.script'] = script
32
33
  end
33
34
  if script_path = config.delete(:script_path)
34
- context.add_parameter 'jruby.worker.script.path', script_path
35
+ params['jruby.worker.script.path'] = script_path
35
36
  end
36
37
  if script.nil? && script_path.nil?
37
38
  if name
38
- context.add_parameter('jruby.worker', name.to_s)
39
+ params['jruby.worker'] = name.to_s
39
40
  else
40
41
  context.logger.warn "not-starting any workers due missing configuration " +
41
42
  "either set :script or :script_path if you're not using a built-in worker"
@@ -45,15 +46,15 @@ module Trinidad
45
46
  config.each do |key, value|
46
47
  case key.to_s
47
48
  when 'thread_count'
48
- context.add_parameter('jruby.worker.thread.count', value.to_s)
49
+ params['jruby.worker.thread.count'] = value.to_s
49
50
  when 'thread_priority'
50
- context.add_parameter('jruby.worker.thread.priority', value.to_s)
51
+ params['jruby.worker.thread.priority'] = value.to_s
51
52
  else
52
53
  value = value.join(',') if value.respond_to?(:join)
53
- context.add_parameter(key.to_s, value.to_s)
54
+ params[key.to_s] = value.to_s
54
55
  end
55
56
  end
56
- context.add_lifecycle_listener listener = WorkerLifecycle.new
57
+ context.add_lifecycle_listener listener = WorkerLifecycle.new(params)
57
58
  listener
58
59
  end
59
60
 
@@ -61,15 +62,56 @@ module Trinidad
61
62
 
62
63
  class WorkerLifecycle < Trinidad::Lifecycle::Base
63
64
 
65
+ attr_reader :context_parameters
66
+
67
+ def initialize(params)
68
+ @context_parameters = params || {}
69
+ if @context_parameters.empty?
70
+ raise ArgumentError, "no context parameters"
71
+ end
72
+ end
73
+
64
74
  def configure_start(event)
65
75
  context = event.lifecycle
66
- jar_file = java.io.File.new JRuby::Rack::Worker::JAR_PATH
67
- context.loader.class_loader.addURL jar_file.to_url
76
+ add_context_parameters(context)
77
+ add_class_loader_jar_url(context)
68
78
  # NOTE: it's important for this listener to be added after
69
79
  # the Rack setup as it expectd to find the RackFactory ...
70
80
  # that's why we hook into #configure_start which happens
71
81
  # right after #before_start but before the actual #start !
72
- context.add_application_listener CONTEXT_LISTENER
82
+ add_application_listener(context)
83
+ end
84
+
85
+ protected
86
+
87
+ def add_context_parameters(context)
88
+ app_params = context.find_application_parameters
89
+ context_parameters.each do |name, value|
90
+ if app_param = app_params.find { |param| param.name == name }
91
+ app_param.value = value
92
+ else
93
+ # a "better" context.add_parameter(name, value) :
94
+ app_param = Trinidad::Tomcat::ApplicationParameter.new
95
+ app_param.name = name; app_param.value = value
96
+ app_param.override = false # confusing to override in web.xml
97
+ context.add_application_parameter app_param
98
+ end
99
+ end
100
+ end
101
+
102
+ def add_class_loader_jar_url(context)
103
+ jar_file = java.io.File.new JRuby::Rack::Worker::JAR_PATH
104
+ class_loader = context.loader.class_loader
105
+ unless class_loader.getURLs.include?(jar_file.to_url)
106
+ class_loader.addURL jar_file.to_url
107
+ end
108
+ end
109
+
110
+ def add_application_listener(context)
111
+ listener = CONTEXT_LISTENER
112
+ unless context.find_application_listeners.include?(listener)
113
+ context.add_application_listener listener
114
+ end
73
115
  end
74
116
 
75
117
  end
@@ -1,7 +1,7 @@
1
1
  module Trinidad
2
2
  module Extensions
3
3
  module Worker
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
6
6
  end
7
7
  end
data/spec/app/config.ru CHANGED
@@ -1,2 +1,9 @@
1
- use Rack::CommonLogger
2
- run Proc.new { [200, {'Content-Type' => 'text/plain'}, 'OK'] }
1
+ # make sure resque can be loaded :
2
+ begin
3
+ require 'resque'
4
+ rescue LoadError => e
5
+ require('rubygems') && retry; raise e
6
+ end
7
+ # and rackup a simple app :
8
+ use Rack::CommonLogger
9
+ run Proc.new { [200, {'Content-Type' => 'text/plain'}, 'OK'] }
@@ -12,13 +12,14 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
12
12
 
13
13
  Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
14
14
 
15
- params = parameters(context)
15
+ it_includes_worker_lifecycle_listener(context)
16
+
17
+ listener = worker_lifecycle_listener_for(context)
18
+ params = listener.context_parameters
16
19
  expect( params['jruby.worker'] ).to eql 'delayed_job'
17
20
  expect( params['jruby.worker.thread.count'] ).to eql '2'
18
21
  expect( params['READ_AHEAD'] ).to eql '3'
19
22
  expect( params['SLEEP_DELAY'] ).to eql '3.0'
20
-
21
- it_includes_worker_lifecycle_listener(context)
22
23
  end
23
24
 
24
25
  it "configures (resque) worker" do
@@ -27,17 +28,18 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
27
28
 
28
29
  Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
29
30
 
30
- params = parameters(context)
31
+ it_includes_worker_lifecycle_listener(context)
32
+
33
+ listener = worker_lifecycle_listener_for(context)
34
+ params = listener.context_parameters
31
35
  expect( params['jruby.worker'] ).to eql 'resque'
32
36
  expect( params['jruby.worker.thread.priority'] ).to eql 'MIN'
33
37
  expect( params['QUEUES'] ).to eql 'low,normal'
34
38
  expect( params['INTERVAL'] ).to eql '1.5'
35
39
  expect( params['VERBOSE'] ).to eql 'true'
36
-
37
- it_includes_worker_lifecycle_listener(context)
38
40
  end
39
41
 
40
- describe 'WorkerLifecycle' do
42
+ describe 'WorkerLifecycle', :integration => true do
41
43
 
42
44
  before do
43
45
  Trinidad.configure! { load File.join(APP_DIR, 'trinidad.rb') }
@@ -54,17 +56,31 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
54
56
  @context.start
55
57
  end
56
58
 
57
- it "configures worker context listener" do
59
+ it "configures worker context listener and init params" do
58
60
  class_name = 'org.kares.jruby.rack.WorkerContextListener'
59
61
  expect( @context.find_application_listeners ).to include class_name
62
+ expect( @context.servlet_context.get_init_parameter('jruby.worker') ).to eql 'resque'
63
+ expect( @context.servlet_context.get_init_parameter('QUEUES') ).to eql 'low,normal'
64
+ end
65
+
66
+ it "re-configures on reload" do
67
+ @context.reload
68
+ class_name = 'org.kares.jruby.rack.WorkerContextListener'
69
+ expect( @context.find_application_listeners ).to include class_name
70
+ expect( @context.servlet_context.get_init_parameter('jruby.worker') ).to eql 'resque'
71
+ expect( @context.servlet_context.get_init_parameter('QUEUES') ).to eql 'low,normal'
60
72
  end
61
73
 
62
74
  end
63
75
 
64
76
  def it_includes_worker_lifecycle_listener(context)
77
+ expect( worker_lifecycle_listener_for(context) ).to_not be nil
78
+ end
79
+
80
+ def worker_lifecycle_listener_for(context)
65
81
  listeners = context.find_lifecycle_listeners
66
82
  klass = Trinidad::Extensions::WorkerWebAppExtension::WorkerLifecycle
67
- expect( listeners.find { |l| l.is_a?(klass) } ).to_not be nil
83
+ listeners.find { |l| l.is_a?(klass) }
68
84
  end
69
85
 
70
86
  protected
metadata CHANGED
@@ -1,161 +1,137 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: trinidad_worker_extension
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
6
  platform: ruby
7
- authors:
8
- - Karol Bucek
9
- autorequire:
7
+ authors:
8
+ - Karol Bucek
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-25 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: trinidad
16
- version_requirements: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ! '>='
19
- - !ruby/object:Gem::Version
20
- version: 1.4.1
21
- none: false
22
- requirement: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ! '>='
25
- - !ruby/object:Gem::Version
26
- version: 1.4.1
27
- none: false
28
- prerelease: false
29
- type: :runtime
30
- - !ruby/object:Gem::Dependency
31
- name: jruby-rack-worker
32
- version_requirements: !ruby/object:Gem::Requirement
33
- requirements:
34
- - - ! '>='
35
- - !ruby/object:Gem::Version
36
- version: '0.7'
37
- none: false
38
- requirement: !ruby/object:Gem::Requirement
39
- requirements:
40
- - - ! '>='
41
- - !ruby/object:Gem::Version
42
- version: '0.7'
43
- none: false
44
- prerelease: false
45
- type: :runtime
46
- - !ruby/object:Gem::Dependency
47
- name: rspec
48
- version_requirements: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - ~>
51
- - !ruby/object:Gem::Version
52
- version: '2.10'
53
- none: false
54
- requirement: !ruby/object:Gem::Requirement
55
- requirements:
56
- - - ~>
57
- - !ruby/object:Gem::Version
58
- version: '2.10'
59
- none: false
60
- prerelease: false
61
- type: :development
62
- - !ruby/object:Gem::Dependency
63
- name: mocha
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ! '>='
67
- - !ruby/object:Gem::Version
68
- version: !binary |-
69
- MA==
70
- none: false
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ! '>='
74
- - !ruby/object:Gem::Version
75
- version: !binary |-
76
- MA==
77
- none: false
78
- prerelease: false
79
- type: :development
80
- - !ruby/object:Gem::Dependency
81
- name: rake
82
- version_requirements: !ruby/object:Gem::Requirement
83
- requirements:
84
- - - ! '>='
85
- - !ruby/object:Gem::Version
86
- version: !binary |-
87
- MA==
88
- none: false
89
- requirement: !ruby/object:Gem::Requirement
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: !binary |-
94
- MA==
95
- none: false
96
- prerelease: false
97
- type: :development
98
- description: ! "Trinidad background worker extension built upon \n JRuby-Rack-Worker\
99
- \ which provides threaded workers along side your (JRuby-Rack)\n application. Includes\
100
- \ (thread-safe) out-of-the-box implementations for popular\n worker libraries such\
101
- \ as Resque and Delayed::Job but customized 'daemon' \n scripts can be used as\
102
- \ well."
103
- email:
104
- - self@kares.org
12
+
13
+ date: 2012-10-29 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: trinidad
17
+ version_requirements: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.1
23
+ requirement: *id001
24
+ prerelease: false
25
+ type: :runtime
26
+ - !ruby/object:Gem::Dependency
27
+ name: jruby-rack-worker
28
+ version_requirements: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0.7"
34
+ requirement: *id002
35
+ prerelease: false
36
+ type: :runtime
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ version_requirements: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: "2.10"
45
+ requirement: *id003
46
+ prerelease: false
47
+ type: :development
48
+ - !ruby/object:Gem::Dependency
49
+ name: mocha
50
+ version_requirements: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ requirement: *id004
57
+ prerelease: false
58
+ type: :development
59
+ - !ruby/object:Gem::Dependency
60
+ name: rake
61
+ version_requirements: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ requirement: *id005
68
+ prerelease: false
69
+ type: :development
70
+ description: |-
71
+ Trinidad background worker extension built upon
72
+ JRuby-Rack-Worker which provides threaded workers along side your (JRuby-Rack)
73
+ application. Includes (thread-safe) out-of-the-box implementations for popular
74
+ worker libraries such as Resque and Delayed::Job but customized 'daemon'
75
+ scripts can be used as well.
76
+ email:
77
+ - self@kares.org
105
78
  executables: []
79
+
106
80
  extensions: []
107
- extra_rdoc_files:
108
- - README.md
109
- - LICENSE
110
- files:
111
- - .gitignore
112
- - Gemfile
113
- - LICENSE
114
- - README.md
115
- - Rakefile
116
- - lib/trinidad_worker_extension.rb
117
- - lib/trinidad_worker_extension/version.rb
118
- - spec/app/config.ru
119
- - spec/app/trinidad.rb
120
- - spec/app/trinidad.yml
121
- - spec/spec_helper.rb
122
- - spec/trinidad_worker_extension_spec.rb
123
- - trinidad_worker_extension.gemspec
81
+
82
+ extra_rdoc_files:
83
+ - README.md
84
+ - LICENSE
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/trinidad_worker_extension.rb
92
+ - lib/trinidad_worker_extension/version.rb
93
+ - spec/app/config.ru
94
+ - spec/app/trinidad.rb
95
+ - spec/app/trinidad.yml
96
+ - spec/spec_helper.rb
97
+ - spec/trinidad_worker_extension_spec.rb
98
+ - trinidad_worker_extension.gemspec
124
99
  homepage: http://github.com/kares/trinidad_worker_extension
125
100
  licenses: []
126
- post_install_message:
101
+
102
+ post_install_message:
127
103
  rdoc_options: []
128
- require_paths:
129
- - lib
130
- required_ruby_version: !ruby/object:Gem::Requirement
131
- requirements:
132
- - - ! '>='
133
- - !ruby/object:Gem::Version
134
- segments:
135
- - 0
136
- hash: 2
137
- version: !binary |-
138
- MA==
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
139
108
  none: false
140
- required_rubygems_version: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - ! '>='
143
- - !ruby/object:Gem::Version
144
- segments:
145
- - 0
146
- hash: 2
147
- version: !binary |-
148
- MA==
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 2
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
117
  none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 2
122
+ segments:
123
+ - 0
124
+ version: "0"
150
125
  requirements: []
151
- rubyforge_project:
126
+
127
+ rubyforge_project:
152
128
  rubygems_version: 1.8.24
153
- signing_key:
129
+ signing_key:
154
130
  specification_version: 3
155
131
  summary: Background Worker Extension for Trinidad
156
- test_files:
157
- - spec/app/config.ru
158
- - spec/app/trinidad.rb
159
- - spec/app/trinidad.yml
160
- - spec/spec_helper.rb
161
- - spec/trinidad_worker_extension_spec.rb
132
+ test_files:
133
+ - spec/app/config.ru
134
+ - spec/app/trinidad.rb
135
+ - spec/app/trinidad.yml
136
+ - spec/spec_helper.rb
137
+ - spec/trinidad_worker_extension_spec.rb