tribe 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9abc5cc35384044da534b226cd17e0366dfa4f5
4
- data.tar.gz: ba406bee8ead723972a90f8693487a8abb641517
3
+ metadata.gz: c4d9186b00b4266dcc7d99ec5181c8506fbf36b0
4
+ data.tar.gz: 5d575e91b713c118adadc90e712f7a0404ec652b
5
5
  SHA512:
6
- metadata.gz: ffc2e47d2ad02dca648aeaada4466668122328c92f5100150dbe6870e3d8df84fcd67c6d8d483e2d3dab27834d9fcafc4ee9fa3c2f555c05d41f491064b78cd4
7
- data.tar.gz: 0609663a8f9ca5c7ee107ee57512385bd3064a53fa050427090bfb0ff55120694a83f327ee04f531a4e0e0461509dfcff3533d9ccecca98fdf505605003f2dc5
6
+ metadata.gz: 1143d749e801940accf16ec273b88620c7d879dc4ac051591a3d20656f2e6ae5475eddda269c09b56556f9bef802272df634ce0d694e2da6890051ef95310505
7
+ data.tar.gz: 03a921647d620c4483ed4a157e82180dfccaba51c18949ece240b090c888fcf6ec90313ee25532e590610fa79174a1550176e94daae1ef2d673af730db73dd48
data/README.md CHANGED
@@ -471,6 +471,19 @@ To support in the tens of thousands, hundreds of thousands, or potentially milli
471
471
  # The pool size is back to the default size.
472
472
  puts "Pool size (after): #{Workers.pool.size}"
473
473
 
474
+ ## Logging
475
+
476
+ Tribe provides a shared instance of ````Logger```` for your convenience:
477
+
478
+ Tribe.logger
479
+
480
+ Every actor also has access to it through the ````logger```` convenience method.
481
+ This local instance of the logger is wrapped in a proxy for your convenience.
482
+ This way your code can assume the logger exists even if ````Tribe.logger```` is set to ````nil````.
483
+
484
+ By default, the logger will log to STDOUT.
485
+ You should change this to a file in your application.
486
+
474
487
  ## Debugging
475
488
 
476
489
  Tribe is written in pure Ruby so it will work with all existing debuggers that support Ruby & threads.
data/lib/tribe.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'set'
2
+ require 'logger'
3
+ require 'thread'
2
4
 
3
5
  require 'workers'
4
6
 
@@ -16,11 +18,36 @@ require 'tribe/future'
16
18
  require 'tribe/root'
17
19
 
18
20
  module Tribe
21
+ @lock = Monitor.new
22
+
23
+ class << self
24
+ attr_reader :lock
25
+ end
26
+
19
27
  def self.registry
20
- return @registry ||= Tribe::Registry.new
28
+ @lock.synchronize do
29
+ @registry ||= Tribe::Registry.new
30
+ end
21
31
  end
22
32
 
23
33
  def self.root
24
- @root ||= Tribe::Root.new(:name => 'root', :permit_root => true)
34
+ @lock.synchronize do
35
+ @root ||= Tribe::Root.new(:name => 'root', :permit_root => true)
36
+ end
37
+ end
38
+
39
+ def self.logger
40
+ @lock.synchronize do
41
+ @logger
42
+ end
43
+ end
44
+
45
+ def self.logger=(val)
46
+ @lock.synchronize do
47
+ @logger = val
48
+ end
25
49
  end
26
50
  end
51
+
52
+ Tribe.logger = Logger.new(STDOUT)
53
+ Tribe.logger.level = Logger::DEBUG
data/lib/tribe/actable.rb CHANGED
@@ -2,25 +2,26 @@ module Tribe
2
2
  module Actable
3
3
  include Workers::Helpers
4
4
 
5
+ private
6
+
5
7
  #
6
8
  # Initialization method.
7
9
  # Notes: Call this in your constructor.
8
10
  #
9
11
 
10
- private
11
-
12
12
  def init_actable(options = {})
13
13
  # Symbols aren't GCed in JRuby so force string names.
14
14
  if options[:name] && !options[:name].is_a?(String)
