workers 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +37 -4
- data/lib/workers/pool.rb +1 -1
- data/lib/workers/scheduler.rb +7 -0
- data/lib/workers/timer.rb +3 -1
- data/lib/workers/version.rb +1 -1
- data/lib/workers/worker.rb +25 -11
- metadata +2 -2
data/README.md
CHANGED
@@ -17,7 +17,7 @@ Or install it yourself as:
|
|
17
17
|
|
18
18
|
$ gem install workers
|
19
19
|
|
20
|
-
## Basic
|
20
|
+
## Workers - Basic
|
21
21
|
|
22
22
|
# Initialize a worker pool.
|
23
23
|
pool = Workers::Pool.new
|
@@ -38,7 +38,7 @@ Or install it yourself as:
|
|
38
38
|
# Wait for the workers to shutdown.
|
39
39
|
pool.join
|
40
40
|
|
41
|
-
## Advanced
|
41
|
+
## Workers - Advanced
|
42
42
|
|
43
43
|
The Worker class is designed to be customized through inheritence and its event system:
|
44
44
|
|
@@ -70,6 +70,9 @@ The Worker class is designed to be customized through inheritence and its event
|
|
70
70
|
# Wait for the workers to shutdown.
|
71
71
|
pool.join
|
72
72
|
|
73
|
+
Note that you can use custom workers without a pool.
|
74
|
+
This effectively gives you direct access to a single event driven thread.
|
75
|
+
|
73
76
|
## Timers
|
74
77
|
|
75
78
|
Timers provide a way to execute code in the future:
|
@@ -83,10 +86,35 @@ Timers provide a way to execute code in the future:
|
|
83
86
|
timer = Workers::PeriodicTimer.new(1) do
|
84
87
|
puts 'Hello world many times'
|
85
88
|
end
|
86
|
-
|
89
|
+
|
90
|
+
# Let the timer print some lines.
|
91
|
+
sleep(5)
|
92
|
+
|
93
|
+
# Shutdown the timer.
|
87
94
|
timer.cancel
|
88
95
|
|
89
|
-
|
96
|
+
## Schedulers
|
97
|
+
|
98
|
+
Schedulers are what trigger Timers to fire.
|
99
|
+
The system has a global default scheduler which should meet most needs (Workers.scheduler).
|
100
|
+
You can create additional or custom ones as necessary:
|
101
|
+
|
102
|
+
# Create a workers pool with a larger than default thread count (optional).
|
103
|
+
pool = Workers::Pool.new(:size => 100)
|
104
|
+
|
105
|
+
# Create a scheduler.
|
106
|
+
scheduler = Workers::Scheduler.new(:pool => pool)
|
107
|
+
|
108
|
+
# Create a timer that uses the above scheduler.
|
109
|
+
timer = Workers::Timer.new(1, :scheduler => scheduler) do
|
110
|
+
puts 'Hello world'
|
111
|
+
end
|
112
|
+
|
113
|
+
# Wait for the timer to fire.
|
114
|
+
sleep(5)
|
115
|
+
|
116
|
+
# Shutdown the scheduler.
|
117
|
+
scheduler.dispose
|
90
118
|
|
91
119
|
## Options (defaults below):
|
92
120
|
|
@@ -114,6 +142,11 @@ Callbacks execute using a Workers::Pool in case they contain blocking operations
|
|
114
142
|
:callback => nil # The proc to execute (provide this or a block, but not both).
|
115
143
|
)
|
116
144
|
|
145
|
+
scheduler = Workers::Scheduler.new(
|
146
|
+
:logger => nil, # Ruby logger instance.
|
147
|
+
:pool => Workers::Pool.new # The workers pool used to execute timer callbacks.
|
148
|
+
)
|
149
|
+
|
117
150
|
|
118
151
|
## TODO - not yet implemented features
|
119
152
|
|
data/lib/workers/pool.rb
CHANGED
@@ -5,8 +5,8 @@ module Workers
|
|
5
5
|
DEFAULT_POOL_SIZE = 20
|
6
6
|
|
7
7
|
def initialize(options = {})
|
8
|
-
@size = options[:size] || Workers::Pool::DEFAULT_POOL_SIZE
|
9
8
|
@logger = Workers::LogProxy.new(options[:logger])
|
9
|
+
@size = options[:size] || Workers::Pool::DEFAULT_POOL_SIZE
|
10
10
|
@worker_class = options[:worker_class] || Workers::Worker
|
11
11
|
|
12
12
|
@input_queue = Queue.new
|
data/lib/workers/scheduler.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module Workers
|
2
2
|
class Scheduler
|
3
|
+
include Workers::Helpers
|
4
|
+
|
3
5
|
def initialize(options = {})
|
6
|
+
@logger = Workers::LogProxy.new(options[:logger])
|
4
7
|
@pool = options[:pool] || Workers::Pool.new
|
5
8
|
@schedule = SortedSet.new
|
6
9
|
@mutex = Mutex.new
|
@@ -41,6 +44,10 @@ module Workers
|
|
41
44
|
return nil
|
42
45
|
end
|
43
46
|
|
47
|
+
def alive?
|
48
|
+
return @thread && @thread.alive?
|
49
|
+
end
|
50
|
+
|
44
51
|
private
|
45
52
|
|
46
53
|
def start_loop
|
data/lib/workers/timer.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
module Workers
|
2
2
|
class Timer
|
3
|
+
include Workers::Helpers
|
4
|
+
|
3
5
|
attr_reader :delay
|
4
6
|
attr_reader :repeat
|
5
7
|
|
6
8
|
def initialize(delay, options = {}, &block)
|
9
|
+
@logger = Workers::LogProxy.new(options[:logger])
|
7
10
|
@delay = delay
|
8
11
|
@callback = options[:callback] || block
|
9
12
|
@repeat = options[:repeat] || false
|
10
13
|
@scheduler = options[:scheduler] || Workers.scheduler
|
11
|
-
@logger = options[:logger]
|
12
14
|
|
13
15
|
@mutex = Mutex.new
|
14
16
|
|
data/lib/workers/version.rb
CHANGED
data/lib/workers/worker.rb
CHANGED
@@ -28,45 +28,59 @@ module Workers
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def join(max_wait = nil)
|
31
|
-
raise "Worker can't join itself
|
31
|
+
raise "Worker can't join itself." if @thread == Thread.current
|
32
32
|
|
33
33
|
return true if !@thread.join(max_wait).nil?
|
34
34
|
|
35
35
|
@thread.kill and return false
|
36
36
|
end
|
37
37
|
|
38
|
+
def alive?
|
39
|
+
return @thread && @thread.alive?
|
40
|
+
end
|
41
|
+
|
38
42
|
private
|
39
43
|
|
40
44
|
def start_event_loop
|
41
45
|
while true
|
42
|
-
event = @input_queue.pop
|
46
|
+
event = @input_queue.pop
|
43
47
|
|
44
48
|
case event.command
|
45
49
|
when :shutdown
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
return
|
50
|
+
shutdown_handler(event)
|
51
|
+
return nil
|
50
52
|
when :perform
|
51
|
-
|
52
|
-
log_error("Worker failed run 'perform' callback.", e)
|
53
|
-
end
|
53
|
+
perform_handler(event)
|
54
54
|
else
|
55
55
|
process_event(event)
|
56
56
|
end
|
57
57
|
end
|
58
|
+
rescue Exception => e
|
59
|
+
exception_handler(e)
|
60
|
+
end
|
61
|
+
|
62
|
+
def shutdown_handler(event)
|
63
|
+
try_callback(event.data)
|
64
|
+
end
|
65
|
+
|
66
|
+
def perform_handler(event)
|
67
|
+
try_callback(event.data)
|
68
|
+
end
|
69
|
+
|
70
|
+
def exception_handler(e)
|
71
|
+
puts concat_e('Worker event loop died.', e)
|
58
72
|
end
|
59
73
|
|
60
74
|
def try_callback(callback, &block)
|
61
75
|
begin
|
62
76
|
callback.call
|
63
77
|
rescue Exception => e
|
64
|
-
block.call(e)
|
78
|
+
block.call(e) if block
|
65
79
|
end
|
66
80
|
end
|
67
81
|
|
68
82
|
def process_event(event)
|
69
|
-
raise
|
83
|
+
raise "Unhandled event (#{event.inspect}). Subclass and override if you need custom events."
|
70
84
|
end
|
71
85
|
end
|
72
86
|
end
|
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.4
|
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-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Simple Ruby workers for performing work in background threads.
|
15
15
|
email:
|