juggler 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ .DS_Store
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Martyn Loughran
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ Add jobs for asynchronous processing
2
+
3
+ Juggler.throw(:method, params)
4
+
5
+ Add handlers, with optional concurrency, inside and EM loop
6
+
7
+ EM.run {
8
+ Juggler.juggle(:method, 10) do |params|
9
+ # This code must return an eventmachine deferrable object
10
+ end
11
+ }
12
+
13
+ For example
14
+
15
+ Juggler.juggle(:download, 10) do |params|
16
+ http = EM::Protocols::HttpClient.request({
17
+ :host => params[:host],
18
+ :port => 80,
19
+ :request => params[:path]
20
+ })
21
+ http.callback do |response|
22
+ puts "Got response status #{response[:status]} for #{a}"
23
+ end
24
+ http
25
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "juggler"
8
+ gem.summary = %Q{Juggling background jobs with EventMachine and Beanstalkd}
9
+ gem.description = %Q{Juggling background jobs with EventMachine and Beanstalkd}
10
+ gem.email = "me@mloughran.com"
11
+ gem.homepage = "http://github.com/mloughran/juggler"
12
+ gem.authors = ["Martyn Loughran"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'juggler'
4
+
5
+ EM.run {
6
+ Juggler.juggle(:http, 3) do |path|
7
+ http = EM::Protocols::HttpClient.request({
8
+ :host => "0.0.0.0",
9
+ :port => 3000,
10
+ :request => path
11
+ })
12
+ http.callback do |response|
13
+ puts "Got response status #{response[:status]} and body \"#{response[:content]}\""
14
+ end
15
+
16
+ http
17
+ end
18
+
19
+ Juggler.juggle(:timer, 5) do |params|
20
+ defer = EM::DefaultDeferrable.new
21
+
22
+ EM::Timer.new(1) do
23
+ defer.set_deferred_status :succeeded, nil
24
+ # defer.set_deferred_status :failed, nil
25
+ end
26
+
27
+ defer.callback do
28
+ puts "Timer ended (params #{params.inspect})"
29
+ end
30
+
31
+ defer.errback do
32
+ puts "Timer failed"
33
+ end
34
+
35
+ defer
36
+ end
37
+ }
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'juggler'
4
+
5
+ # Throw some jobs
6
+
7
+ 10.times do |i|
8
+ path = ['/fast', '/slow'][i % 2]
9
+ Juggler.throw(:http, path)
10
+ end
11
+ 10.times { Juggler.throw(:timer, {:foo => 'bar'}) }
@@ -0,0 +1,24 @@
1
+ require 'beanstalk-client'
2
+ require 'eventmachine'
3
+
4
+ class Juggler
5
+ class << self
6
+ def throw(method, params, options = {})
7
+ # TODO: Do some checking on the method
8
+ connection.use(method.to_s)
9
+ connection.put(Marshal.dump(params))
10
+ end
11
+
12
+ def juggle(method, concurrency = 1, &strategy)
13
+ Runner.new(method, concurrency, strategy).run
14
+ end
15
+
16
+ private
17
+
18
+ def connection
19
+ @connection ||= Beanstalk::Pool.new('localhost:11300')
20
+ end
21
+ end
22
+ end
23
+
24
+ require 'juggler/runner'
@@ -0,0 +1,58 @@
1
+ class Juggler
2
+ class Runner
3
+ class << self
4
+ def start
5
+ @started ||= begin
6
+ Signal.trap('INT') { EM.stop }
7
+ Signal.trap('TERM') { EM.stop }
8
+ true
9
+ end
10
+ end
11
+ end
12
+
13
+ def initialize(method, concurrency, strategy)
14
+ @strategy = strategy
15
+ @concurrency = concurrency
16
+ @queue = method.to_s
17
+ @running = []
18
+ end
19
+
20
+ def reserve
21
+ beanstalk_job = connection.reserve(0)
22
+ params = Marshal.load(beanstalk_job.body)
23
+ job = @strategy.call(params)
24
+ @running << job
25
+ job.callback do
26
+ @running.delete(job)
27
+ beanstalk_job.delete
28
+ end
29
+ job.errback do
30
+ @running.delete(job)
31
+ # Built in exponential backoff
32
+ beanstalk_job.decay
33
+ end
34
+ rescue Beanstalk::TimedOut
35
+ end
36
+
37
+ def run
38
+ EM.add_periodic_timer do
39
+ reserve if spare_slot?
40
+ end
41
+ Runner.start
42
+ end
43
+
44
+ private
45
+
46
+ def spare_slot?
47
+ @running.size < @concurrency
48
+ end
49
+
50
+ def connection
51
+ @pool ||= begin
52
+ pool = Beanstalk::Pool.new('localhost:11300')
53
+ pool.watch(@queue)
54
+ pool
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Juggler do
4
+ it "should put jobs on queue" do
5
+ Juggler.throw('method', {:foo => "bar"})
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Juggler::Runner do
4
+ it "should delete job from beanstalkd when successful" do
5
+ @mock_beanstalk = mock(Beanstalk::Pool)
6
+ @mock_job = mock(Beanstalk::Job, :body => "foo")
7
+ Beanstalk::Pool.should_receive(:new).and_return(@mock_beanstalk)
8
+
9
+ @mock_job.should_receive(:delete)
10
+
11
+ Juggler::Runner.new(:method, 1, lambda do
12
+ deferrable = EM::DefaultDeferrable.new
13
+ deferrable.set_deferred_status :succeeded, nil
14
+ deferrable
15
+ end).run
16
+
17
+ sleep 1
18
+ end
19
+
20
+ it "should retry job if not successful"
21
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'juggler'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: juggler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Martyn Loughran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-19 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Juggling background jobs with EventMachine and Beanstalkd
17
+ email: me@mloughran.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.md
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - VERSION
31
+ - examples/test_consumer.rb
32
+ - examples/test_producer.rb
33
+ - lib/juggler.rb
34
+ - lib/juggler/runner.rb
35
+ - spec/juggler_spec.rb
36
+ - spec/spec_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/mloughran/juggler
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Juggling background jobs with EventMachine and Beanstalkd
65
+ test_files:
66
+ - spec/juggler_spec.rb
67
+ - spec/runner_spec.rb
68
+ - spec/spec_helper.rb
69
+ - examples/test_consumer.rb
70
+ - examples/test_producer.rb