15
15
  raise Tribe::ActorNameError.new('Name must be a string.')
16
16
  end
17
17
 
18
- @logger = Workers::LogProxy.new(options[:logger])
19
18
  @_actable = Tribe::ActorState.new
19
+
20
20
  @_actable.dedicated = options[:dedicated] || false
21
21
  @_actable.pool = @_actable.dedicated ? Workers::Pool.new(:size => 1) : (options[:pool] || Workers.pool)
22
22
  @_actable.mailbox = Tribe::Mailbox.new(@_actable.pool)
23
23
  @_actable.registry = options[:registry] || Tribe.registry
24
+ @_actable.logger = Workers::LogProxy.new(options[:logger] || Tribe.logger)
24
25
  @_actable.scheduler = options[:scheduler] || Workers.scheduler
25
26
  @_actable.name = options[:name]
26
27
  @_actable.parent = options[:parent]
@@ -47,38 +48,21 @@ module Tribe
47
48
  process_events
48
49
  end
49
50
 
50
- return nil
51
+ nil
51
52
  end
52
53
 
53
54
  def direct_message!(command, data = nil, src = nil)
54
55
  deliver_event!(Tribe::Event.new(command, data, src))
55
56
 
56
- return nil
57
- end
58
-
59
- def message!(dest, command, data = nil)
60
- event = Tribe::Event.new(command, data, self)
61
-
62
- dest.deliver_event!(event)
63
-
64
- return nil
65
- end
66
-
67
- def future!(dest, command, data = nil)
68
- event = Tribe::Event.new(command, data, self)
69
- event.future = future = Tribe::Future.new(self)
70
-
71
- dest.deliver_event!(event)
72
-
73
- return future
57
+ nil
74
58
  end
75
59
 
76
60
  def shutdown!
77
- return direct_message!(:__shutdown__)
61
+ direct_message!(:__shutdown__)
78
62
  end
79
63
 
80
64
  def perform!(&block)
81
- return direct_message!(:__perform__, block)
65
+ direct_message!(:__perform__, block)
82
66
  end
83
67
 
84
68
  def spawn!(klass, actor_options = {}, spawn_options = {})
@@ -102,7 +86,7 @@ module Tribe
102
86
  @_actable.supervisees.add(child)
103
87
  end
104
88
 
105
- return child
89
+ child
106
90
  end
107
91
 
108
92
  def alive?
@@ -114,23 +98,27 @@ module Tribe
114
98
  end
115
99
 
116
100
  def name
117
- return @_actable.name
101
+ @_actable.name
118
102
  end
119
103
 
120
104
  def identifier
121
- return @_actable.name ? "#{object_id}:#{@_actable.name}" : object_id
105
+ @_actable.name ? "#{object_id}:#{@_actable.name}" : object_id
122
106
  end
123
107
 
124
108
  def exception
125
- return @_actable.exception
109
+ @_actable.exception
126
110
  end
127
111
 
128
112
  def registry
129
- return @_actable.registry
113
+ @_actable.registry
130
114
  end
131
115
 
132
116
  def pool
133
- return @_actable.pool
117
+ @_actable.pool
118
+ end
119
+
120
+ def logger
121
+ @_actable.logger
134
122
  end
135
123
 
136
124
  #
@@ -179,7 +167,7 @@ module Tribe
179
167
  process_events
180
168
  end
181
169
 
182
- return nil
170
+ nil
183
171
  end
184
172
 
185
173
  def event_handler(event)
@@ -220,7 +208,7 @@ module Tribe
220
208
  @_actable.active_event = nil
221
209
  end
222
210
 
223
- return nil
211
+ nil
224
212
  end
225
213
 
226
214
  def initialize_handler(event)
@@ -236,9 +224,14 @@ module Tribe
236
224
  @_actable.children.clear
237
225
  @_actable.supervisees.clear
238
226
 
227
+ log_exception_handler(exception)
239
228
  on_exception(Event.new(:exception, {:exception => exception}))
240
229
 
