ismasan-proto_processor 0.2.0 → 0.3.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.
- data/README.markdown +24 -16
- data/VERSION.yml +1 -1
- data/lib/proto_processor.rb +1 -2
- data/lib/proto_processor/strategy.rb +2 -0
- data/lib/proto_processor/task.rb +11 -8
- metadata +2 -2
data/README.markdown
CHANGED
@@ -1,15 +1,19 @@
|
|
1
|
-
|
1
|
+
## proto_processor
|
2
2
|
|
3
3
|
A couple of modules to ease the creation of background processors or any kind of delegated task or tasks for which you want flow control and error reporting.
|
4
4
|
|
5
|
-
|
5
|
+
## Install
|
6
6
|
|
7
|
-
|
7
|
+
sudo gem install ismasan-proto_processor
|
8
|
+
|
9
|
+
## Examples
|
10
|
+
|
11
|
+
### Strategies
|
8
12
|
|
9
13
|
You start by defining a *strategy*. A strategy defines the sequence of tasks your program will carry out.
|
10
14
|
|
11
|
-
require 'rubygems'
|
12
|
-
require 'proto_processor'
|
15
|
+
require 'rubygems'
|
16
|
+
require 'proto_processor'
|
13
17
|
|
14
18
|
class ImageStrategy
|
15
19
|
include ProtoProcessor::Strategy
|
@@ -44,7 +48,7 @@ Strategy#run captures the output of tasks and gives you a Report object, with in
|
|
44
48
|
|
45
49
|
Apart from the *process* method, Strategies are normal Ruby classes so you can go ahead and add whatever functionality you want to them, including, for example, an *initialize* method, or a factory.
|
46
50
|
|
47
|
-
|
51
|
+
### Tasks
|
48
52
|
|
49
53
|
Tasks do the real work. They also implement a *process* method where you can put you image resizing, file storing, email sending or whatever code you want.
|
50
54
|
|
@@ -65,7 +69,7 @@ That's pretty much it. Any exceptions raised within *process* will be captured,
|
|
65
69
|
|
66
70
|
Every task has an *options* hash available.
|
67
71
|
|
68
|
-
|
72
|
+
### Validating task options
|
69
73
|
|
70
74
|
The previous example shows a simple way of checking that a task was passed required parameters as part of the options hash.
|
71
75
|
|
@@ -75,9 +79,9 @@ If parameters declared in this way are not present in the options, a ProtoProces
|
|
75
79
|
|
76
80
|
You can also raise manually in the process method.
|
77
81
|
|
78
|
-
|
79
|
-
|
80
|
-
|
82
|
+
def process
|
83
|
+
raise ArgumentError, ":width option must be > 0" if options[:width] < 1
|
84
|
+
end
|
81
85
|
|
82
86
|
Tasks also have *before_process* and *after_process* callbacks that will run if defined. You can guess what they do :)
|
83
87
|
|
@@ -87,7 +91,7 @@ Lastly, you can define your own *validate* method which will be run before proce
|
|
87
91
|
(1..500).include? options[:width]
|
88
92
|
end
|
89
93
|
|
90
|
-
|
94
|
+
### Chaining tasks in strategies
|
91
95
|
|
92
96
|
run_task [CropTask, ResizeTask, ZipTask, EmailTask], options
|
93
97
|
|
@@ -111,7 +115,7 @@ You can use task chains to process elements in a collection:
|
|
111
115
|
|
112
116
|
If any task in the chain fails the error will be logged. The following tasks will not be processed.
|
113
117
|
|
114
|
-
|
118
|
+
### Stand alone tasks
|
115
119
|
|
116
120
|
Tasks are quite simple objects. They expect an array with an input, an options hash and a report hash as an argument and return the same.
|
117
121
|
|
@@ -129,14 +133,18 @@ If a task expects a certain key in the report passed from a previous task in a c
|
|
129
133
|
|
130
134
|
If :some_key doesn't exist in the passed report, the task will not process and halt any chain it is in.
|
131
135
|
|
132
|
-
|
136
|
+
### Logging
|
133
137
|
|
134
138
|
Default to STDOUT. Just add your own logger and logger level.
|
135
139
|
|
136
|
-
ProtoProcessor.logger = Logger.new('my_processor.log')
|
137
|
-
ProtoProcessor.logger.level = Logger::ERROR
|
140
|
+
ProtoProcessor.logger = Logger.new('my_processor.log')
|
141
|
+
ProtoProcessor.logger.level = Logger::ERROR
|
142
|
+
|
143
|
+
## TODO
|
138
144
|
|
145
|
+
* Improve DSL in strategies
|
146
|
+
* Better log formatting?
|
139
147
|
|
140
|
-
|
148
|
+
## Copyright
|
141
149
|
|
142
150
|
Copyright (c) 2009 Ismael Celis. See LICENSE for details.
|
data/VERSION.yml
CHANGED
data/lib/proto_processor.rb
CHANGED
@@ -10,9 +10,11 @@ module ProtoProcessor::Strategy
|
|
10
10
|
|
11
11
|
def run
|
12
12
|
begin
|
13
|
+
ProtoProcessor.logger.info "Running strategy #{self.class.name}"
|
13
14
|
process
|
14
15
|
rescue StandardError => e
|
15
16
|
report.fail!(e)
|
17
|
+
ProtoProcessor.logger.error e.class.name
|
16
18
|
ProtoProcessor.logger.debug e.message + "\n" + e.backtrace.join("\n")
|
17
19
|
end
|
18
20
|
yield report if block_given?
|
data/lib/proto_processor/task.rb
CHANGED
@@ -26,7 +26,6 @@ module ProtoProcessor
|
|
26
26
|
def self.included(base)
|
27
27
|
base.class_eval do
|
28
28
|
attr_reader :input, :options, :report, :error
|
29
|
-
puts "Including validations in #{base.name}"
|
30
29
|
extend Validations
|
31
30
|
end
|
32
31
|
#base.extend Validations
|
@@ -42,15 +41,15 @@ module ProtoProcessor
|
|
42
41
|
@error = nil
|
43
42
|
end
|
44
43
|
|
45
|
-
class HaltedChainError < StandardError
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
44
|
+
# class HaltedChainError < StandardError
|
45
|
+
# def message
|
46
|
+
# "Task not run because previous task failed"
|
47
|
+
# end
|
48
|
+
# end
|
50
49
|
|
51
50
|
def run
|
52
51
|
begin
|
53
|
-
|
52
|
+
log_halt_task and return false if report[:status] == FAILURE
|
54
53
|
run_validations!
|
55
54
|
before_process
|
56
55
|
process
|
@@ -64,7 +63,7 @@ module ProtoProcessor
|
|
64
63
|
report!(:status, FAILURE)
|
65
64
|
report!(:error, {:name => e.class.name, :message => e.message})
|
66
65
|
@error = e
|
67
|
-
ProtoProcessor.logger.
|
66
|
+
ProtoProcessor.logger.error "#{self.class.name}: #{e.class.name} => #{e.message}"
|
68
67
|
ProtoProcessor.logger.debug e.backtrace.join("\n")
|
69
68
|
end
|
70
69
|
[@input, @options, @report]
|
@@ -109,6 +108,10 @@ module ProtoProcessor
|
|
109
108
|
|
110
109
|
protected
|
111
110
|
|
111
|
+
def log_halt_task
|
112
|
+
ProtoProcessor.logger.info "#{self.class.name} not run because previous task failed"
|
113
|
+
end
|
114
|
+
|
112
115
|
def validate
|
113
116
|
# implement this in subclasses if needed
|
114
117
|
true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ismasan-proto_processor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-26 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|