workers 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .rvmrc
19
+ *.swp
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Workers
2
2
 
3
- TODO: Write a gem description
3
+ Workers is a Ruby gem for performing work in background threads.
4
+ Design goals include high performance, low latency, simple API, and customizability.
4
5
 
5
6
  ## Installation
6
7
 
@@ -16,10 +17,68 @@ Or install it yourself as:
16
17
 
17
18
  $ gem install workers
18
19
 
19
- ## Usage
20
+ ## Basic Usage
20
21
 
21
- TODO: Write usage instructions here
22
+ # Initialize a worker pool.
23
+ pool = Workers::Pool.new
24
+
25
+ # Perform some work in the background.
26
+ 100.times do
27
+ pool.perform do
28
+ sleep(rand(3))
29
+ puts "Hello world from thread #{Thread.current.object_id}"
30
+ end
31
+ end
32
+
33
+ # Tell the workers to shutdown.
34
+ pool.shutdown do
35
+ puts "Worker thread #{Thread.current.object_id} is shutting down."
36
+ end
37
+
38
+ # Wait for the workers to finish.
39
+ pool.join
22
40
 
41
+ ## Advanced Usage
42
+
43
+ The Worker class is designed to be customized.
44
+
45
+ # Create a custom worker class that handles custom commands.
46
+ class CustomWorker < Workers::Worker
47
+ private
48
+ def process_event(event)
49
+ case event.command
50
+ when :my_custom
51
+ puts "Worker received custom event: #{event.data}"
52
+ sleep(1)
53
+ end
54
+ end
55
+ end
56
+
57
+ # Create a pool that uses your custom worker class.
58
+ pool = Workers::Pool.new(:worker_class => CustomWorker)
59
+
60
+ # Tell the workers to do some work using custom events.
61
+ 100.times do |i|
62
+ pool.enqueue(:my_custom, i)
63
+ end
64
+
65
+ # Tell the workers to shutdown.
66
+ pool.shutdown do
67
+ puts "Worker thread #{Thread.current.object_id} is shutting down."
68
+ end
69
+
70
+ # Wait for it to finish working and shutdown.
71
+ pool.join
72
+
73
+ ## Pool Options
74
+
75
+ The pool class takes a few options (defaults below):
76
+
77
+ pool = Workers::Pool.new(
78
+ :size => 20, # Number of threads to create.
79
+ :logger => nil # Ruby Logger instance.
80
+ :worker_class => Workers::Worker # Class of worker to use for this pool.
81
+
23
82
  ## Contributing
24
83
 
25
84
  1. Fork it
data/lib/workers/pool.rb CHANGED
@@ -7,9 +7,17 @@ module Workers
7
7
  def initialize(options = {})
8
8
  @size = options[:size] || Workers::Pool::DEFAULT_POOL_SIZE
9
9
  @logger = Workers::LogProxy.new(options[:logger])
10
+ @worker_class = options[:worker_class] || Workers::Worker
11
+
10
12
  @input_queue = Queue.new
11
13
  @workers = []
12
- @size.times { @workers << Workers::Worker.new(:input_queue => @input_queue) }
14
+ @size.times { @workers << @worker_class.new(:input_queue => @input_queue) }
15
+ end
16
+
17
+ def enqueue(command, data)
18
+ @input_queue.push(Event.new(command, data))
19
+
20
+ return nil
13
21
  end
14
22
 
15
23
  def perform(options = {}, &block)
@@ -27,11 +35,5 @@ module Workers
27
35
  def join(max_wait = nil)
28
36
  return @workers.map { |w| w.join(max_wait) }
29
37
  end
30
-
31
- private
32
-
33
- def enqueue(command, data)
34
- @input_queue.push(Event.new(command, data))
35
- end
36
38
  end
37
39
  end
@@ -1,3 +1,3 @@
1
1
  module Workers
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -5,9 +5,16 @@ module Workers
5
5
  def initialize(options = {})
6
6
  @logger = Workers::LogProxy.new(options[:logger])
7
7
  @input_queue = options[:input_queue] || Queue.new
8
+
8
9
  @thread = Thread.new { start_event_loop }
9
10
  end
10
11
 
12
+ def enqueue(command, data)
13
+ @input_queue.push(Event.new(command, data))
14
+
15
+ return nil
16
+ end
17
+
11
18
  def perform(options = {}, &block)
12
19
  enqueue(:perform, block)
13
20
 
@@ -30,10 +37,6 @@ module Workers
30
37
 
31
38
  private
32
39
 
33
- def enqueue(command, data)
34
- @input_queue.push(Event.new(command, data))
35
- end
36
-
37
40
  def start_event_loop
38
41
  while true
39
42
  event = @input_queue.pop # Blocking.
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -24,8 +24,6 @@ files:
24
24
  - README.md
25
25
  - Rakefile
26
26
  - lib/workers.rb
27
- - lib/workers/.pool.rb.swp
28
- - lib/workers/.worker.rb.swp
29
27
  - lib/workers/event.rb
30
28
  - lib/workers/helpers.rb
31
29
  - lib/workers/log_proxy.rb
Binary file
Binary file