get_back 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Joe Kutner
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.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ get_back
2
+ ==========
3
+
4
+ Get_Back is a library for making a Ruby method run in the background. But it only works with JRuby.
5
+
6
+ Examples
7
+ ---------
8
+
9
+ Lets say you want to send an email as part of your sign up process. But you don't want that to run synchronously cause
10
+ it could take too long. Make it get back!
11
+
12
+ require 'get_back'
13
+
14
+ class User
15
+ extend GetBack::JoJo
16
+
17
+ def send_email(action)
18
+ puts "Thanks for #{action} my website"
19
+ end
20
+
21
+ get_back :send_email
22
+
23
+ end
24
+
25
+ Now the send_email method will *always* run in a separate thread.
26
+
27
+ The background methods are run in a separate thread. The threads a pulled from a pool. If you need to limit the
28
+ concurrency of a method, you can fix the pool size:
29
+
30
+ get_back :send_email, :pool => 10
31
+
32
+ You can also specify callbacks for success, rescue, and ensure. The success callback is just a block thats passed to
33
+ the get_back call:
34
+
35
+ get_back :send_email do
36
+ log.info "email send successfully
37
+ end
38
+
39
+ The rescue and ensure callbacks reference other methods
40
+
41
+ get_back :send_email, :rescue => :log_bad_email
42
+
43
+ def log_bad_email(e) do
44
+ log.error "something bad happened when we sent an email!"
45
+ end
46
+
47
+ Or you can send more email:
48
+
49
+ get_back :send_mail, :ensure => :send_spam
50
+
51
+ def send_spam
52
+ send_email("spamming")
53
+ end
54
+
55
+ Copyright
56
+ ----------
57
+
58
+ Copyright © 2011 Joe Kutner. Released under the MIT License.
59
+
60
+ See LICENSE for details.
data/get_back.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{get_back}
5
+ s.version = "0.0.1"
6
+ s.authors = ["Joe Kutner"]
7
+ s.date = Time.now
8
+ s.description = "Easy Background Jobs for JRuby"
9
+ s.email = ["jpkutner@gmail.com"]
10
+ s.files = Dir['{lib,spec}/**/*'] + Dir['{*.md,*.txt,*.gemspec}']
11
+ s.homepage = "http://github.com/jkutner/get_back"
12
+ s.require_paths = ["lib"]
13
+ s.summary = "Easy Background Jobs for JRuby"
14
+ s.test_files = Dir["spec/*_spec.rb"]
15
+ s.platform = "java"
16
+ end
@@ -0,0 +1,56 @@
1
+ require 'java'
2
+
3
+ module GetBack
4
+ module JoJo
5
+ @@executors = java.util.Collections.synchronizedList([])
6
+
7
+ at_exit { @@executors.each {|e| e.shutdown} }
8
+
9
+ def get_back(method, config={}, &block)
10
+ with_executor_service(config[:pool]) do |e|
11
+
12
+ # give the new method a name
13
+ gb_method_name = "_get_back_#{method}_".to_sym
14
+
15
+ # create a new proxy
16
+ old_method = instance_method(method)
17
+ define_method(gb_method_name) do |*args|
18
+ old_method.bind(self).call(*args)
19
+ end
20
+
21
+ # redefine the method
22
+ define_method(method) do |*args|
23
+ e.submit do
24
+ begin
25
+ r = send(gb_method_name, *args)
26
+ if block_given?
27
+ if block.arity == 1
28
+ yield self
29
+ else
30
+ yield self, r
31
+ end
32
+ end
33
+ rescue Exception => ex
34
+ send(config[:rescue], ex) if config[:rescue]
35
+ raise
36
+ ensure
37
+ send(config[:ensure]) if config[:ensure]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def with_executor_service(pool_size=nil)
47
+ if pool_size.nil?
48
+ e = java.util.concurrent.Executors.newCachedThreadPool
49
+ else
50
+ e = java.util.concurrent.Executors.newFixedThreadPool(pool_size.to_i)
51
+ end
52
+ @@executors << e
53
+ yield e
54
+ end
55
+ end
56
+ end
data/lib/get_back.rb ADDED
@@ -0,0 +1 @@
1
+ require 'get_back/jojo'
@@ -0,0 +1,149 @@
1
+ require '../lib/get_back'
2
+
3
+ class Foo
4
+ extend GetBack::JoJo
5
+
6
+ attr_accessor :step1, :step2, :step3
7
+
8
+ def initialize
9
+ @step1 = false
10
+ @step2 = false
11
+ @step3 = false
12
+ end
13
+
14
+ def do_it
15
+ sleep(1) until @step1 == true
16
+ @step2 = true
17
+ end
18
+ get_back :do_it
19
+
20
+ def do_it_fixed
21
+ sleep(1) until @step1 == true
22
+ @step2 = true
23
+ end
24
+ get_back :do_it_fixed, :pool => 2
25
+
26
+ def do_it_success
27
+ sleep(1) until @step1 == true
28
+ @step2 = true
29
+ end
30
+ get_back :do_it_success do |f|
31
+ f.step3 = true
32
+ end
33
+
34
+ def do_it_rescue
35
+ sleep(1) until @step1 == true
36
+ @step2 = true
37
+ raise "Some Error"
38
+ end
39
+ get_back :do_it_rescue, :rescue => :callback1
40
+
41
+ def callback1(e)
42
+ @step3 = true
43
+ end
44
+
45
+ def do_it_ensure
46
+ sleep(1) until @step1 == true
47
+ @step2 = true
48
+ raise "Some Error"
49
+ end
50
+ get_back :do_it_ensure, :ensure => :callback0
51
+
52
+ def callback0
53
+ @step3 = true
54
+ end
55
+
56
+ def do_it_args(a, b, c)
57
+ sleep(1) until @step1 == true
58
+ @step2 = a && b && c
59
+ end
60
+ get_back :do_it_args
61
+ end
62
+
63
+ describe Foo do
64
+
65
+ context "#do_it" do
66
+ it "runs in the background" do
67
+ subject.do_it
68
+ sleep(2)
69
+ subject.step1.should be_false
70
+ subject.step2.should be_false
71
+ subject.step1 = true
72
+ sleep(1)
73
+ subject.step2.should be_true
74
+ subject.step1.should be_true
75
+ end
76
+ end
77
+
78
+ context "#do_it_args" do
79
+ it "runs in the background" do
80
+ subject.do_it_args(true, true, true)
81
+ sleep(2)
82
+ subject.step1.should be_false
83
+ subject.step2.should be_false
84
+ subject.step1 = true
85
+ sleep(1)
86
+ subject.step2.should be_true
87
+ subject.step1.should be_true
88
+ end
89
+ end
90
+
91
+ context "#do_it_fixed" do
92
+ it "runs in the background" do
93
+ subject.do_it_fixed
94
+ sleep(2)
95
+ subject.step1.should be_false
96
+ subject.step2.should be_false
97
+ subject.step1 = true
98
+ sleep(1)
99
+ subject.step2.should be_true
100
+ subject.step1.should be_true
101
+ end
102
+ end
103
+
104
+ context "#do_it_success" do
105
+ it "runs in the background" do
106
+ subject.do_it_success
107
+ sleep(2)
108
+ subject.step1.should be_false
109
+ subject.step2.should be_false
110
+ subject.step3.should be_false
111
+ subject.step1 = true
112
+ sleep(1)
113
+ subject.step2.should be_true
114
+ subject.step1.should be_true
115
+ subject.step3.should be_true
116
+ end
117
+ end
118
+
119
+ context "#do_it_rescue" do
120
+ it "runs in the background" do
121
+ subject.do_it_rescue
122
+ sleep(2)
123
+ subject.step1.should be_false
124
+ subject.step2.should be_false
125
+ subject.step3.should be_false
126
+ subject.step1 = true
127
+ sleep(1)
128
+ subject.step2.should be_true
129
+ subject.step1.should be_true
130
+ subject.step3.should be_true
131
+ end
132
+ end
133
+
134
+ context "#do_it_ensure" do
135
+ it "runs in the background" do
136
+ subject.do_it_ensure
137
+ sleep(2)
138
+ subject.step1.should be_false
139
+ subject.step2.should be_false
140
+ subject.step3.should be_false
141
+ subject.step1 = true
142
+ sleep(1)
143
+ subject.step2.should be_true
144
+ subject.step1.should be_true
145
+ subject.step3.should be_true
146
+ end
147
+ end
148
+ end
149
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: get_back
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: java
7
+ authors:
8
+ - Joe Kutner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-11 00:00:00.000000000 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Easy Background Jobs for JRuby
16
+ email:
17
+ - jpkutner@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/get_back.rb
23
+ - lib/get_back/jojo.rb
24
+ - spec/happy_path_spec.rb
25
+ - README.md
26
+ - LICENSE.txt
27
+ - get_back.gemspec
28
+ has_rdoc: true
29
+ homepage: http://github.com/jkutner/get_back
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ none: false
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ none: false
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.5.1
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Easy Background Jobs for JRuby
53
+ test_files:
54
+ - spec/happy_path_spec.rb
55
+ ...