simple_pipeline 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/README.md +4 -0
- data/lib/simple_pipeline/timeout.rb +0 -6
- data/lib/simple_pipeline/version.rb +3 -3
- data/lib/simple_pipeline.rb +77 -34
- data/spec/simple_pipeline_spec.rb +52 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0841cc3eab093abcc6a954e3d24c1153de824d02'
|
4
|
+
data.tar.gz: 46f0b23b29b62843bbefbe73615ac318c1082214
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc93171e985aa842a237ddce20c0ab935d5bcb776a63153e3ca6429018435e8cae0dff833641ea6c39b6c6184d838df00d26610707b80acd731ec762cbe20015
|
7
|
+
data.tar.gz: eb66c3a879ef980ab1679a8e6b5150cc9c0680ac85d6664a1f5d255f40e92fa9a051d4771116a9bc956f143c1459c1657c28e478c36125c6356ed3a78486d143
|
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -90,5 +90,9 @@ pipeline.add pipe2
|
|
90
90
|
```
|
91
91
|
This will override the timeout value set by the class definition.
|
92
92
|
|
93
|
+
## Related Projects
|
94
|
+
|
95
|
+
* [PiecePipe](https://github.com/atomicobject/piece_pipe), an alternative implementation of the pipeline pattern.
|
96
|
+
|
93
97
|
## License
|
94
98
|
**SimplePipeline** is released under the [MIT license](MIT-LICENSE).
|
@@ -17,12 +17,6 @@ class SimplePipeline
|
|
17
17
|
@timeout_in_sec = sec
|
18
18
|
end
|
19
19
|
|
20
|
-
def process_with_timeout (payload)
|
21
|
-
::Timeout::timeout(timeout) {
|
22
|
-
process(payload)
|
23
|
-
}
|
24
|
-
end
|
25
|
-
|
26
20
|
def timeout
|
27
21
|
@timeout_in_sec || self.class.class_variable_get(:@@timeout_in_sec)
|
28
22
|
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
class SimplePipeline
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
1
|
+
class SimplePipeline
|
2
|
+
VERSION = "0.0.4"
|
3
|
+
end
|
data/lib/simple_pipeline.rb
CHANGED
@@ -1,35 +1,78 @@
|
|
1
|
-
require "simple_pipeline/timeout"
|
2
|
-
require "simple_pipeline/version"
|
3
|
-
|
4
|
-
|
5
|
-
class SimplePipeline
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
1
|
+
require "simple_pipeline/timeout"
|
2
|
+
require "simple_pipeline/version"
|
3
|
+
|
4
|
+
|
5
|
+
class SimplePipeline
|
6
|
+
|
7
|
+
attr_reader :errors
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@pipe_order = []
|
11
|
+
@pipe_config = {}
|
12
|
+
@errors = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def add (pipe, args = {})
|
16
|
+
begin
|
17
|
+
raise ArgumentError, "invalid pipe - incorrect number of arguments for process() method (should be 1)" unless pipe.class.instance_method(:process).arity == 1
|
18
|
+
rescue NameError
|
19
|
+
raise ArgumentError, "invalid pipe - process() method not found"
|
20
|
+
end
|
21
|
+
|
22
|
+
@pipe_order << pipe
|
23
|
+
@pipe_config[pipe] = args
|
24
|
+
|
25
|
+
return pipe
|
26
|
+
end
|
27
|
+
|
28
|
+
def size
|
29
|
+
return @pipe_order.size
|
30
|
+
end
|
31
|
+
|
32
|
+
def process (payload)
|
33
|
+
@pipe_order.each do |pipe|
|
34
|
+
begin
|
35
|
+
timeout = @pipe_config[pipe][:timeout]
|
36
|
+
|
37
|
+
if timeout.nil? && (pipe.is_a? SimplePipeline::Timeout)
|
38
|
+
timeout = pipe.timeout
|
39
|
+
end
|
40
|
+
|
41
|
+
if timeout.nil?
|
42
|
+
pipe.process(payload)
|
43
|
+
else
|
44
|
+
process_with_timeout(pipe, payload, timeout)
|
45
|
+
end
|
46
|
+
rescue
|
47
|
+
raise $! unless continue_on_error?($!, @pipe_config[pipe][:continue_on_error?])
|
48
|
+
@errors << $!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
alias :length :size
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def continue_on_error? (e, config_value)
|
58
|
+
return false if config_value.nil? || config_value == false
|
59
|
+
return true if config_value == true
|
60
|
+
|
61
|
+
if (config_value.is_a? Class) && (e.is_a? config_value)
|
62
|
+
return true
|
63
|
+
end
|
64
|
+
|
65
|
+
if config_value.is_a? Array
|
66
|
+
config_value.each { |klass| return true if (klass.is_a? Class) && (e.is_a? klass) }
|
67
|
+
end
|
68
|
+
|
69
|
+
return false
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def process_with_timeout (pipe, payload, timeout)
|
74
|
+
::Timeout::timeout(timeout) {
|
75
|
+
pipe.process(payload)
|
76
|
+
}
|
77
|
+
end
|
35
78
|
end
|
@@ -22,6 +22,18 @@ class TimeoutPipe
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
class TimeoutPipe2
|
26
|
+
def process (payload)
|
27
|
+
sleep 10 # seconds
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class ExceptionPipe
|
32
|
+
def process (payload)
|
33
|
+
raise ArgumentError.new "Something bad happened"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
25
37
|
describe SimplePipeline do
|
26
38
|
it "should support three normal pipes" do
|
27
39
|
pipeline = SimplePipeline.new
|
@@ -49,7 +61,7 @@ describe SimplePipeline do
|
|
49
61
|
}.to raise_error(ArgumentError)
|
50
62
|
end
|
51
63
|
|
52
|
-
it "should support pipes with timeout" do
|
64
|
+
it "should support pipes with timeout values" do
|
53
65
|
pipeline = SimplePipeline.new
|
54
66
|
pipe = TimeoutPipe.new
|
55
67
|
pipeline.add pipe
|
@@ -61,5 +73,44 @@ describe SimplePipeline do
|
|
61
73
|
expect {
|
62
74
|
pipeline.process({})
|
63
75
|
}.to raise_error(Timeout::Error)
|
76
|
+
|
77
|
+
pipeline = SimplePipeline.new
|
78
|
+
pipeline.add TimeoutPipe2.new, :timeout => 1
|
79
|
+
|
80
|
+
expect {
|
81
|
+
pipeline.process({})
|
82
|
+
}.to raise_error(Timeout::Error)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should support pipes with continue_on_error => true" do
|
86
|
+
pipeline = SimplePipeline.new
|
87
|
+
pipeline.add TestPipe.new
|
88
|
+
pipeline.add ExceptionPipe.new
|
89
|
+
pipeline.add TestPipe.new
|
90
|
+
|
91
|
+
payload = {:test_value => 10}
|
92
|
+
|
93
|
+
expect {
|
94
|
+
pipeline.process(payload)
|
95
|
+
}.to raise_error(ArgumentError)
|
96
|
+
|
97
|
+
expect(payload[:test_value]).to eq 100
|
98
|
+
expect(pipeline.errors.size).to eq 0
|
99
|
+
|
100
|
+
pipeline = SimplePipeline.new
|
101
|
+
pipeline.add TestPipe.new
|
102
|
+
pipeline.add ExceptionPipe.new, :continue_on_error? => true
|
103
|
+
pipeline.add ExceptionPipe.new, :continue_on_error? => ArgumentError
|
104
|
+
pipeline.add ExceptionPipe.new, :continue_on_error? => StandardError
|
105
|
+
pipeline.add ExceptionPipe.new, :continue_on_error? => [ArgumentError]
|
106
|
+
pipeline.add ExceptionPipe.new, :continue_on_error? => [RuntimeError, StandardError]
|
107
|
+
pipeline.add TestPipe.new
|
108
|
+
|
109
|
+
payload = {:test_value => 10}
|
110
|
+
|
111
|
+
pipeline.process(payload)
|
112
|
+
|
113
|
+
expect(payload[:test_value]).to eq 1000
|
114
|
+
expect(pipeline.errors.size).to eq 5
|
64
115
|
end
|
65
116
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_pipeline
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Wong
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|