workers 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/workers.rb +0 -14
- data/lib/workers/version.rb +1 -1
- metadata +2 -6
- data/lib/workers/actor.rb +0 -79
- data/lib/workers/dedicated_actor.rb +0 -9
- data/lib/workers/mailbox.rb +0 -26
- data/lib/workers/registry.rb +0 -45
data/README.md
CHANGED
data/lib/workers.rb
CHANGED
@@ -10,10 +10,6 @@ require 'workers/log_proxy'
|
|
10
10
|
require 'workers/scheduler'
|
11
11
|
require 'workers/timer'
|
12
12
|
require 'workers/periodic_timer'
|
13
|
-
require 'workers/mailbox'
|
14
|
-
require 'workers/actor'
|
15
|
-
require 'workers/dedicated_actor'
|
16
|
-
require 'workers/registry'
|
17
13
|
|
18
14
|
module Workers
|
19
15
|
def self.pool
|
@@ -33,18 +29,8 @@ module Workers
|
|
33
29
|
@scheduler.dispose if @scheduler
|
34
30
|
@scheduler = val
|
35
31
|
end
|
36
|
-
|
37
|
-
def self.registry
|
38
|
-
return @registry ||= Workers::Registry.new
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.registry=(val)
|
42
|
-
@registry.dispose if @registry
|
43
|
-
@registry = val
|
44
|
-
end
|
45
32
|
end
|
46
33
|
|
47
34
|
# Force initialization of defaults.
|
48
35
|
Workers.pool
|
49
36
|
Workers.scheduler
|
50
|
-
Workers.registry
|
data/lib/workers/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: workers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
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-01-
|
12
|
+
date: 2013-01-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Simple Ruby workers for performing work in background threads.
|
15
15
|
email:
|
@@ -24,15 +24,11 @@ files:
|
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
26
26
|
- lib/workers.rb
|
27
|
-
- lib/workers/actor.rb
|
28
|
-
- lib/workers/dedicated_actor.rb
|
29
27
|
- lib/workers/event.rb
|
30
28
|
- lib/workers/helpers.rb
|
31
29
|
- lib/workers/log_proxy.rb
|
32
|
-
- lib/workers/mailbox.rb
|
33
30
|
- lib/workers/periodic_timer.rb
|
34
31
|
- lib/workers/pool.rb
|
35
|
-
- lib/workers/registry.rb
|
36
32
|
- lib/workers/scheduler.rb
|
37
33
|
- lib/workers/timer.rb
|
38
34
|
- lib/workers/version.rb
|
data/lib/workers/actor.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
module Workers
|
2
|
-
class Actor
|
3
|
-
include Workers::Helpers
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
@logger = Workers::LogProxy.new(options[:logger])
|
7
|
-
@dedicated = options[:dedicated] || false
|
8
|
-
@mailbox = options[:mailbox] || Workers::Mailbox.new
|
9
|
-
@registry = options[:registry] || Workers.registry
|
10
|
-
@name = options[:name]
|
11
|
-
@pool = @dedicated ? Workers::Pool.new(:size => 1) : (options[:pool] || Workers.pool)
|
12
|
-
@alive = true
|
13
|
-
|
14
|
-
@registry.register(self)
|
15
|
-
end
|
16
|
-
|
17
|
-
def enqueue(command, data = nil)
|
18
|
-
return false unless @alive
|
19
|
-
|
20
|
-
@mailbox.push(Event.new(command, data))
|
21
|
-
|
22
|
-
@pool.perform do
|
23
|
-
process_events
|
24
|
-
end
|
25
|
-
|
26
|
-
return true
|
27
|
-
end
|
28
|
-
|
29
|
-
def alive?
|
30
|
-
@mailbox.synchronize do
|
31
|
-
return @alive
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def name
|
36
|
-
return @name
|
37
|
-
end
|
38
|
-
|
39
|
-
def identifier
|
40
|
-
return @name ? "#{object_id}:#{@name}" : object_id
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def process_events
|
46
|
-
while (event = @mailbox.shift)
|
47
|
-
case event.command
|
48
|
-
when :shutdown
|
49
|
-
shutdown_handler(event)
|
50
|
-
@pool.shutdown if @dedicated
|
51
|
-
@mailbox.synchronize do
|
52
|
-
@alive = false
|
53
|
-
end
|
54
|
-
else
|
55
|
-
process_event(event)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
rescue Exception => e
|
59
|
-
@alive = false
|
60
|
-
exception_handler(e)
|
61
|
-
end
|
62
|
-
|
63
|
-
#
|
64
|
-
# Subclass and override the below methods.
|
65
|
-
#
|
66
|
-
|
67
|
-
def process_event(event)
|
68
|
-
puts "Actor (#{identifier}) received event (#{event.inspect})."
|
69
|
-
end
|
70
|
-
|
71
|
-
def exception_handler(e)
|
72
|
-
puts concat_e("Actor (#{identifier}) died.", e)
|
73
|
-
end
|
74
|
-
|
75
|
-
def shutdown_handler(event)
|
76
|
-
puts "Actor (#{identifier}) is shutting down."
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
data/lib/workers/mailbox.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module Workers
|
2
|
-
class Mailbox
|
3
|
-
def initialize(options = {})
|
4
|
-
@messages = []
|
5
|
-
@mutex = Mutex.new
|
6
|
-
end
|
7
|
-
|
8
|
-
def push(event)
|
9
|
-
@mutex.synchronize do
|
10
|
-
@messages.push(event)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def shift
|
15
|
-
@mutex.synchronize do
|
16
|
-
@messages.shift
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def synchronize(&block)
|
21
|
-
@mutex.synchronize do
|
22
|
-
block.call
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/lib/workers/registry.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
module Workers
|
2
|
-
class Registry
|
3
|
-
def initialize
|
4
|
-
@mutex = Mutex.new
|
5
|
-
@actors_by_name = {}
|
6
|
-
end
|
7
|
-
|
8
|
-
def register(actor)
|
9
|
-
@mutex.synchronize do
|
10
|
-
return false unless actor.name
|
11
|
-
|
12
|
-
if @actors_by_name[actor.name]
|
13
|
-
raise "Actor already exists (#{actor.name})."
|
14
|
-
else
|
15
|
-
@actors_by_name[actor.name] = actor
|
16
|
-
end
|
17
|
-
|
18
|
-
return true
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def unregister(actor)
|
23
|
-
@mutex.synchronize do
|
24
|
-
return false unless actor.name
|
25
|
-
return false unless @actors_by_name[actor.name]
|
26
|
-
|
27
|
-
@actors_by_name.delete(actor.name)
|
28
|
-
|
29
|
-
return true
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def [](val)
|
34
|
-
@mutex.synchronize do
|
35
|
-
return @actors_by_name[val]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def dispose
|
40
|
-
@mutex.synchronize do
|
41
|
-
@actors_by_name.clear
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|