hopper 0.0.1.pre.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33d562bd6dd3fb4fbf879d90de308e9a065545ff
4
+ data.tar.gz: b9a8719f539441ef1a9d2807a1075f0ae1131844
5
+ SHA512:
6
+ metadata.gz: 4d050bda365e3af3e77f0592b169e9e713e4fa5c7ce550b3c8e09fb8cb82461f67783ae47c689f78c6aba1d5c5389ebec6aae29c8cce4328268fc9c956052658
7
+ data.tar.gz: f1be57515a48a11bc67947c8bad4225d5fc4f44db0162d51e72ac567969265e43c46f824e4563622cafd872734098c76bd10e723665bf0d4de2a05b13d58e565
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ env:
4
+ global:
5
+ - COVERAGE=true
6
+ matrix:
7
+ - TEST_SUITE=unit
8
+ - TEST_SUITE=integration
9
+
10
+ script: "bundle exec rake test:$TEST_SUITE"
11
+
12
+ rvm:
13
+ - "1.9.3"
14
+ - "2.1.3"
15
+ - "ruby-head"
16
+ - "rbx-2"
17
+ services:
18
+ - rabbitmq
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gem 'coveralls', group: :test, require: false
5
+ gem 'codeclimate-test-reporter', group: :test, require: nil
6
+
7
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Ed Carrel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ Hopper
2
+ ======
3
+ A framework for task execution written around bunny.
data/Rakefile ADDED
@@ -0,0 +1,61 @@
1
+ require 'pathname'
2
+ require 'rubygems/package'
3
+ require 'rubygems/installer'
4
+
5
+ require 'coveralls/rake/task'
6
+
7
+ require 'rake/testtask'
8
+
9
+ namespace :coinstar do
10
+ task :test do
11
+ Rake::Task['test'].invoke
12
+ end
13
+ end
14
+
15
+ task :build do
16
+ path = Pathname.glob("*.gemspec")
17
+ spec = Gem::Specification.load(path.first.to_s)
18
+ package = Gem::Package.build(spec)
19
+ Gem::Installer.new(package).install
20
+ end
21
+
22
+ task :clean do
23
+ path = Pathname.glob("*.gemspec")
24
+ spec = Gem::Specification.load(path.first.to_s)
25
+ if File.exists?(spec.file_name)
26
+ File.unlink(spec.file_name)
27
+ end
28
+ end
29
+
30
+ task :install => :build do
31
+ path = Pathname.glob("*.gemspec")
32
+ spec = Gem::Specification.load(path.first.to_s)
33
+ Gem::Installer.new(spec.file_name).install
34
+ end
35
+
36
+ task :test do
37
+ Rake::Task['test:unit'].invoke
38
+ Rake::Task['test:integration'].invoke
39
+ end
40
+
41
+ namespace :test do
42
+ Rake::TestTask.new("integration") do |t|
43
+ t.libs << "test"
44
+ t.libs << "config"
45
+ t.test_files = FileList['test/integration/**/*_test.rb']
46
+ t.verbose = true
47
+ end
48
+
49
+ Rake::TestTask.new("unit") do |t|
50
+ t.libs << "test"
51
+ t.libs << "config"
52
+ t.test_files = FileList['test/unit/**/*_test.rb']
53
+ t.verbose = true
54
+ end
55
+ end
56
+
57
+ Coveralls::RakeTask.new
58
+
59
+ task :test_with_coveralls => ['test:unit', 'test:integration', 'coveralls:push']
60
+
61
+ task :default => :test_with_coveralls
data/hopper.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'hopper/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "hopper"
7
+ spec.version = Hopper::VERSION
8
+ spec.summary = "A framework for task execution written around bunny"
9
+ spec.description = "A framework for task execution written around bunny"
10
+ spec.platform = "ruby"
11
+
12
+ spec.homepage = "https://github.com/azanar/hopper"
13
+ spec.license = "MIT"
14
+
15
+ spec.authors = ["Ed Carrel"]
16
+ spec.email = ["edward@carrel.org"]
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency 'activesupport', '~> 4'
24
+ spec.add_runtime_dependency 'bunny', '~> 1'
25
+ spec.add_runtime_dependency 'msgpack', '~> 0'
26
+
27
+ spec.add_development_dependency 'test-unit', '~> 3'
28
+ spec.add_development_dependency 'mocha', '~> 1'
29
+ spec.add_development_dependency 'simplecov', '~> 0'
30
+ spec.add_development_dependency "bundler", "~> 1.6"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
data/lib/hopper.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'stringio'
2
+
3
+ require 'pp'
4
+
5
+ require 'active_support'
6
+
7
+ module Hopper
8
+ def self.logger
9
+ return @logger if @logger
10
+
11
+ @logger = Logger.new(STDOUT)
12
+ @logger.formatter = Logger::Formatter.new
13
+
14
+ @logger.formatter = proc { |severity, datetime, progname, msg|
15
+ "[#{datetime}, #{severity}] #{msg}\n"
16
+ }
17
+ @logger
18
+ end
19
+
20
+ def self.logger=(logger)
21
+ @logger = logger
22
+ end
23
+
24
+ def self.env
25
+ @_env ||= ActiveSupport::StringInquirer.new(ENV["HOPPER_ENV"] || ENV["RAILS_ENV"] || "development")
26
+ end
27
+
28
+ def self.env=(environment)
29
+ @_env = ActiveSupport::StringInquirer.new(environment)
30
+ end
31
+
32
+ def self.ignore_redshift?
33
+ Hopper.env.sandbox? or Hopper.env.staging?
34
+ end
35
+ end
36
+
@@ -0,0 +1,42 @@
1
+ require 'bunny'
2
+
3
+ require 'hopper/queue'
4
+ require 'hopper/channel/queue'
5
+
6
+ module Hopper
7
+ class Channel
8
+ class Proxy
9
+ def initialize(hopper, bunny)
10
+ @hopper = hopper
11
+ @bunny = bunny
12
+ end
13
+
14
+ def queue(name)
15
+ @bunny.queue(name)
16
+ end
17
+ end
18
+
19
+ def initialize
20
+ conn = Bunny.new
21
+ conn.start
22
+
23
+ @bunny = conn.create_channel
24
+ @bunny.prefetch(1)
25
+
26
+ @queues = {}
27
+ end
28
+
29
+ def queue(name)
30
+ if @queues.has_key?(name)
31
+ @queues[name]
32
+ else
33
+ #TODO: This violates SRP. We both manage caching the queues and
34
+ #constructing a bunch of intermediate objects. I'm wondering if this
35
+ #belongs more in a factory somewhere else.
36
+ queue = Hopper::Queue.new(name)
37
+ proxy = Hopper::Channel::Proxy.new(self, @bunny)
38
+ @queues[name] = Hopper::Channel::Queue.new(proxy, queue)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,51 @@
1
+ module Hopper
2
+ class Channel
3
+ class Queue
4
+ class Proxy
5
+ def initialize(bunny)
6
+ @bunny = bunny
7
+ end
8
+
9
+ def channel
10
+ @bunny.channel
11
+ end
12
+
13
+ def publish(message, opts = {})
14
+ @bunny.publish(message, opts)
15
+ end
16
+
17
+ def subscribe(*args)
18
+ @bunny.subscribe(*args) do |*a|
19
+ yield a
20
+ end
21
+ end
22
+ end
23
+
24
+ def initialize(channel,queue)
25
+ @channel = channel
26
+ @queue = queue
27
+
28
+ end
29
+
30
+ attr_reader :queue
31
+
32
+ def listener(opts = {})
33
+ Hopper::Channel::Queue::Listener.new(proxy, opts)
34
+ end
35
+
36
+ def publisher(opts = {})
37
+ Hopper::Channel::Queue::Publisher.new(proxy, opts)
38
+ end
39
+
40
+ private
41
+
42
+ def proxy
43
+ Hopper::Channel::Queue::Proxy.new(bunny)
44
+ end
45
+
46
+ def bunny
47
+ @bunny ||= @queue.for_channel(@channel)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ require 'hopper/message/delivered'
2
+ require 'hopper/message'
3
+
4
+ module Hopper
5
+ class Channel
6
+ class Queue
7
+ class Listener
8
+ def initialize(proxy, opts = {})
9
+ @proxy = proxy
10
+ end
11
+
12
+ def listen
13
+ @proxy.subscribe(:ack => true, :block => true) do |delivery_information, properties, payload|
14
+ message = Message::Delivered.new(self, delivery_information, properties, payload)
15
+ begin
16
+ yield message
17
+ rescue Exception => e
18
+ Hopper.logger.error "Caught exception #{e} that worker should have caught. Message #{message.tag} being rejected by default.\n\n#{e.backtrace.join("\n")}"
19
+ reject(message)
20
+ end
21
+ end
22
+ end
23
+
24
+ def acknowledge(message)
25
+ bunny_channel.acknowledge(message.tag)
26
+ end
27
+
28
+ def retry(message)
29
+ bunny_channel.reject(message.tag, true)
30
+ end
31
+
32
+ def reject(message)
33
+ bunny_channel.reject(message.tag, false)
34
+ end
35
+
36
+ private
37
+
38
+ def bunny_channel
39
+ @proxy.channel
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ module Hopper
2
+ class Channel
3
+ class Queue
4
+ class Publisher
5
+ def initialize(proxy, opts = {})
6
+ @proxy = proxy
7
+ end
8
+
9
+ def publish(message, opts = {})
10
+ @proxy.publish(message.payload, opts)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ require 'msgpack'
2
+
3
+ require 'hopper'
4
+
5
+ module Hopper
6
+ class Message
7
+ def initialize(payload)
8
+ @payload = payload
9
+ end
10
+
11
+ attr_reader :payload
12
+ end
13
+ end
@@ -0,0 +1,54 @@
1
+ require 'msgpack'
2
+
3
+ require 'hopper'
4
+
5
+ module Hopper
6
+ class Message
7
+ class Delivered
8
+ def initialize(queue, delivery_information, properties, payload)
9
+ @queue = queue
10
+ @delivery_information = delivery_information
11
+ @properties = properties
12
+ @message = Hopper::Message.new(payload)
13
+ end
14
+
15
+ attr_reader :message
16
+
17
+ class RetryableError < StandardError ; end
18
+
19
+ class FatalError < StandardError ; end
20
+
21
+ def acknowledge
22
+ #Hopper.logger.info "Acknowledging #{tag}"
23
+ @queue.acknowledge(self)
24
+ end
25
+
26
+ def retry
27
+ Hopper.logger.info "Re-enqueueing #{tag}"
28
+ @queue.retry(self)
29
+ end
30
+
31
+ def reject(requeue = nil)
32
+ if !requeue.nil?
33
+ Hopper.logger.warn "Hopper::Queue::Message#reject with requeue set to 'true' is deprecated."
34
+ if requeue
35
+ self.retry
36
+ else
37
+ self.reject
38
+ end
39
+ else
40
+ Hopper.logger.info "Rejecting #{tag}"
41
+ @queue.reject(self)
42
+ end
43
+ end
44
+
45
+ def terminate
46
+ @delivery_information.consumer.cancel
47
+ end
48
+
49
+ def tag
50
+ @delivery_information.delivery_tag
51
+ end
52
+ end
53
+ end
54
+ end