spox-processmailer 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +165 -0
- data/README.rdoc +89 -0
- data/lib/processmailer.rb +1 -0
- data/lib/processmailer/Actions.rb +8 -0
- data/lib/processmailer/Exceptions.rb +22 -0
- data/lib/processmailer/LogHelper.rb +32 -0
- data/lib/processmailer/PostOffice.rb +175 -0
- data/lib/processmailer/Postbox.rb +91 -0
- metadata +75 -0
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.rdoc
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
= Important note
|
2
|
+
|
3
|
+
This library is in an alpha state and still being developed and tested
|
4
|
+
|
5
|
+
= ProcessMailer
|
6
|
+
|
7
|
+
The idea is simple. Mail data to processes.
|
8
|
+
|
9
|
+
== What?
|
10
|
+
|
11
|
+
The ProcessMailer mails data. To processes. (Oh, it also allows you to send data to hooks, which uses a thread pool and doesn't restrict memory access, but that's not the point of this library!) So, the better question might be, why? Well, for one, you can make things faster. See, what's great about this library is that you encapsulate your logic in little processes that have been forked. Data is then packaged up and sent to these processes where the processes can then do what ever they please to that piece of data. This simple design, though some what more complicated in use, allows you to make use of multiple cpus/cores where using something like a thread pool would keep you limited to a single core.
|
12
|
+
|
13
|
+
== How does it work?
|
14
|
+
|
15
|
+
Well, the general overview is something like this:
|
16
|
+
|
17
|
+
Main<||>---------PostOffice<>----------*Postbox
|
18
|
+
|
19
|
+
In crappy, texty, UMLy fashion, what we have is a Main program composed of a PostOffice. That PostOffice has an aggregation of zero or more Postboxes it knows about. That's the basic structure.
|
20
|
+
|
21
|
+
== How about an example?
|
22
|
+
|
23
|
+
Sure, lets make something simple. We will start out using blocks:
|
24
|
+
|
25
|
+
require 'processmailer'
|
26
|
+
|
27
|
+
po = ProcessMailer::PostOffice.new
|
28
|
+
po.register{|a| puts "Decrement: #{a}"; a -= 1; a}
|
29
|
+
po.register{|a| puts "Increment: #{a}"; a += 1; a}
|
30
|
+
|
31
|
+
po.deliver(1)
|
32
|
+
sleep(2)
|
33
|
+
po.clean
|
34
|
+
|
35
|
+
First, we create our PostOffice. Next, we register two Postboxes using blocks. The argument is the Object that will be delivered via the PostOffice. After registering two Postboxes, we deliver a Fixnum. The PostOffice delivers messages to registered Postboxes using a thread pool, so our main line of execution will continue. So, lets sleep for a couple seconds to see some output generated. Finally, we clean the PostOffice which removes all registered Postboxes.
|
36
|
+
|
37
|
+
== Blocks suck
|
38
|
+
|
39
|
+
Well, not really, but they can be a bit restrictive. For something a little more robust, we create a class that's a subclass of Postbox, and let it do the work:
|
40
|
+
|
41
|
+
require 'processmailer'
|
42
|
+
|
43
|
+
class Test < ProcessMailer::Postbox
|
44
|
+
def initialize(args)
|
45
|
+
super
|
46
|
+
@foo = 2
|
47
|
+
end
|
48
|
+
def process(obj)
|
49
|
+
puts "Decrement: #{obj}"
|
50
|
+
obj -= @foo
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
po = ProcessMailer::PostOffice.new
|
55
|
+
po.register(Test)
|
56
|
+
po.register{|a| puts "Increment: #{a}"; a += 1; a}
|
57
|
+
|
58
|
+
po.deliver(1)
|
59
|
+
sleep(2)
|
60
|
+
po.clean
|
61
|
+
|
62
|
+
This is doing the same basic thing we did in the blocks. The Test class is simply a subclass of Postbox, and the process method is overridden to do the grunt work. The reason we send in a Class type, and not the instantiated object, is to allow the PostOffice to do the proper creation and forking. That's pretty much it.
|
63
|
+
|
64
|
+
== One last thing
|
65
|
+
|
66
|
+
The result of the process method or block for a given Postbox will be returned to the PostOffice for delivery. That is why the examples run
|
67
|
+
continuously. Returning a nil value will stop delivery. This means that the example below will only output two lines:
|
68
|
+
|
69
|
+
require 'processmailer'
|
70
|
+
|
71
|
+
po = ProcessMailer::PostOffice.new
|
72
|
+
po.register{|a| puts "Decrement: #{a}"; nil}
|
73
|
+
po.register{|a| puts "Increment: #{a}"; nil}
|
74
|
+
|
75
|
+
po.deliver(1)
|
76
|
+
sleep(2)
|
77
|
+
po.clean
|
78
|
+
|
79
|
+
|
80
|
+
== ASCII Representation:
|
81
|
+
|
82
|
+
[PostOffice]------[message]---->[Postbox]
|
83
|
+
^ |
|
84
|
+
-----------[return val]-------'
|
85
|
+
|
86
|
+
== Informations
|
87
|
+
|
88
|
+
Author: spox <spox@modspox.com>
|
89
|
+
License: LGPLv3
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'processmailer/PostOffice'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ProcessMailer
|
2
|
+
module Exceptions
|
3
|
+
class InvalidType < Exception
|
4
|
+
attr_reader :expected
|
5
|
+
attr_reader :received
|
6
|
+
def initialize(e,r)
|
7
|
+
@expected = e
|
8
|
+
@received = r
|
9
|
+
end
|
10
|
+
def to_s
|
11
|
+
"Expected type: #{e}. Received type: #{r}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
class EmptyParameters < Exception
|
15
|
+
def to_s
|
16
|
+
'Parameters passed are empty'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
class Resync < Exception
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module ProcessMailer
|
4
|
+
|
5
|
+
class LogHelper
|
6
|
+
|
7
|
+
def initialize(logger=nil)
|
8
|
+
@logger = logger
|
9
|
+
end
|
10
|
+
|
11
|
+
def info(m)
|
12
|
+
@logger.info(m) unless @logger.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def warn(m)
|
16
|
+
@logger.warn(m) unless @logger.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def fatal(m)
|
20
|
+
@logger.fatal(m) unless @logger.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
def error(m)
|
24
|
+
@logger.error(m) unless @logger.nil?
|
25
|
+
end
|
26
|
+
|
27
|
+
def raw
|
28
|
+
return @logger
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'actionpool'
|
2
|
+
require 'processmailer/Postbox'
|
3
|
+
require 'processmailer/Exceptions'
|
4
|
+
require 'processmailer/LogHelper'
|
5
|
+
|
6
|
+
module ProcessMailer
|
7
|
+
# The PostOffice is the driver of the
|
8
|
+
# process mailer. Its job is to accept
|
9
|
+
# objects which are then passed off to
|
10
|
+
# each Postbox, which may or may not process
|
11
|
+
# the object. Messages returned to the
|
12
|
+
# PostOffice by a Postbox will then be
|
13
|
+
# sent to any proc that has hooked into
|
14
|
+
# the type.
|
15
|
+
class PostOffice
|
16
|
+
# args:: setup hash
|
17
|
+
# :max_threads:: default number of threads per Postbox
|
18
|
+
# :min_threads:: minimum number of threads per Postbox
|
19
|
+
# :thread_to:: maximum time thread is allowed to idle
|
20
|
+
# :action_to:: maximum time a thread may work on an action
|
21
|
+
# :logger:: Logger to use
|
22
|
+
# :pool:: ActionPool for PostOffice to utilize (not used by Postboxes)
|
23
|
+
# Sets up a PostOffice to handle message delivery.
|
24
|
+
def initialize(args={})
|
25
|
+
default_args(args)
|
26
|
+
@max_workers = args[:max_threads]
|
27
|
+
@min_workers = args[:min_threads]
|
28
|
+
@logger = LogHelper.new(args[:logger])
|
29
|
+
@thread_to = args[:thread_to]
|
30
|
+
@action_to = args[:action_to]
|
31
|
+
@postboxes = {} # {PID => {:read => rd, :write => wr}}
|
32
|
+
@hooks = {} # {Some::Class => []}
|
33
|
+
@readers = []
|
34
|
+
@close_postoffice = false
|
35
|
+
@processor = Thread.new{listen}
|
36
|
+
@pool = args[:pool] ? args[:pool] : nil
|
37
|
+
end
|
38
|
+
# obj:: Serializable object for delivery
|
39
|
+
# Delivers object to Postboxes for processing
|
40
|
+
def deliver(obj)
|
41
|
+
s = [Marshal.dump(obj)].pack('m')
|
42
|
+
@postboxes.each_value{|pipes| pipes[:write].puts s}
|
43
|
+
call_hooks(obj)
|
44
|
+
end
|
45
|
+
# pb:: Class name of custom Postbox
|
46
|
+
# Registers a new Postbox with the PostOffice. Returns Postbox process ID
|
47
|
+
def register(pb=nil, &block)
|
48
|
+
raise Exceptions::InvalidType.new(Class, pb.class) unless pb.nil? || pb.is_a?(Class)
|
49
|
+
raise Exceptions::EmptyParameters.new if pb.nil? && !block_given?
|
50
|
+
r,w = IO.pipe
|
51
|
+
pid = nil
|
52
|
+
if(block_given?)
|
53
|
+
pid = Kernel.fork do
|
54
|
+
box = Postbox.new(:proc => block, :read_pipe => r, :write_pipe => w, :max_threads => @max_workers,
|
55
|
+
:min_threads => @min_workers, :thread_to => @thread_to, :action_to => @action_to,
|
56
|
+
:logger => @logger.raw)
|
57
|
+
Signal.trap('HUP'){ box.close }
|
58
|
+
box.listen
|
59
|
+
end
|
60
|
+
else
|
61
|
+
pid = Kernel.fork do
|
62
|
+
box = pb.new(:read_pipe => r, :write_pipe => w, :max_threads => @max_workers,
|
63
|
+
:min_threads => @min_workers, :thread_to => @thread_to,
|
64
|
+
:action_to => @action_to, :logger => @logger.raw)
|
65
|
+
Signal.trap('HUP'){ box.close }
|
66
|
+
box.listen
|
67
|
+
end
|
68
|
+
end
|
69
|
+
if(pid)
|
70
|
+
@postboxes[pid] = {:read => r, :write => w}
|
71
|
+
@readers << r
|
72
|
+
@processor.raise Exceptions::Resync.new
|
73
|
+
return pid
|
74
|
+
end
|
75
|
+
end
|
76
|
+
# pid:: Postbox delivery address (Process ID)
|
77
|
+
# Removes a Postbox from the PostOffice
|
78
|
+
def unregister(pid)
|
79
|
+
pid = pid.to_i
|
80
|
+
raise Exception.new('Failed to locate process') unless @postboxes.has_key?(pid)
|
81
|
+
pipes = @postboxes.delete(pid)
|
82
|
+
Process.kill('HUP', pid)
|
83
|
+
@readers.delete(pipes[:read])
|
84
|
+
@processor.raise Exceptions::Resync.new
|
85
|
+
pipes[:write].puts "stop"
|
86
|
+
Process.waitpid(pid)
|
87
|
+
end
|
88
|
+
# c:: Class
|
89
|
+
# action:: callable block (Proc/lambda)
|
90
|
+
# block:: code block
|
91
|
+
# Add a hook to a given object type. When
|
92
|
+
# the PostOffice receives the object, the
|
93
|
+
# block will be called. Return an ID to be
|
94
|
+
# used when unhooking
|
95
|
+
#
|
96
|
+
# Example:
|
97
|
+
# po.hook(Array){|obj| puts obj.join(', ')}
|
98
|
+
def hook(c, action=nil, &block)
|
99
|
+
b = action.nil? ? block : action
|
100
|
+
raise Exceptions::EmptyParameters.new if b.nil?
|
101
|
+
raise Exceptions::InvalidType.new(Class, c.class) unless c.is_a?(Class)
|
102
|
+
@hooks[c] = Array.new unless @hooks[c]
|
103
|
+
@hooks[c] << b
|
104
|
+
@pool = ActionPool::Pool.new(1, 5, nil, nil, @logger.raw) if @pool.nil?
|
105
|
+
return @hooks[c].index(b)
|
106
|
+
end
|
107
|
+
# c:: Class
|
108
|
+
# hid:: ID from hook()
|
109
|
+
# Unhook a block from object delivery
|
110
|
+
def unhook(c, hid)
|
111
|
+
raise Exceptions::EmptyParameters.new unless c.is_a?(Class)
|
112
|
+
raise Exceptions::EmptyParameters.new unless hid.is_a?(Integer)
|
113
|
+
@hooks[c].delete_at(hid)
|
114
|
+
@hooks.delete(c) if @hooks[c].empty?
|
115
|
+
end
|
116
|
+
|
117
|
+
# Returns hash of hooks currently in the PostOffice
|
118
|
+
def hooks
|
119
|
+
@hooks
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns hash of PostBoxes currently in the PostOffice
|
123
|
+
def postboxes
|
124
|
+
@postboxes
|
125
|
+
end
|
126
|
+
# Stop all processes
|
127
|
+
def clean
|
128
|
+
@postboxes.each_key{|k| unregister(k)}
|
129
|
+
end
|
130
|
+
private
|
131
|
+
def listen
|
132
|
+
until(@close_postoffice) do
|
133
|
+
begin
|
134
|
+
s = Kernel.select(@readers, nil, nil, nil)
|
135
|
+
for sock in s[0] do
|
136
|
+
if(sock.closed?)
|
137
|
+
close_on_socket(sock)
|
138
|
+
else
|
139
|
+
string = sock.gets
|
140
|
+
deliver(Marshal.load(string.unpack('m')[0])) unless string.nil?
|
141
|
+
end
|
142
|
+
end
|
143
|
+
rescue Exceptions::Resync
|
144
|
+
# resync sockets #
|
145
|
+
rescue Object => boom
|
146
|
+
@logger.error("PostOffice error encountered reading message: #{boom}")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
def call_hooks(obj)
|
151
|
+
obj.class.ancestors.each do |klass|
|
152
|
+
if(@hooks[klass])
|
153
|
+
@hooks[klass].each do |hook|
|
154
|
+
@pool.process do
|
155
|
+
result = nil
|
156
|
+
begin
|
157
|
+
result = hook.call(obj)
|
158
|
+
rescue Object => boom
|
159
|
+
@logger.warn("Hook generated an error: #{boom}")
|
160
|
+
result = boom
|
161
|
+
ensure
|
162
|
+
deliver(result) unless result.nil?
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
def default_args(args)
|
170
|
+
{:max_threads => 5, :min_threads => 1, :thread_to => nil, :action_to => nil, :logger => nil}.each_pair{|k,v|
|
171
|
+
args[k] = v unless args.has_key?(k)
|
172
|
+
}
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'actionpool'
|
2
|
+
require 'processmailer/Exceptions'
|
3
|
+
require 'processmailer/LogHelper'
|
4
|
+
|
5
|
+
module ProcessMailer
|
6
|
+
class Postbox
|
7
|
+
# Initialize the Postbox. Subclasses should
|
8
|
+
# super(), preferably first thing
|
9
|
+
def initialize(args)
|
10
|
+
default_args(args)
|
11
|
+
@pipes = {:read => args[:read_pipe], :write => args[:write_pipe]}
|
12
|
+
@proc = args[:proc] ? args[:proc] : lambda{nil}
|
13
|
+
@pool = ActionPool::Pool.new(args[:min_threads], args[:max_threads], args[:thread_to], args[:action_to], args[:logger])
|
14
|
+
@logger = LogHelper.new(args[:logger])
|
15
|
+
@stop = false
|
16
|
+
end
|
17
|
+
# Close the postbox for delivery
|
18
|
+
def close
|
19
|
+
@stop = true
|
20
|
+
end
|
21
|
+
# read:: read IO.pipe
|
22
|
+
# write:: write IO.pipe
|
23
|
+
# Install pipes for Postbox (Generally
|
24
|
+
# used by the PostOffice)
|
25
|
+
def install_pipe(read, write)
|
26
|
+
@pipes[:read] = read
|
27
|
+
@pipes[:write] = write
|
28
|
+
end
|
29
|
+
# obj:: object to be processed
|
30
|
+
# Process an object. This method should
|
31
|
+
# be overridden in subclasses.
|
32
|
+
def process(obj)
|
33
|
+
return @proc.call(obj)
|
34
|
+
end
|
35
|
+
# Listen for incoming messages from PostOffice
|
36
|
+
def listen
|
37
|
+
until(@stop) do
|
38
|
+
begin
|
39
|
+
Kernel.select([@pipes[:read]], nil, nil, nil)
|
40
|
+
receive
|
41
|
+
rescue Exceptions::Resync
|
42
|
+
# resync sockets #
|
43
|
+
rescue Object => boom
|
44
|
+
@logger.error("Postbox encountered error on listen: #{boom}")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
private
|
49
|
+
def receive
|
50
|
+
@logger.info("Postbox (#{self}) has message waiting")
|
51
|
+
begin
|
52
|
+
s = @pipes[:read].gets
|
53
|
+
@logger.info("Postbox (#{self}) received an empty message") if s.nil? || s.empty?
|
54
|
+
return if s.nil? || s.empty?
|
55
|
+
if(s.strip == 'stop')
|
56
|
+
@logger.info("Postbox (#{self}) received a stop instruction")
|
57
|
+
return
|
58
|
+
end
|
59
|
+
@logger.info("Postbox (#{self}) message: #{s}")
|
60
|
+
obj = s.size > 0 ? Marshal.load(s.unpack('m')[0]) : nil
|
61
|
+
@logger.info("Postbox (#{self}) message reconstructed: #{obj}")
|
62
|
+
run_process(obj)
|
63
|
+
rescue Object => boom
|
64
|
+
@logger.error("Postbox encountered error on receive: #{boom}")
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
def send(obj)
|
69
|
+
return if obj.nil?
|
70
|
+
@pipes[:write].puts [Marshal.dump(obj)].pack('m')
|
71
|
+
end
|
72
|
+
def run_process(obj)
|
73
|
+
@pool.process do
|
74
|
+
result = nil
|
75
|
+
begin
|
76
|
+
result = process(obj)
|
77
|
+
rescue Object => boom
|
78
|
+
@logger.warn("Postbox contents generated exception on call: #{boom}")
|
79
|
+
result = boom
|
80
|
+
end
|
81
|
+
send(result)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
def default_args(args)
|
85
|
+
{:read_pipe => nil, :write_pipe => nil, :proc => nil, :max_threads => 5,
|
86
|
+
:min_threads => 1, :thread_to => nil, :action_to => nil, :logger => nil}.each_pair{|k,v|
|
87
|
+
args[k] = v unless args.has_key?(k)
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spox-processmailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- spox
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ActionPool
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: ProcessMailer for concurrent data processing
|
26
|
+
email: spox@modspox.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- LICENSE
|
34
|
+
files:
|
35
|
+
- README.rdoc
|
36
|
+
- LICENSE
|
37
|
+
- lib/processmailer.rb
|
38
|
+
- lib/processmailer/Actions.rb
|
39
|
+
- lib/processmailer/Exceptions.rb
|
40
|
+
- lib/processmailer/LogHelper.rb
|
41
|
+
- lib/processmailer/Postbox.rb
|
42
|
+
- lib/processmailer/PostOffice.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/spox/processmailer
|
45
|
+
licenses:
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --title
|
49
|
+
- ProcessMailer
|
50
|
+
- --main
|
51
|
+
- README.rdoc
|
52
|
+
- --line-numbers
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 1.8.6
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 2
|
73
|
+
summary: Process Post Office
|
74
|
+
test_files: []
|
75
|
+
|