trinidad_threaded_resque_extension 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in trinidad_threaded_resque_extension.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Carl Hörberg
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Trinidad Threaded Resque Extension
2
+
3
+ Run Resque workers, threaded, within the Trinidad process.
4
+
5
+ Inspiration: https://github.com/trinidad/trinidad_resque_extension/
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'trinidad_threaded_resque_extension'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Then configure it via config/trinidad.rb:
18
+
19
+ require 'bundler/setup'
20
+ Trinidad.configure |config|
21
+ ...
22
+ config.extensions = {
23
+ threaded_resque = {
24
+ setup: './config/setup_resque_workers', # will be required before starting the workers
25
+ 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
32
+ }
33
+ }
34
+ }
35
+
36
+ ## Usage
37
+
38
+ Start trinidad and enjoy the low memory usage while having high concurrency.
39
+
40
+ Note that becasue JRuby can't fork you want to have well behaved workers, watch out for memory leakage..
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,47 @@
1
+ require 'resque'
2
+ require 'trinidad'
3
+ require_relative "trinidad_threaded_resque_extension/version"
4
+ require_relative "trinidad_threaded_resque_extension/resque_disable_signal_handlers"
5
+
6
+ module Trinidad
7
+ 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
43
+ end
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,10 @@
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
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module TrinidadThreadedResqueExtension
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require './lib/trinidad_threaded_resque_extension'
2
+ Resque.inline = true
3
+
4
+ describe Trinidad::Extensions::ThreadedResque::ThreadedResqueLifecycleListener do
5
+ subject { Trinidad::Extensions::ThreadedResque::ThreadedResqueLifecycleListener.new }
6
+
7
+ it 'starts by default one worker' do
8
+ workers = subject.start_workers
9
+ workers.should have(1).things
10
+ end
11
+
12
+ 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']
18
+ end
19
+
20
+ 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']
28
+ end
29
+
30
+ it 'can shutdown workers' do
31
+ subject.start_workers
32
+ workers = subject.stop_workers
33
+ workers[0].shutdown?.should == true
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/trinidad_threaded_resque_extension/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Carl Hörberg"]
6
+ gem.email = ["carl.hoerberg@gmail.com"]
7
+ gem.description = %q{Runs Resque workers, threaded, within the Trinidad application server}
8
+ gem.summary = %q{Runs Resque workers, threaded, within the Trinidad application server}
9
+ gem.homepage = "https://github.com/carlhoerberg/trinidad_threaded_resque_extension"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "trinidad_threaded_resque_extension"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = TrinidadThreadedResqueExtension::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency "json"
21
+ gem.add_runtime_dependency "trinidad"
22
+ gem.add_runtime_dependency "resque"
23
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trinidad_threaded_resque_extension
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Carl Hörberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ version_requirements: &2056 !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ none: false
22
+ requirement: *2056
23
+ prerelease: false
24
+ type: :development
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ version_requirements: &2074 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ none: false
33
+ requirement: *2074
34
+ prerelease: false
35
+ type: :development
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ version_requirements: &2090 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ none: false
44
+ requirement: *2090
45
+ prerelease: false
46
+ type: :development
47
+ - !ruby/object:Gem::Dependency
48
+ name: trinidad
49
+ version_requirements: &2106 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ requirement: *2106
56
+ prerelease: false
57
+ type: :runtime
58
+ - !ruby/object:Gem::Dependency
59
+ name: resque
60
+ version_requirements: &2124 !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ none: false
66
+ requirement: *2124
67
+ prerelease: false
68
+ type: :runtime
69
+ description: Runs Resque workers, threaded, within the Trinidad application server
70
+ email:
71
+ - carl.hoerberg@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/trinidad_threaded_resque_extension.rb
82
+ - lib/trinidad_threaded_resque_extension/resque_disable_signal_handlers.rb
83
+ - lib/trinidad_threaded_resque_extension/version.rb
84
+ - spec/resque_extension_spec.rb
85
+ - trinidad_threaded_resque_extension.gemspec
86
+ homepage: https://github.com/carlhoerberg/trinidad_threaded_resque_extension
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ none: false
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ none: false
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.15
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Runs Resque workers, threaded, within the Trinidad application server
110
+ test_files:
111
+ - spec/resque_extension_spec.rb
112
+ ...