stapfen 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stapfen.gemspec
4
+ gemspec
5
+
6
+
7
+ group :development do
8
+ gem 'rake'
9
+ gem 'rspec', '~> 2.11'
10
+ gem 'guard-rspec'
11
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 R. Tyler Croy
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.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Stapfen
2
+
3
+ Stapfen is a simple gem to make writing stand-alone STOMP workers easier.
4
+
5
+
6
+ ## Usage
7
+
8
+ (Examples can be found in the `examples/` directory)
9
+
10
+
11
+ Consider the following `myworker.rb` file:
12
+
13
+ class MyWorker < Stapfen::Worker
14
+ configure do
15
+ {:hosts => [
16
+ {
17
+ :host => 'localhost',
18
+ :port => 61613,
19
+ :login => 'guest',
20
+ :passcode => 'guest',
21
+ :ssl => false
22
+ }
23
+ ]}
24
+ end
25
+
26
+ consume 'thequeue' do |message|
27
+ data = expensive_computation(message.body)
28
+ persist(data)
29
+ end
30
+ end
31
+
32
+ MyWorker.run!
33
+
34
+
35
+ The value returned from the `configure` block is expected to be a valid
36
+ `Stomp::Client` [connection
37
+ hash](https://github.com/stompgem/stomp#hash-login-example-usage-this-is-the-recommended-login-technique).
38
+
39
+ It is also important to note that the `consume` block will be invoked inside an
40
+ **instance** of `MyWorker` and will execute inside its own `Thread`, so take
41
+ care when accessing other shared resources.
42
+
43
+
44
+ ## Installation
45
+
46
+ Add this line to your application's Gemfile:
47
+
48
+ gem 'stapfen'
49
+
50
+ And then execute:
51
+
52
+ $ bundle
53
+
54
+ Or install it yourself as:
55
+
56
+ $ gem install stapfen
57
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec') do |t|
5
+ t.rspec_opts = '--color --fail-fast'
6
+ end
7
+
8
+ task :test => :spec
9
+
10
+ task :default => [:spec, :build]
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
2
+ require 'stapfen'
3
+
4
+
5
+ class Worker < Stapfen::Worker
6
+
7
+ configure do
8
+ {:hosts => [
9
+ {
10
+ :host => 'localhost',
11
+ :port => 61613,
12
+ :login => 'guest',
13
+ :passcode => 'guest',
14
+ :ssl => false
15
+ }
16
+ ]}
17
+ end
18
+
19
+ consume 'jms.queue.test' do |message|
20
+ puts "received: #{message}"
21
+ end
22
+
23
+ consume 'jms.topic.foo', {:ack => 'client'} do |message|
24
+ main_thread do
25
+ client.acknowledge(message)
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ Worker.run!
@@ -0,0 +1,3 @@
1
+ module Stapfen
2
+ VERSION = "1.0.#{ENV['BUILD_NUMBER'] || 'dev'}"
3
+ end
@@ -0,0 +1,103 @@
1
+ require 'stomp'
2
+
3
+ module Stapfen
4
+ class Worker
5
+ class << self
6
+ attr_accessor :configuration, :consumers
7
+ end
8
+
9
+ def self.run!
10
+ self.new.run
11
+ end
12
+
13
+ def self.configure
14
+ unless block_given?
15
+ raise Stapfen::ConfigurationError
16
+ end
17
+ @configuration = yield
18
+ end
19
+
20
+ def self.consume(queue_name, headers={}, &block)
21
+ unless block_given?
22
+ raise Stapfen::ConsumeError, "Cannot consume #{queue_name} without a block!"
23
+ end
24
+ @consumers ||= []
25
+ @consumers << [queue_name, headers, block]
26
+ end
27
+
28
+ attr_accessor :client
29
+
30
+ def initialize
31
+ @pool = []
32
+ @workqueue = Queue.new
33
+ handle_signals!
34
+ end
35
+
36
+ def main_thread(&block)
37
+ @workqueue << block
38
+ end
39
+
40
+ def run
41
+ @client = Stomp::Client.new(self.class.configuration)
42
+
43
+ self.class.consumers.each do |name, headers, block|
44
+
45
+ # We're taking each block and turning it into a method so that we can
46
+ # use the instance scope instead of the blocks originally bound scope
47
+ # which would be at a class level
48
+ method_name = name.gsub(/[.|\-]/, '_').to_sym
49
+ self.class.send(:define_method, method_name, &block)
50
+
51
+ client.subscribe(name, headers) do |message|
52
+ @pool << Thread.new do
53
+ self.send(method_name, message)
54
+ end
55
+ end
56
+ end
57
+
58
+ begin
59
+ # Performing this join/open loop to make sure that we don't
60
+ # experience potential deadlocks between signal handlers who might
61
+ # close the connection, and an infinite Client#join call
62
+ while client.open? do
63
+ client.join(1)
64
+
65
+ until @workqueue.empty?
66
+ block = @workqueue.pop
67
+ begin
68
+ block.call
69
+ rescue => e
70
+ puts "Exception! #{e}"
71
+ end
72
+ end
73
+
74
+ @pool = @pool.select { |t| t.alive? }
75
+ end
76
+ rescue Interrupt
77
+ exit_cleanly
78
+ end
79
+ end
80
+
81
+ def handle_signals!
82
+ Signal.trap(:INT) do
83
+ exit_cleanly
84
+ exit!
85
+ end
86
+ Signal.trap(:TERM) do
87
+ exit_cleanly
88
+ end
89
+ end
90
+
91
+ def exit_cleanly
92
+ unless @pool.empty?
93
+ puts "Giving #{@pool.size} threads 10s each to finish up"
94
+ @pool.each do |t|
95
+ t.join(10)
96
+ end
97
+ end
98
+ unless client.closed?
99
+ client.close
100
+ end
101
+ end
102
+ end
103
+ end
data/lib/stapfen.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'stapfen/version'
2
+ require 'stapfen/worker'
3
+
4
+ module Stapfen
5
+ class ConfigurationError < StandardError
6
+ end
7
+ class ConsumeError < StandardError
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '../lib'))
2
+
3
+ require 'stapfen'
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Stapfen::Worker do
5
+ let(:worker) { subject }
6
+
7
+ context 'class methods' do
8
+ subject { described_class }
9
+ let(:worker) { described_class }
10
+
11
+ it { should respond_to :run! }
12
+ it { should respond_to :configure }
13
+ it { should respond_to :consume }
14
+
15
+ describe '#configure' do
16
+ it 'should error when not passed a block' do
17
+ expect {
18
+ worker.configure
19
+ }.to raise_error(Stapfen::ConfigurationError)
20
+ end
21
+
22
+ it 'should save the return value from the block' do
23
+ config = {:valid => true}
24
+
25
+ worker.configure do
26
+ config
27
+ end
28
+
29
+ worker.configuration.should == config
30
+ end
31
+ end
32
+
33
+
34
+ describe 'consume' do
35
+ it 'should raise an error if no block is passed' do
36
+ expect {
37
+ worker.consume 'jms.queue.lol'
38
+ }.to raise_error(Stapfen::ConsumeError)
39
+ end
40
+
41
+ context 'with just a queue name' do
42
+ let(:name) { 'jms.queue.lol' }
43
+
44
+ it 'should add an entry for the queue name' do
45
+ worker.consume(name) do
46
+ nil
47
+ end
48
+
49
+ worker.consumers.should_not be_empty
50
+ entry = worker.consumers.first
51
+ entry.first.should eq(name)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
data/stapfen.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stapfen/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "stapfen"
8
+ gem.version = Stapfen::VERSION
9
+ gem.authors = ["R. Tyler Croy"]
10
+ gem.email = ["rtyler.croy@lookout.com"]
11
+ gem.description = "A simple gem for writing good basic STOMP workers"
12
+ gem.summary = "A simple gem for writing good basic STOMP workers"
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+
21
+ gem.add_dependency('stomp', '>= 1.2.8')
22
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stapfen
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - R. Tyler Croy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: stomp
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.2.8
30
+ description: A simple gem for writing good basic STOMP workers
31
+ email:
32
+ - rtyler.croy@lookout.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Guardfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - examples/simple.rb
44
+ - lib/stapfen.rb
45
+ - lib/stapfen/version.rb
46
+ - lib/stapfen/worker.rb
47
+ - spec/spec_helper.rb
48
+ - spec/worker_spec.rb
49
+ - stapfen.gemspec
50
+ homepage: ''
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ segments:
63
+ - 0
64
+ hash: -2006440266819501787
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ segments:
72
+ - 0
73
+ hash: -2006440266819501787
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.25
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A simple gem for writing good basic STOMP workers
80
+ test_files:
81
+ - spec/spec_helper.rb
82
+ - spec/worker_spec.rb