trinidad_worker_extension 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +10 -0
- data/Gemfile +3 -2
- data/LICENSE +1 -1
- data/README.md +9 -3
- data/Rakefile +1 -0
- data/lib/trinidad_worker_extension.rb +30 -21
- data/lib/trinidad_worker_extension/version.rb +1 -1
- data/spec/app/trinidad.rb +4 -5
- data/spec/spec_helper.rb +2 -2
- data/spec/trinidad_worker_extension_spec.rb +19 -19
- data/trinidad_worker_extension.gemspec +3 -4
- metadata +7 -17
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
Background Workers for [Trinidad](https://github.com/trinidad/trinidad/) running
|
4
4
|
as background (daemon) threads along side your Rack/Rails deployed application.
|
5
5
|
|
6
|
-
Built upon https://github.com/kares/jruby-rack-worker thus supports popular
|
6
|
+
Built upon https://github.com/kares/jruby-rack-worker thus supports popular
|
7
7
|
worker libraries such as **Resque** and **Delayed::Job**.
|
8
8
|
|
9
9
|
## Install
|
@@ -30,6 +30,12 @@ Or install it yourself as a plain old gem :
|
|
30
30
|
|
31
31
|
Like all extensions set it up in the configuration file (e.g. *trinidad.yml*).
|
32
32
|
|
33
|
+
**NOTE:** The extension will not be configuring workers threads to start when
|
34
|
+
running in **rackup** mode (e.g. `rails s`) because it expects JRuby-Rack to be
|
35
|
+
not loaded in the embedded mode. Running Trinidad using `rackup` is mostly
|
36
|
+
suitable for development/testing thus this is not seen as a limitation (simply
|
37
|
+
start `trinidad -e staging` to check whether your workers are doing fine).
|
38
|
+
|
33
39
|
### Delayed::Job
|
34
40
|
|
35
41
|
```yaml
|
@@ -86,7 +92,7 @@ The following start script will be executed in each Thread http://git.io/XglTpw
|
|
86
92
|
```
|
87
93
|
|
88
94
|
If you'd like to specify custom parameters you can do so within the configuration
|
89
|
-
file or the deployment descriptor as context init parameters or as java system
|
95
|
+
file or the deployment descriptor as context init parameters or as java system
|
90
96
|
properties, use the following code to obtain them in your code :
|
91
97
|
|
92
98
|
```ruby
|
@@ -100,5 +106,5 @@ worker.queues = (env['QUEUES']).split(',')
|
|
100
106
|
|
101
107
|
## Copyright
|
102
108
|
|
103
|
-
Copyright (c)
|
109
|
+
Copyright (c) 2013 [Karol Bucek](https://github.com/kares).
|
104
110
|
See LICENSE (http://en.wikipedia.org/wiki/MIT_License) for details.
|
data/Rakefile
CHANGED
@@ -8,11 +8,11 @@ module Trinidad
|
|
8
8
|
|
9
9
|
def configure(context)
|
10
10
|
if ! options || options.size == 0
|
11
|
-
context.logger.info "
|
11
|
+
context.logger.info "No worker(s) seems to be configured"
|
12
12
|
else
|
13
13
|
worker_config = options.first
|
14
14
|
if options.size > 1
|
15
|
-
context.logger.info "
|
15
|
+
context.logger.info "Currently only 1 worker configuration per " <<
|
16
16
|
"web-app is supported, will use first: #{worker_config.inspect}"
|
17
17
|
end
|
18
18
|
if worker_config.is_a?(Array) # [ key, val ]
|
@@ -24,12 +24,17 @@ module Trinidad
|
|
24
24
|
end
|
25
25
|
|
26
26
|
protected
|
27
|
-
|
27
|
+
|
28
28
|
def configure_worker(context, name, config)
|
29
|
-
config = config.dup
|
30
|
-
params = {}
|
29
|
+
config = config.dup; params = {}
|
31
30
|
if script = config.delete(:script)
|
32
|
-
|
31
|
+
if File.exists?(script)
|
32
|
+
context.logger.warn "Seems you've set :script to an actual file path: " <<
|
33
|
+
"'#{script}' please use :script_path instead to make this warning go away"
|
34
|
+
params['jruby.worker.script.path'] = script
|
35
|
+
else
|
36
|
+
params['jruby.worker.script'] = script
|
37
|
+
end
|
33
38
|
end
|
34
39
|
if script_path = config.delete(:script_path)
|
35
40
|
params['jruby.worker.script.path'] = script_path
|
@@ -38,7 +43,7 @@ module Trinidad
|
|
38
43
|
if name
|
39
44
|
params['jruby.worker'] = name.to_s
|
40
45
|
else
|
41
|
-
context.logger.warn "
|
46
|
+
context.logger.warn "Not starting any workers due missing configuration " <<
|
42
47
|
"either set :script or :script_path if you're not using a built-in worker"
|
43
48
|
return
|
44
49
|
end
|
@@ -57,22 +62,26 @@ module Trinidad
|
|
57
62
|
context.add_lifecycle_listener listener = WorkerLifecycle.new(params)
|
58
63
|
listener
|
59
64
|
end
|
60
|
-
|
65
|
+
|
61
66
|
CONTEXT_LISTENER = 'org.kares.jruby.rack.WorkerContextListener'
|
62
|
-
|
67
|
+
|
63
68
|
class WorkerLifecycle < Trinidad::Lifecycle::Base
|
64
|
-
|
69
|
+
|
65
70
|
attr_reader :context_parameters
|
66
|
-
|
71
|
+
|
67
72
|
def initialize(params)
|
68
73
|
@context_parameters = params || {}
|
69
|
-
if @context_parameters.empty?
|
70
|
-
raise ArgumentError, "no context parameters"
|
71
|
-
end
|
74
|
+
raise ArgumentError, "no context parameters" if @context_parameters.empty?
|
72
75
|
end
|
73
|
-
|
76
|
+
|
74
77
|
def configure_start(event)
|
75
78
|
context = event.lifecycle
|
79
|
+
configure = context.findApplicationListeners.any? # in rackup mode empty
|
80
|
+
configure ||= Java::JavaLang::Boolean.getBoolean('trinidad.extensions.worker')
|
81
|
+
unless configure
|
82
|
+
context.logger.info "Skipped configuration of worker extension (due rackup mode)"
|
83
|
+
return
|
84
|
+
end
|
76
85
|
add_context_parameters(context)
|
77
86
|
add_class_loader_jar_url(context)
|
78
87
|
# NOTE: it's important for this listener to be added after
|
@@ -81,9 +90,9 @@ module Trinidad
|
|
81
90
|
# right after #before_start but before the actual #start !
|
82
91
|
add_application_listener(context)
|
83
92
|
end
|
84
|
-
|
93
|
+
|
85
94
|
protected
|
86
|
-
|
95
|
+
|
87
96
|
def add_context_parameters(context)
|
88
97
|
app_params = context.find_application_parameters
|
89
98
|
context_parameters.each do |name, value|
|
@@ -98,7 +107,7 @@ module Trinidad
|
|
98
107
|
end
|
99
108
|
end
|
100
109
|
end
|
101
|
-
|
110
|
+
|
102
111
|
def add_class_loader_jar_url(context)
|
103
112
|
jar_file = java.io.File.new JRuby::Rack::Worker::JAR_PATH
|
104
113
|
class_loader = context.loader.class_loader
|
@@ -106,16 +115,16 @@ module Trinidad
|
|
106
115
|
class_loader.addURL jar_file.to_url
|
107
116
|
end
|
108
117
|
end
|
109
|
-
|
118
|
+
|
110
119
|
def add_application_listener(context)
|
111
120
|
listener = CONTEXT_LISTENER
|
112
121
|
unless context.find_application_listeners.include?(listener)
|
113
122
|
context.add_application_listener listener
|
114
123
|
end
|
115
124
|
end
|
116
|
-
|
125
|
+
|
117
126
|
end
|
118
|
-
|
127
|
+
|
119
128
|
end
|
120
129
|
end
|
121
130
|
end
|
data/spec/app/trinidad.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
Trinidad.configure do |config|
|
2
2
|
config[:jruby_min_runtimes] = 1
|
3
3
|
config[:jruby_max_runtimes] = 1
|
4
|
-
|
4
|
+
|
5
5
|
worker_config = {}
|
6
6
|
worker_config[:resque] = {
|
7
7
|
:thread_priority => 'MIN',
|
8
8
|
'QUEUES' => ['low', 'normal'],
|
9
9
|
'INTERVAL' => 1.5,
|
10
|
-
'
|
11
|
-
}
|
12
|
-
config[:extensions] = {
|
13
|
-
:worker => worker_config
|
10
|
+
'LOGGING' => 'debug'
|
14
11
|
}
|
12
|
+
|
13
|
+
config[:extensions] = { :worker => worker_config }
|
15
14
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
begin
|
2
|
-
require 'bundler
|
2
|
+
require 'bundler'
|
3
3
|
rescue LoadError => e
|
4
4
|
require('rubygems') && retry
|
5
5
|
raise e
|
6
6
|
end
|
7
|
+
Bundler.require(:default, :test)
|
7
8
|
|
8
9
|
require 'rspec'
|
9
|
-
require 'mocha'
|
10
10
|
|
11
11
|
lib = File.expand_path('../lib', File.dirname(__FILE__))
|
12
12
|
$: << lib unless $:.include?(lib)
|
@@ -2,7 +2,7 @@ require File.expand_path('spec_helper', File.dirname(__FILE__))
|
|
2
2
|
require 'yaml'
|
3
3
|
|
4
4
|
describe Trinidad::Extensions::WorkerWebAppExtension do
|
5
|
-
|
5
|
+
|
6
6
|
APP_DIR = File.expand_path('app', File.dirname(__FILE__))
|
7
7
|
|
8
8
|
it "configures (delayed) worker" do
|
@@ -11,9 +11,9 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
|
|
11
11
|
web_app = create_web_app; context = create_web_app_context(web_app)
|
12
12
|
|
13
13
|
Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
|
14
|
-
|
14
|
+
|
15
15
|
it_includes_worker_lifecycle_listener(context)
|
16
|
-
|
16
|
+
|
17
17
|
listener = worker_lifecycle_listener_for(context)
|
18
18
|
params = listener.context_parameters
|
19
19
|
expect( params['jruby.worker'] ).to eql 'delayed_job'
|
@@ -25,22 +25,22 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
|
|
25
25
|
it "configures (resque) worker" do
|
26
26
|
Trinidad.configure! { load File.join(APP_DIR, 'trinidad.rb') }
|
27
27
|
web_app = create_web_app; context = create_web_app_context(web_app)
|
28
|
-
|
28
|
+
|
29
29
|
Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
|
30
|
-
|
30
|
+
|
31
31
|
it_includes_worker_lifecycle_listener(context)
|
32
|
-
|
32
|
+
|
33
33
|
listener = worker_lifecycle_listener_for(context)
|
34
34
|
params = listener.context_parameters
|
35
35
|
expect( params['jruby.worker'] ).to eql 'resque'
|
36
36
|
expect( params['jruby.worker.thread.priority'] ).to eql 'MIN'
|
37
37
|
expect( params['QUEUES'] ).to eql 'low,normal'
|
38
38
|
expect( params['INTERVAL'] ).to eql '1.5'
|
39
|
-
expect( params['
|
39
|
+
expect( params['LOGGING'] ).to eql 'debug'
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
describe 'WorkerLifecycle', :integration => true do
|
43
|
-
|
43
|
+
|
44
44
|
before do
|
45
45
|
Trinidad.configure! { load File.join(APP_DIR, 'trinidad.rb') }
|
46
46
|
@web_app = create_web_app; @context = create_web_app_context(@web_app)
|
@@ -51,11 +51,11 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
|
|
51
51
|
@lifecycle = listeners.find { |l| l.is_a?(klass) }
|
52
52
|
# do what Trinidad server startup would :
|
53
53
|
@context.add_lifecycle_listener(@web_app.define_lifecycle)
|
54
|
-
|
54
|
+
|
55
55
|
#@event = mock('event'); @event.stub!(:lifecycle).and_return @context
|
56
56
|
@context.start
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
it "configures worker context listener and init params" do
|
60
60
|
class_name = 'org.kares.jruby.rack.WorkerContextListener'
|
61
61
|
expect( @context.find_application_listeners ).to include class_name
|
@@ -70,29 +70,29 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
|
|
70
70
|
expect( @context.servlet_context.get_init_parameter('jruby.worker') ).to eql 'resque'
|
71
71
|
expect( @context.servlet_context.get_init_parameter('QUEUES') ).to eql 'low,normal'
|
72
72
|
end
|
73
|
-
|
73
|
+
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
def it_includes_worker_lifecycle_listener(context)
|
77
77
|
expect( worker_lifecycle_listener_for(context) ).to_not be nil
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
def worker_lifecycle_listener_for(context)
|
81
81
|
listeners = context.find_lifecycle_listeners
|
82
82
|
klass = Trinidad::Extensions::WorkerWebAppExtension::WorkerLifecycle
|
83
83
|
listeners.find { |l| l.is_a?(klass) }
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
protected
|
87
|
-
|
87
|
+
|
88
88
|
def parameters(context)
|
89
89
|
context.find_parameters.inject({}) do |hash, name|
|
90
90
|
hash[name] = context.find_parameter(name); hash
|
91
91
|
end
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
private
|
95
|
-
|
95
|
+
|
96
96
|
def tomcat
|
97
97
|
@tomcat ||= org.apache.catalina.startup.Tomcat.new
|
98
98
|
end
|
@@ -113,5 +113,5 @@ describe Trinidad::Extensions::WorkerWebAppExtension do
|
|
113
113
|
context.addLifecycleListener lifecycle if lifecycle
|
114
114
|
context
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
end
|
@@ -21,9 +21,8 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.extra_rdoc_files = %w[ README.md LICENSE ]
|
22
22
|
|
23
23
|
gem.require_paths = ["lib"]
|
24
|
-
gem.add_dependency 'trinidad', ">= 1.4.
|
25
|
-
gem.add_dependency 'jruby-rack-worker', ">= 0.
|
26
|
-
gem.add_development_dependency 'rspec', '~> 2.
|
27
|
-
gem.add_development_dependency 'mocha'
|
24
|
+
gem.add_dependency 'trinidad', ">= 1.4.4"
|
25
|
+
gem.add_dependency 'jruby-rack-worker', ">= 0.9"
|
26
|
+
gem.add_development_dependency 'rspec', '~> 2.11'
|
28
27
|
gem.add_development_dependency 'rake'
|
29
28
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: trinidad_worker_extension
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Karol Bucek
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2013-06-06 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: trinidad
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 1.4.
|
22
|
+
version: 1.4.4
|
23
23
|
requirement: *id001
|
24
24
|
prerelease: false
|
25
25
|
type: :runtime
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: "0.
|
33
|
+
version: "0.9"
|
34
34
|
requirement: *id002
|
35
35
|
prerelease: false
|
36
36
|
type: :runtime
|
@@ -41,12 +41,12 @@ dependencies:
|
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
43
43
|
- !ruby/object:Gem::Version
|
44
|
-
version: "2.
|
44
|
+
version: "2.11"
|
45
45
|
requirement: *id003
|
46
46
|
prerelease: false
|
47
47
|
type: :development
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
name:
|
49
|
+
name: rake
|
50
50
|
version_requirements: &id004 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
@@ -56,17 +56,6 @@ dependencies:
|
|
56
56
|
requirement: *id004
|
57
57
|
prerelease: false
|
58
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
59
|
description: |-
|
71
60
|
Trinidad background worker extension built upon
|
72
61
|
JRuby-Rack-Worker which provides threaded workers along side your (JRuby-Rack)
|
@@ -84,6 +73,7 @@ extra_rdoc_files:
|
|
84
73
|
- LICENSE
|
85
74
|
files:
|
86
75
|
- .gitignore
|
76
|
+
- .travis.yml
|
87
77
|
- Gemfile
|
88
78
|
- LICENSE
|
89
79
|
- README.md
|