241
- return nil
230
+ nil
231
+ end
232
+
233
+ def log_exception_handler(exception)
234
+ logger.error("EXCEPTION: #{exception.message}\n#{exception.backtrace.join("\n")}\n--")
242
235
  end
243
236
 
244
237
  def shutdown_handler(event)
@@ -252,13 +245,13 @@ module Tribe
252
245
 
253
246
  on_shutdown(Event.new(:shutdown, {}))
254
247
 
255
- return nil
248
+ nil
256
249
  end
257
250
 
258
251
  def perform_handler(event)
259
252
  event.data.call
260
253
 
261
- return nil
254
+ nil
262
255
  end
263
256
 
264
257
  def cleanup_handler(exception = nil)
@@ -268,7 +261,7 @@ module Tribe
268
261
  @_actable.registry.unregister(self)
269
262
  @_actable.timers.each { |t| t.cancel } if @_actable.timers
270
263
 
271
- return nil
264
+ nil
272
265
  end
273
266
 
274
267
  def child_died_handler(child, exception)
@@ -281,7 +274,7 @@ module Tribe
281
274
  raise Tribe::ActorChildDied.new("#{child.identifier} died.")
282
275
  end
283
276
 
284
- return nil
277
+ nil
285
278
  end
286
279
 
287
280
  def child_shutdown_handler(child)
@@ -290,14 +283,14 @@ module Tribe
290
283
 
291
284
  on_child_shutdown(Event.new(:child_shutdown, {:child => child}))
292
285
 
293
- return nil
286
+ nil
294
287
  end
295
288
 
296
289
  def parent_died_handler(parent, exception)
297
290
  on_parent_died(Event.new(:parent_died, {:parent => parent, :exception => exception}))
298
291
  raise Tribe::ActorParentDied.new("#{parent.identifier} died.")
299
292
 
300
- return nil
293
+ nil
301
294
  end
302
295
 
303
296
  #
@@ -307,6 +300,23 @@ module Tribe
307
300
 
308
301
  private
309
302
 
303
+ def message!(dest, command, data = nil)
304
+ event = Tribe::Event.new(command, data, self)
305
+
306
+ dest.deliver_event!(event)
307
+
308
+ nil
309
+ end
310
+
311
+ def future!(dest, command, data = nil)
312
+ event = Tribe::Event.new(command, data, self)
313
+ event.future = future = Tribe::Future.new(self)
314
+
315
+ dest.deliver_event!(event)
316
+
317
+ future
318
+ end
319
+
310
320
  def timer!(delay, command, data = nil)
311
321
  timer = Workers::Timer.new(delay, :scheduler => @_actable.scheduler) do
312
322
  @_actable.timers.delete(timer)
@@ -315,7 +325,7 @@ module Tribe
315
325
 
316
326
  @_actable.timers.add(timer)
317
327
 
318
- return timer
328
+ timer
319
329
  end
320
330
 
321
331
  def periodic_timer!(delay, command, data = nil)
@@ -329,19 +339,19 @@ module Tribe
329
339
 
330
340
  @_actable.timers.add(timer)
331
341
 
332
- return timer
342
+ timer
333
343
  end
334
344
 
335
345
  def forward!(dest)
336
346
  dest.deliver_event!(@_actable.active_event)
337
347
  @_actable.active_event = nil
338
348
 
339
- return nil
349
+ nil
340
350
  end
341
351
 
342
352
  # Wrap blocking code using this method to automatically expand/contract the pool.
343
- # This way you avoid potential deadlock with blocking code.
344
- # Not needed for dedicated actors since they already have their own thread.
353
+ # This way you avoid potential thread starvation. Not needed for dedicated actors
354
+ # since they already have their own thread.
345
355
  def blocking!
346
356
  if @_actable.dedicated
347
357
  yield
@@ -12,5 +12,6 @@ module Tribe
12
12
  attr_accessor :children
13
13
  attr_accessor :supervisees
14
14
  attr_accessor :exception
15
+ attr_accessor :logger
15
16
  end
16
17
  end
data/lib/tribe/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tribe
2
- VERSION = '0.6.1'
2
+ VERSION = '0.6.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tribe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Remesch