trinidad_worker_extension 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ spec/app/log/*.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
4
+ gem 'resque', :group => :test
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+
2
+ Copyright (c) 2012 Karol Bucek
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ == Additional Bundled Software
24
+
25
+ JRuby::Rack::Worker is licensed under the Apache License, Version 2.0.
26
+ See http://www.apache.org/licenses/LICENSE-2.0 for details.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Trinidad Worker Extension
2
+
3
+ Background Workers for [Trinidad](https://github.com/trinidad/trinidad/) running
4
+ as background (daemon) threads along side your Rack/Rails deployed application.
5
+
6
+ Built upon https://github.com/kares/jruby-rack-worker thus supports popular
7
+ worker libraries such as **Resque** and **Delayed::Job**.
8
+
9
+ ## Install
10
+
11
+ Along with Trinidad in your application's *Gemfile* :
12
+
13
+ ```ruby
14
+ group :server do
15
+ platform :jruby do
16
+ gem 'trinidad', :require => false
17
+ gem 'trinidad_worker_extension', :require => false
18
+ end
19
+ end
20
+ ```
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as a plain old gem :
25
+
26
+ $ gem install trinidad_worker_extension
27
+
28
+
29
+ ## Configure
30
+
31
+ Like all extensions set it up in the configuration file (e.g. *trinidad.yml*).
32
+
33
+ ### Delayed::Job
34
+
35
+ ```yaml
36
+ ---
37
+ # ...
38
+ extensions:
39
+ worker:
40
+ delayed_job:
41
+ # all settings here are optional
42
+ thread_count: 1
43
+ thread_priority: NORM
44
+ # DJ specifics (optional as well) :
45
+ QUEUE: mailers,tasks
46
+ READ_AHEAD: 3 # default 5
47
+ SLEEP_DELAY: 2.5 # default 5
48
+ #MIN_PRIORITY: 1
49
+ #MAX_PRIORITY: 5
50
+ ```
51
+
52
+ The following start script will be executed in each Thread http://git.io/yLSgLA
53
+
54
+ ### Resque
55
+
56
+ ```ruby
57
+ Trinidad.configure do |config|
58
+ config[:extensions] = {
59
+ :worker =>
60
+ :resque => {
61
+ :thread_priority => 4, # bit bellow NORM (5)
62
+ 'QUEUES' => ['*'],
63
+ 'INTERVAL' => 2.5, # default is 5.0
64
+ 'VERBOSE' => true, # verbose logging
65
+ #'VVERBOSE' => true, # very_verbose logging
66
+ }
67
+ }
68
+ end
69
+ ```
70
+
71
+ The following start script will be executed in each Thread http://git.io/XglTpw
72
+
73
+ ### Custom Worker
74
+
75
+ ```yaml
76
+ ---
77
+ # ...
78
+ extensions:
79
+ worker:
80
+ custom:
81
+ #script: require 'my_worker'; MyWorker.start
82
+ script.path: "lib/my_worker/start_worker.rb"
83
+ # all settings here are optional
84
+ #thread_count: 1
85
+ #thread_priority: NORM
86
+ ```
87
+
88
+ 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
90
+ properties, use the following code to obtain them in your code :
91
+
92
+ ```ruby
93
+ require 'jruby/rack/worker/env'
94
+ env = JRuby::Rack::Worker::ENV
95
+
96
+ worker = MyWorker.new
97
+ worker.queues = (env['QUEUES']).split(',')
98
+ # ...
99
+ ```
100
+
101
+ ## Copyright
102
+
103
+ Copyright (c) 2012 [Karol Bucek](https://github.com/kares).
104
+ See LICENSE (http://en.wikipedia.org/wiki/MIT_License) for details.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'bundler/gem_helper'
3
+ rescue LoadError => e
4
+ require('rubygems') && retry
5
+ raise e
6
+ end
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require 'rspec/core/rake_task'
10
+ RSpec::Core::RakeTask.new(:spec) do |spec|
11
+ spec.rspec_opts = ['--color', "--format documentation"]
12
+ end
13
+
14
+ task :default => :spec
@@ -0,0 +1,79 @@
1
+ require 'trinidad'
2
+ require 'trinidad_worker_extension/version'
3
+ require 'jruby/rack/worker'
4
+
5
+ module Trinidad
6
+ module Extensions
7
+ class WorkerWebAppExtension < WebAppExtension
8
+
9
+ def configure(context)
10
+ if ! options || options.size == 0
11
+ context.logger.info "no worker(s) seems to be configured"
12
+ else
13
+ worker_config = options.first
14
+ if options.size > 1
15
+ context.logger.info "currently only 1 worker configuration per " +
16
+ "web-app is supported, will use first: #{worker_config.inspect}"
17
+ end
18
+ if worker_config.is_a?(Array) # [ key, val ]
19
+ configure_worker context, worker_config[0], worker_config[1]
20
+ else
21
+ configure_worker context, nil, worker_config
22
+ end
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def configure_worker(context, name, config)
29
+ config = config.dup
30
+ if script = config.delete(:script)
31
+ context.add_parameter 'jruby.worker.script', script
32
+ end
33
+ if script_path = config.delete(:script_path)
34
+ context.add_parameter 'jruby.worker.script.path', script_path
35
+ end
36
+ if script.nil? && script_path.nil?
37
+ if name
38
+ context.add_parameter('jruby.worker', name.to_s)
39
+ else
40
+ context.logger.warn "not-starting any workers due missing configuration " +
41
+ "either set :script or :script_path if you're not using a built-in worker"
42
+ return
43
+ end
44
+ end
45
+ config.each do |key, value|
46
+ case key.to_s
47
+ when 'thread_count'
48
+ context.add_parameter('jruby.worker.thread.count', value.to_s)
49
+ when 'thread_priority'
50
+ context.add_parameter('jruby.worker.thread.priority', value.to_s)
51
+ else
52
+ value = value.join(',') if value.respond_to?(:join)
53
+ context.add_parameter(key.to_s, value.to_s)
54
+ end
55
+ end
56
+ context.add_lifecycle_listener listener = WorkerLifecycle.new
57
+ listener
58
+ end
59
+
60
+ CONTEXT_LISTENER = 'org.kares.jruby.rack.WorkerContextListener'
61
+
62
+ class WorkerLifecycle < Trinidad::Lifecycle::Base
63
+
64
+ def configure_start(event)
65
+ 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
68
+ # NOTE: it's important for this listener to be added after
69
+ # the Rack setup as it expectd to find the RackFactory ...
70
+ # that's why we hook into #configure_start which happens
71
+ # right after #before_start but before the actual #start !
72
+ context.add_application_listener CONTEXT_LISTENER
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,7 @@
1
+ module Trinidad
2
+ module Extensions
3
+ module Worker
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ use Rack::CommonLogger
2
+ run Proc.new { [200, {'Content-Type' => 'text/plain'}, 'OK'] }
@@ -0,0 +1,15 @@
1
+ Trinidad.configure do |config|
2
+ config[:jruby_min_runtimes] = 1
3
+ config[:jruby_max_runtimes] = 1
4
+
5
+ worker_config = {}
6
+ worker_config[:resque] = {
7
+ :thread_priority => 'MIN',
8
+ 'QUEUES' => ['low', 'normal'],
9
+ 'INTERVAL' => 1.5,
10
+ 'VERBOSE' => true
11
+ }
12
+ config[:extensions] = {
13
+ :worker => worker_config
14
+ }
15
+ end
@@ -0,0 +1,17 @@
1
+ ---
2
+ environment: production
3
+ port: 4444
4
+ trap: false
5
+ extensions:
6
+ worker:
7
+ delayed_job: # jruby.worker
8
+ #script: "require 'sample/resque_worker'; Sample::ResqueWorker.start"
9
+ #script_path: sample/start_resque_worker.rb
10
+ thread_count: 2 # jruby.worker.thread.count
11
+ # DJ specific :
12
+ #QUEUE: all
13
+ READ_AHEAD: 3
14
+ SLEEP_DELAY: 3.0
15
+ another: # (will be ignored)
16
+ script: " puts 'started another worker' "
17
+ thread_priority: MIN # jruby.worker.thread.priority
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError => e
4
+ require('rubygems') && retry
5
+ raise e
6
+ end
7
+
8
+ require 'rspec'
9
+ require 'mocha'
10
+
11
+ lib = File.expand_path('../lib', File.dirname(__FILE__))
12
+ $: << lib unless $:.include?(lib)
13
+ require 'trinidad_worker_extension'
@@ -0,0 +1,101 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+ require 'yaml'
3
+
4
+ describe Trinidad::Extensions::WorkerWebAppExtension do
5
+
6
+ APP_DIR = File.expand_path('app', File.dirname(__FILE__))
7
+
8
+ it "configures (delayed) worker" do
9
+ options = YAML.load( File.read(File.join(APP_DIR, 'trinidad.yml')) )
10
+ Trinidad.configure!(options)
11
+ web_app = create_web_app; context = create_web_app_context(web_app)
12
+
13
+ Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
14
+
15
+ params = parameters(context)
16
+ expect( params['jruby.worker'] ).to eql 'delayed_job'
17
+ expect( params['jruby.worker.thread.count'] ).to eql '2'
18
+ expect( params['READ_AHEAD'] ).to eql '3'
19
+ expect( params['SLEEP_DELAY'] ).to eql '3.0'
20
+
21
+ it_includes_worker_lifecycle_listener(context)
22
+ end
23
+
24
+ it "configures (resque) worker" do
25
+ Trinidad.configure! { load File.join(APP_DIR, 'trinidad.rb') }
26
+ web_app = create_web_app; context = create_web_app_context(web_app)
27
+
28
+ Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
29
+
30
+ params = parameters(context)
31
+ expect( params['jruby.worker'] ).to eql 'resque'
32
+ expect( params['jruby.worker.thread.priority'] ).to eql 'MIN'
33
+ expect( params['QUEUES'] ).to eql 'low,normal'
34
+ expect( params['INTERVAL'] ).to eql '1.5'
35
+ expect( params['VERBOSE'] ).to eql 'true'
36
+
37
+ it_includes_worker_lifecycle_listener(context)
38
+ end
39
+
40
+ describe 'WorkerLifecycle' do
41
+
42
+ before do
43
+ Trinidad.configure! { load File.join(APP_DIR, 'trinidad.rb') }
44
+ @web_app = create_web_app; @context = create_web_app_context(@web_app)
45
+ # sets up the worker lifecycle listener :
46
+ Trinidad::Extensions.configure_webapp_extensions(@web_app.extensions, tomcat, @context)
47
+ listeners = @context.find_lifecycle_listeners
48
+ klass = Trinidad::Extensions::WorkerWebAppExtension::WorkerLifecycle
49
+ @lifecycle = listeners.find { |l| l.is_a?(klass) }
50
+ # do what Trinidad server startup would :
51
+ @context.add_lifecycle_listener(@web_app.define_lifecycle)
52
+
53
+ #@event = mock('event'); @event.stub!(:lifecycle).and_return @context
54
+ @context.start
55
+ end
56
+
57
+ it "configures worker context listener" do
58
+ class_name = 'org.kares.jruby.rack.WorkerContextListener'
59
+ expect( @context.find_application_listeners ).to include class_name
60
+ end
61
+
62
+ end
63
+
64
+ def it_includes_worker_lifecycle_listener(context)
65
+ listeners = context.find_lifecycle_listeners
66
+ klass = Trinidad::Extensions::WorkerWebAppExtension::WorkerLifecycle
67
+ expect( listeners.find { |l| l.is_a?(klass) } ).to_not be nil
68
+ end
69
+
70
+ protected
71
+
72
+ def parameters(context)
73
+ context.find_parameters.inject({}) do |hash, name|
74
+ hash[name] = context.find_parameter(name); hash
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def tomcat
81
+ @tomcat ||= org.apache.catalina.startup.Tomcat.new
82
+ end
83
+
84
+ def create_web_app(config = {})
85
+ Trinidad::WebApp.create({ :context_path => '/', :root_dir => APP_DIR }.merge(config))
86
+ end
87
+
88
+ def create_web_app_context(context_dir = APP_DIR, web_app_or_context_path = '/')
89
+ context_path, lifecycle = web_app_or_context_path, nil
90
+ if web_app_or_context_path.is_a?(Trinidad::WebApp)
91
+ context_path = web_app_or_context_path.context_path
92
+ lifecycle = web_app_or_context_path.define_lifecycle
93
+ end
94
+ context = tomcat.addWebapp(context_path, context_dir.to_s)
95
+ context_config = org.apache.catalina.startup.ContextConfig.new
96
+ context.addLifecycleListener context_config
97
+ context.addLifecycleListener lifecycle if lifecycle
98
+ context
99
+ end
100
+
101
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "trinidad_worker_extension/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "trinidad_worker_extension"
7
+ gem.version = Trinidad::Extensions::Worker::VERSION
8
+ gem.authors = ["Karol Bucek"]
9
+ gem.email = ["self@kares.org"]
10
+ gem.homepage = "http://github.com/kares/trinidad_worker_extension"
11
+ gem.summary = %q{Background Worker Extension for Trinidad}
12
+ gem.description = %q{Trinidad background worker extension built upon
13
+ JRuby-Rack-Worker which provides threaded workers along side your (JRuby-Rack)
14
+ application. Includes (thread-safe) out-of-the-box implementations for popular
15
+ worker libraries such as Resque and Delayed::Job but customized 'daemon'
16
+ scripts can be used as well.}
17
+
18
+ gem.files = `git ls-files`.split("\n")
19
+ gem.test_files = `git ls-files -- test/* spec/*`.split("\n")
20
+
21
+ gem.extra_rdoc_files = %w[ README.md LICENSE ]
22
+
23
+ gem.require_paths = ["lib"]
24
+ gem.add_dependency 'trinidad', ">= 1.4.1"
25
+ gem.add_dependency 'jruby-rack-worker', ">= 0.7"
26
+ gem.add_development_dependency 'rspec', '~> 2.10'
27
+ gem.add_development_dependency 'mocha'
28
+ gem.add_development_dependency 'rake'
29
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trinidad_worker_extension
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Karol Bucek
9
+ autorequire:
10
+ bindir: bin
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
105
+ executables: []
106
+ 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
124
+ homepage: http://github.com/kares/trinidad_worker_extension
125
+ licenses: []
126
+ post_install_message:
127
+ 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==
139
+ 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==
149
+ none: false
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.24
153
+ signing_key:
154
+ specification_version: 3
155
+ 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