frenzy_bunnies 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in frenzy_bunnies.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dotan Nahum
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,29 @@
1
+ # FrenzyBunnies
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'frenzy_bunnies'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install frenzy_bunnies
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'frenzy_bunnies'
3
+ require 'frenzy_bunnies/cli'
4
+
5
+ FrenzyBunnies::CLI.start
6
+
data/examples/feed.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'hot_bunnies'
3
+
4
+
5
+
6
+
7
+ connection = HotBunnies.connect(:host => 'localhost')
8
+ channel = connection.create_channel
9
+ channel.prefetch = 10
10
+
11
+ exchange = channel.exchange('frenzy_bunnies', :type => :direct, :durable => true)
12
+
13
+
14
+
15
+ 100_000.times do |i|
16
+ exchange.publish("hello world! #{i}", :routing_key => 'new.feeds')
17
+ end
18
+ puts "done"
19
+
20
+
@@ -0,0 +1,33 @@
1
+ $:<< File.expand_path('../lib', File.dirname(__FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'frenzy_bunnies'
5
+
6
+ class FeedWorker
7
+ include FrenzyBunnies::Worker
8
+ from_queue 'new.feeds', :prefetch => 20, :threads => 13, :durable => true
9
+
10
+ def work(msg)
11
+ puts msg
12
+ ack!
13
+ end
14
+ end
15
+
16
+ class FeedDownloader
17
+ include FrenzyBunnies::Worker
18
+ from_queue 'new.downloads', :durable => true
19
+ def work(msg)
20
+ puts msg
21
+ ack!
22
+ end
23
+ end
24
+
25
+ f = FrenzyBunnies::Context.new
26
+
27
+ f.run FeedWorker,FeedDownloader
28
+
29
+
30
+ trap "INT" do
31
+ f.stop
32
+ exit!
33
+ end
@@ -0,0 +1,21 @@
1
+ class FeedWorker
2
+ include FrenzyBunnies::Worker
3
+ from_queue 'new.feeds', :prefetch => 20, :threads => 13, :durable => true
4
+
5
+ def work(msg)
6
+ puts msg
7
+ ack!
8
+ end
9
+ end
10
+
11
+ class FeedDownloader
12
+ include FrenzyBunnies::Worker
13
+ from_queue 'new.downloads', :durable => true
14
+ def work(msg)
15
+ puts msg
16
+ ack!
17
+ end
18
+ end
19
+
20
+
21
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/frenzy_bunnies/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dotan Nahum"]
6
+ gem.email = ["jondotan@gmail.com"]
7
+ gem.description = %q{RabbitMQ JRuby based workers on top of hot_bunnies}
8
+ gem.summary = %q{RabbitMQ JRuby based workers on top of hot_bunnies}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "frenzy_bunnies"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = FrenzyBunnies::VERSION
17
+
18
+ gem.add_runtime_dependency 'hot_bunnies', '>= 1.4.0.pre3'
19
+ gem.add_runtime_dependency 'thor'
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'hot_bunnies'
2
+
3
+
4
+ module FrenzyBunnies
5
+ end
6
+
7
+
8
+ require "frenzy_bunnies/version"
9
+ require 'frenzy_bunnies/queue_factory'
10
+ require 'frenzy_bunnies/context'
11
+ require 'frenzy_bunnies/worker'
12
+
@@ -0,0 +1,29 @@
1
+ require 'thor'
2
+
3
+
4
+ class FrenzyBunnies::CLI < Thor
5
+ BUNNIES =<<-EOF
6
+
7
+ (\\___/)
8
+ (='.'=) Frenzy Bunnies!
9
+ (")_(") JRuby based workers on top of hot_bunnies
10
+
11
+ EOF
12
+
13
+ desc 'run', "run workers from a file"
14
+ def start_workers(workerfile)
15
+
16
+ require workerfile
17
+ # enumerate all workers
18
+ workers = []
19
+ ObjectSpace.each_object(Class){|o| workers << o if o.ancestors.map(&:name).include? "FrenzyBunnies::Worker"}
20
+ workers.uniq!
21
+
22
+ puts BUNNIES
23
+
24
+ c = FrenzyBunnies::Context.new
25
+ c.logger.info "Discovered #{workers.inspect}"
26
+ c.run *workers
27
+ Signal.trap('INT') { c.stop; exit! }
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ require 'logger'
2
+
3
+ class FrenzyBunnies::Context
4
+ attr_reader :queue_factory, :logger
5
+
6
+ def initialize(opts={})
7
+ @opts = opts
8
+ @opts[:host] ||= 'localhost'
9
+ @opts[:exchange] ||= 'frenzy_bunnies'
10
+ @opts[:heartbeat] ||= 5
11
+ @logger = @opts[:logger] || Logger.new(STDOUT)
12
+ @connection = HotBunnies.connect(:host => @opts[:host], :heartbeat_interval => @opts[:heartbeat])
13
+ @connection.add_shutdown_listener(lambda { |cause| @logger.error("Disconnected: #{cause}"); stop;})
14
+
15
+ @queue_factory = FrenzyBunnies::QueueFactory.new(@connection, @opts[:exchange])
16
+ end
17
+
18
+ def run(*klasses)
19
+ @klasses = []
20
+ klasses.each{|klass| klass.start(self); @klasses << klass}
21
+ end
22
+
23
+ def stop
24
+ @klasses.each{|klass| klass.stop }
25
+ end
26
+ end
27
+
@@ -0,0 +1,17 @@
1
+ class FrenzyBunnies::QueueFactory
2
+ def initialize(connection, exchange)
3
+ @connection = connection
4
+ @exchange = exchange
5
+ end
6
+
7
+ def build_queue(name, prefetch, durable)
8
+ channel = @connection.create_channel
9
+ channel.prefetch = prefetch
10
+
11
+ exchange = channel.exchange(@exchange, :type => :direct, :durable => durable)
12
+
13
+ queue = channel.queue(name, :durable => durable)
14
+ queue.bind(exchange, :routing_key => name)
15
+ queue
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module FrenzyBunnies
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,73 @@
1
+ module FrenzyBunnies::Worker
2
+ import java.util.concurrent.Executors
3
+
4
+ def ack!
5
+ true
6
+ end
7
+ def work
8
+
9
+ end
10
+
11
+ def self.included(base)
12
+ base.extend ClassMethods
13
+ end
14
+
15
+ module ClassMethods
16
+ def from_queue(q, opts={})
17
+ @queue_name = q
18
+ @queue_opts = opts
19
+ end
20
+
21
+ def start(context)
22
+ @logger = context.logger
23
+
24
+ @queue_opts[:prefetch] ||= 10
25
+ @queue_opts[:durable] ||= false
26
+
27
+ if @queue_opts[:threads]
28
+ @thread_pool = Executors.new_fixed_thread_pool(@queue_opts[:threads])
29
+ else
30
+ @thread_pool = Executors.new_cached_thread_pool
31
+ end
32
+
33
+ q = context.queue_factory.build_queue(@queue_name, @queue_opts[:prefetch], @queue_opts[:durable])
34
+ @s = q.subscribe(:ack => true)
35
+
36
+ say "#{@queue_opts[:threads] ? "#{@queue_opts[:threads]} threads " : ''}with #{@queue_opts[:prefetch]} prefetch on <#{@queue_name}>."
37
+
38
+ @s.each(:blocking => false, :executor => @thread_pool) do |h, msg|
39
+ wkr = new
40
+ begin
41
+ if(wkr.work(msg))
42
+ h.ack
43
+ else
44
+ h.reject
45
+ error "Cannot process message <#{msg.inspect}>"
46
+ end
47
+ rescue
48
+ h.reject
49
+ error "ERROR #{$!}"
50
+ end
51
+ end
52
+
53
+ say "workers up."
54
+ end
55
+
56
+ def stop
57
+ say "stopping"
58
+ @thread_pool.shutdown_now
59
+ say "pool shutdown"
60
+ # @s.cancel #for some reason when the channel socket is broken, this is holding the process up and we're zombie.
61
+ say "stopped"
62
+ end
63
+
64
+ def say(text)
65
+ @logger.info "[#{self.name}] #{text}"
66
+ end
67
+
68
+ def error(text)
69
+ @logger.error "[#{self.name}] #{text}"
70
+ end
71
+ end
72
+ end
73
+
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: frenzy_bunnies
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dotan Nahum
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hot_bunnies
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.0.pre3
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.4.0.pre3
30
+ - !ruby/object:Gem::Dependency
31
+ name: thor
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: RabbitMQ JRuby based workers on top of hot_bunnies
47
+ email:
48
+ - jondotan@gmail.com
49
+ executables:
50
+ - frenzy_bunnies
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - bin/frenzy_bunnies
60
+ - examples/feed.rb
61
+ - examples/feed_worker.rb
62
+ - examples/feed_workers_bin.rb
63
+ - frenzy_bunnies.gemspec
64
+ - lib/frenzy_bunnies.rb
65
+ - lib/frenzy_bunnies/cli.rb
66
+ - lib/frenzy_bunnies/context.rb
67
+ - lib/frenzy_bunnies/queue_factory.rb
68
+ - lib/frenzy_bunnies/version.rb
69
+ - lib/frenzy_bunnies/worker.rb
70
+ homepage: ''
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.23
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: RabbitMQ JRuby based workers on top of hot_bunnies
94
+ test_files: []