tribe 0.0.11 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -2
- data/README.md +37 -0
- data/lib/tribe/actable.rb +41 -0
- data/lib/tribe/safe_set.rb +40 -0
- data/lib/tribe/version.rb +1 -1
- data/lib/tribe.rb +3 -0
- data/tribe.gemspec +1 -1
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -90,6 +90,43 @@ In general you shouldn't have to create your own since there is a global one (Tr
|
|
90
90
|
:name => nil # The name of the actor (must be unique in the registry).
|
91
91
|
)
|
92
92
|
|
93
|
+
## Timers
|
94
|
+
|
95
|
+
Actors can create timers to perform some work in the future.
|
96
|
+
Both one-shot and periodic timers are provides.
|
97
|
+
|
98
|
+
class MyActor < Tribe::Actor
|
99
|
+
private
|
100
|
+
def initialize(options = {})
|
101
|
+
super
|
102
|
+
|
103
|
+
timer(1, :timer, Time.now)
|
104
|
+
periodic_timer(1, :periodic_timer, Time.now)
|
105
|
+
end
|
106
|
+
|
107
|
+
def on_timer(event)
|
108
|
+
puts "MyActor (#{identifier}) ONE-SHOT: #{event.data}"
|
109
|
+
end
|
110
|
+
|
111
|
+
def on_periodic_timer(event)
|
112
|
+
puts "MyActor (#{identifier}) PERIODIC: #{event.data}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# Create some named actors.
|
117
|
+
10.times do |i|
|
118
|
+
MyActor.new(:name => "my_actor_#{i}")
|
119
|
+
end
|
120
|
+
|
121
|
+
# Sleep in order to observe the timers.
|
122
|
+
sleep 10
|
123
|
+
|
124
|
+
# Shutdown the actors.
|
125
|
+
10.times do |i|
|
126
|
+
actor = Tribe.registry["my_actor_#{i}"]
|
127
|
+
actor.enqueue(:shutdown)
|
128
|
+
end
|
129
|
+
|
93
130
|
## TODO - missing features
|
94
131
|
|
95
132
|
- Futures.
|
data/lib/tribe/actable.rb
CHANGED
@@ -7,6 +7,8 @@ module Tribe
|
|
7
7
|
@dedicated = options[:dedicated] || false
|
8
8
|
@mailbox = options[:mailbox] || Tribe::Mailbox.new
|
9
9
|
@registry = options[:registry] || Tribe.registry
|
10
|
+
@scheduler = options[:scheduler] || Workers.scheduler
|
11
|
+
@timers = Tribe::SafeSet.new
|
10
12
|
@name = options[:name]
|
11
13
|
@pool = @dedicated ? Workers::Pool.new(:size => 1) : (options[:pool] || Workers.pool)
|
12
14
|
@alive = true
|
@@ -71,14 +73,53 @@ module Tribe
|
|
71
73
|
# Override and call super as necessary.
|
72
74
|
def process_event(event)
|
73
75
|
send("on_#{event.command}", event)
|
76
|
+
|
77
|
+
return nil
|
74
78
|
end
|
75
79
|
|
76
80
|
# Override and call super as necessary.
|
77
81
|
def exception_handler(e)
|
82
|
+
return nil
|
78
83
|
end
|
79
84
|
|
80
85
|
# Override and call super as necessary.
|
81
86
|
def shutdown_handler(event)
|
87
|
+
shutdown_timers
|
88
|
+
|
89
|
+
return nil
|
90
|
+
end
|
91
|
+
|
92
|
+
def shutdown_timers
|
93
|
+
@timers.each do |timer|
|
94
|
+
timer.cancel
|
95
|
+
end
|
96
|
+
|
97
|
+
return nil
|
98
|
+
end
|
99
|
+
|
100
|
+
def timer(delay, command, data = nil)
|
101
|
+
timer = Workers::Timer.new(delay, :scheduler => @scheduler) do
|
102
|
+
@timers.delete(timer)
|
103
|
+
enqueue(command, data)
|
104
|
+
end
|
105
|
+
|
106
|
+
@timers.add(timer)
|
107
|
+
|
108
|
+
return timer
|
109
|
+
end
|
110
|
+
|
111
|
+
def periodic_timer(delay, command, data = nil)
|
112
|
+
timer = Workers::PeriodicTimer.new(delay, :scheduler => @scheduler) do
|
113
|
+
enqueue(command, data)
|
114
|
+
unless alive?
|
115
|
+
@timers.delete(timer)
|
116
|
+
timer.cancel
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
@timers.add(timer)
|
121
|
+
|
122
|
+
return timer
|
82
123
|
end
|
83
124
|
end
|
84
125
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# Ruby's built in Set class may not be thread safe.
|
2
|
+
# This class wraps each method to make it so.
|
3
|
+
# More methods will be wrapped as needed.
|
4
|
+
|
5
|
+
module Tribe
|
6
|
+
class SafeSet
|
7
|
+
def initialize
|
8
|
+
@mutex = Mutex.new
|
9
|
+
@set = Set.new
|
10
|
+
|
11
|
+
return nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def add(item)
|
15
|
+
@mutex.synchronize do
|
16
|
+
@set.add(item)
|
17
|
+
end
|
18
|
+
|
19
|
+
return self
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete(item)
|
23
|
+
@mutex.synchronize do
|
24
|
+
@set.delete(item)
|
25
|
+
end
|
26
|
+
|
27
|
+
return self
|
28
|
+
end
|
29
|
+
|
30
|
+
def each(&block)
|
31
|
+
@mutex.synchronize do
|
32
|
+
@set.each do |item|
|
33
|
+
yield(item)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
return self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/tribe/version.rb
CHANGED
data/lib/tribe.rb
CHANGED
data/tribe.gemspec
CHANGED
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: workers
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - '='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.
|
21
|
+
version: 0.1.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - '='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.
|
29
|
+
version: 0.1.1
|
30
30
|
description: Tribe is a Ruby gem that implements event-driven actors.
|
31
31
|
email:
|
32
32
|
- chad@remesch.com
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- lib/tribe/dedicated_actor.rb
|
47
47
|
- lib/tribe/mailbox.rb
|
48
48
|
- lib/tribe/registry.rb
|
49
|
+
- lib/tribe/safe_set.rb
|
49
50
|
- lib/tribe/version.rb
|
50
51
|
- tribe.gemspec
|
51
52
|
homepage: https://github.com/chadrem/tribe
|