eventr 0.1.0 → 0.2.5

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30f604978c9fe74a9839039595bfe5a2b78565c5
4
+ data.tar.gz: 9acc64eab183b11feecb1255e2553ba4f4a44ef9
5
+ SHA512:
6
+ metadata.gz: c4730cf4133c2c091859a13cffde0819cdd7648acb32962ab01e02fa09ad7031098f1e809254d98c3dd49c15e812a87bf9dfc81e19e5ae02731185b07fb2f6bd
7
+ data.tar.gz: 8f36a3634be23fd82ea2d2698bbeaf864808f4299b79ddbc874ad01edd562da891012f20260ebcb734575ad05b1f03fdb8ebe69dfaff86cbca441ceea889cbd8
@@ -0,0 +1,67 @@
1
+ AllCops:
2
+ Includes:
3
+ - Rakefile
4
+ Excludes:
5
+ - test.rb
6
+ - bin/**
7
+
8
+
9
+ SpecialGlobalVars:
10
+ Enabled: false
11
+
12
+ PerlBackrefs:
13
+ Enabled: false
14
+
15
+ Documentation:
16
+ Enabled: false
17
+
18
+ BlockComments:
19
+ Enabled: false
20
+
21
+ Encoding:
22
+ Enabled: false
23
+
24
+ RegexpLiteral:
25
+ Enabled: false
26
+
27
+ # dots at the end of lines are okay
28
+ DotPosition:
29
+ Enabled: false
30
+
31
+ MethodLength:
32
+ CountComments: false
33
+ Max: 15
34
+
35
+ ParameterLists:
36
+ Max: 5
37
+ CountKeywordArgs: true
38
+
39
+ BlockNesting:
40
+ Max: 4
41
+
42
+ CollectionMethods:
43
+ PreferredMethods:
44
+ map: 'collect'
45
+ map!: 'collect!'
46
+ reduce: 'inject'
47
+ detect: 'find'
48
+ find_all: 'select'
49
+
50
+ CaseIndentation:
51
+ IndentWhenRelativeTo: end
52
+ IndentOneStep: true
53
+
54
+ LineLength:
55
+ Enabled: false
56
+
57
+ # Personally, I prefer to outdent public/protected/private, as it makes
58
+ # it easier to see the different sections of code.
59
+ AccessModifierIndentation:
60
+ EnforcedStyle: outdent
61
+
62
+ EmptyLinesAroundAccessModifier:
63
+ Enabled: true
64
+
65
+ EndAlignment:
66
+ AlignWith: variable
67
+
@@ -16,9 +16,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
16
16
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
  =end
18
18
 
19
- require "eventr/version"
20
- require 'eventr/errors'
19
+ require 'eventr/version'
21
20
 
22
21
  module Eventr
23
- autoload :Coordinator, 'eventr/coordinator'
22
+ autoload :Consumer, 'eventr/actors'
23
+ autoload :Publisher, 'eventr/actors'
24
+ autoload :SupervisedObject, 'eventr/actors'
25
+ autoload :Coordinator, 'eventr/coordinator'
26
+ autoload :Error, 'eventr/errors'
24
27
  end
@@ -21,6 +21,8 @@ require 'monitor'
21
21
 
22
22
  module Eventr
23
23
  class SupervisedObject
24
+ attr_reader :on_exception
25
+
24
26
  def stop
25
27
  threads.values.each { |t| t.send :kill }
26
28
  end
@@ -30,27 +32,31 @@ module Eventr
30
32
  start_supervisor_thread
31
33
  end
32
34
 
33
- def threads() @threads ||= {}; end
34
- def application() threads[:application]; end
35
- def supervisor() threads[:supervisor]; end
35
+ def threads
36
+ @threads ||= {}
37
+ end
36
38
 
37
- def on_exception
38
- @on_exception
39
+ def application
40
+ threads[:application]
39
41
  end
40
42
 
41
- def on_exception=(&block)
43
+ def supervisor
44
+ threads[:supervisor]
45
+ end
46
+
47
+ def on_exception=(&block) # rubocop:disable TrivialAccessors
42
48
  @on_exception = block
43
49
  end
44
50
 
45
51
  def sleep_time_from_backoff
46
52
  backoff = Thread.current[:backoff] || 0
47
- (0..backoff).inject([1,0]) { |(a,b), _| [b, a+b] }[0]
53
+ (0..backoff).inject([1, 0]) { |(a, b), _| [b, a + b] }[0]
48
54
  end
49
55
 
50
56
  def start_application_thread
51
- threads[:application] ||= Thread.new {
57
+ threads[:application] ||= Thread.new do
52
58
  begin
53
- main()
59
+ main
54
60
  rescue StandardError => e
55
61
  on_exception.call(e) if on_exception.respond_to? :call
56
62
  warn "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
@@ -58,17 +64,17 @@ module Eventr
58
64
  ensure
59
65
  threads[:supervisor].wakeup # wakeup the supervisor to help us recover
60
66
  end
61
- }
67
+ end
62
68
  end
63
69
 
64
- def start_supervisor_thread
65
- threads[:supervisor] ||= Thread.new {
70
+ def start_supervisor_thread # rubocop:disable MethodLength
71
+ threads[:supervisor] ||= Thread.new do
66
72
  Thread.current[:backoff] = 1
67
73
 
68
74
  begin
69
75
  runs = 5
70
- loop {
71
- unless (application && application.alive?)
76
+ loop do
77
+ unless application && application.alive?
72
78
  puts "#{self.class.name}::Supervisor: cleaning up app thread and restarting it."
73
79
  threads[:application] = nil
74
80
  start_application_thread
@@ -87,7 +93,7 @@ module Eventr
87
93
  end
88
94
 
89
95
  sleep 1
90
- }
96
+ end
91
97
 
92
98
  rescue StandardError => e
93
99
  warn "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
@@ -101,11 +107,12 @@ module Eventr
101
107
  end
102
108
 
103
109
  # if the supervisor goes away, take the whole thing down.
104
- threads[:application].raise SupervisorDown, "supervisor went away due to: #{e.class.name}: #{e.message} -> #{e.backtrace.first}"
110
+ error_msg = "supervisor went away due to: #{e.class.name}: #{e.message} -> #{e.backtrace.first}"
111
+ threads[:application].raise Error::SupervisorDown, error_msg
105
112
 
106
113
  raise e
107
114
  end
108
- }
115
+ end
109
116
  end
110
117
  end
111
118
 
@@ -114,18 +121,28 @@ module Eventr
114
121
  private :block, :events
115
122
 
116
123
  def initialize(&block)
117
- @block = block
124
+ @block = block || method(:default_loop)
118
125
  @events = Queue.new
119
126
  end
120
127
 
121
- def pop(non_block=false)
128
+ def pop(non_block = false)
122
129
  events.pop(non_block)
123
130
  end
124
- alias :shift :pop
131
+ alias_method :shift, :pop
125
132
 
126
133
  def main
127
134
  block.call(events)
128
135
  end
136
+
137
+ def push(event)
138
+ @events << event
139
+ end
140
+ alias_method :publish, :push
141
+
142
+ def default_loop(events)
143
+ loop { Thread.stop }
144
+ end
145
+ private :default_loop
129
146
  end
130
147
 
131
148
  class Consumer < SupervisedObject
@@ -141,5 +158,4 @@ module Eventr
141
158
  loop { block.call(publisher.pop) }
142
159
  end
143
160
  end
144
-
145
161
  end
@@ -15,9 +15,7 @@ You should have received a copy of the GNU General Public License along
15
15
  with this program; if not, write to the Free Software Foundation, Inc.,
16
16
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
  =end
18
-
19
- require 'eventr/errors'
20
- require 'eventr/actors'
18
+ require 'singleton'
21
19
 
22
20
  module Eventr
23
21
  class Coordinator
@@ -31,30 +29,48 @@ module Eventr
31
29
  end
32
30
 
33
31
  def self.method_missing(method, *args, &block)
34
- return super unless self.instance.respond_to?(method)
32
+ return super unless instance.public_method.include? method
35
33
  instance.public_send(method, *args, &block)
36
34
  end
37
35
 
36
+ def publishers(*queues)
37
+ queues.each { |queue| publisher(queue) }
38
+ end
39
+
38
40
  def publisher(queue_name, &block)
39
- raise InvalidQueue, "publisher already defined for queue '#{queue_name}'" if @publishers.include? queue_name
41
+ if @publishers.include? queue_name
42
+ fail Error::InvalidQueue, "publisher already defined for queue '#{queue_name}'"
43
+ end
40
44
  @publishers[queue_name] = Publisher.new(&block)
41
45
  end
42
46
 
47
+ def publish(queue_name, event)
48
+ unless @publishers.include? queue_name
49
+ fail Error::InvalidQueue, "Publisher #{queue_name.inspect} doesn't exist"
50
+ end
51
+ @publishers[queue_name].push(event)
52
+ end
53
+
43
54
  def consumer(queue_name, &block)
44
- raise InvalidQueue, "#{queue_name} queue does not exist. Define a publisher for the queue first." unless @publishers.include? queue_name
55
+ unless @publishers.include? queue_name
56
+ fail Error::InvalidQueue, "#{queue_name} queue does not exist. Define a publisher for the queue first."
57
+ end
45
58
  @consumers[queue_name] ||= []
46
59
  @consumers[queue_name] << Consumer.new(@publishers[queue_name], &block)
47
60
  end
48
61
 
49
- def start
50
- @publishers.each do |queue_name, _pubisher|
51
- _pubisher.start
52
- @consumers[queue_name].each { |c| c.start }
62
+ def start # rubycop:disable
63
+ @publishers.each do |q, p|
64
+ p.start
65
+ @consumers[q].each { |c| c.start }
53
66
  end
54
67
  end
55
68
 
56
69
  def stop
57
- @publishers.each { |q, p| p.stop; @consumers[q].each { |c| c.stop } }
70
+ @publishers.each do |q, p|
71
+ p.stop
72
+ @consumers[q].each { |c| c.stop }
73
+ end
58
74
  end
59
75
  end
60
76
  end
@@ -17,7 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
17
17
  =end
18
18
 
19
19
  module Eventr
20
- class Error < ::StandardError; end
21
- class InvalidQueue < Error; end
22
- class SupervisorDown < Error; end
20
+ class Error < ::StandardError
21
+ class InvalidQueue < Error; end
22
+ class SupervisorDown < Error; end
23
+ end
23
24
  end
@@ -17,5 +17,5 @@ with this program; if not, write to the Free Software Foundation, Inc.,
17
17
  =end
18
18
 
19
19
  module Eventr
20
- VERSION = "0.1.0"
20
+ VERSION = '0.2.5'
21
21
  end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Carl P. Corliss
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-11 00:00:00.000000000 Z
11
+ date: 2014-03-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: A threaded publish/subscribe object, for internal application events.
@@ -67,7 +60,8 @@ extensions: []
67
60
  extra_rdoc_files:
68
61
  - README.md
69
62
  files:
70
- - .gitignore
63
+ - ".gitignore"
64
+ - ".rubocop.yml"
71
65
  - Gemfile
72
66
  - LICENSE
73
67
  - README.md
@@ -81,27 +75,25 @@ files:
81
75
  homepage: http://github.com/rabbitt/eventr
82
76
  licenses:
83
77
  - GPLv2
78
+ metadata: {}
84
79
  post_install_message:
85
80
  rdoc_options: []
86
81
  require_paths:
87
82
  - lib
88
83
  required_ruby_version: !ruby/object:Gem::Requirement
89
- none: false
90
84
  requirements:
91
- - - ! '>='
85
+ - - ">="
92
86
  - !ruby/object:Gem::Version
93
87
  version: '0'
94
88
  required_rubygems_version: !ruby/object:Gem::Requirement
95
- none: false
96
89
  requirements:
97
- - - ! '>='
90
+ - - ">="
98
91
  - !ruby/object:Gem::Version
99
92
  version: '0'
100
93
  requirements: []
101
94
  rubyforge_project:
102
- rubygems_version: 1.8.23
95
+ rubygems_version: 2.2.2
103
96
  signing_key:
104
- specification_version: 3
97
+ specification_version: 4
105
98
  summary: Internal PubSub event object
106
99
  test_files: []
107
- has_rdoc: