daemonic 0.1.0 → 0.1.1

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: b1446f7014c74a118db956ede363c6ba0dc31dc6
4
- data.tar.gz: e519190127e484ab4ca0722928eaac7feadc9e6f
3
+ metadata.gz: 02568ce6526b1a6407039c4abb9b45d005bf5e7f
4
+ data.tar.gz: 388216ee58cdbc4b272f15b76df288ebe65d29c0
5
5
  SHA512:
6
- metadata.gz: 584c0e7c4fb1cb899743762b553ddcf4bb76169029f41c4db8b89ab456bcb23f18fa8d7688e2d6ea26b45b9ca18de9caf9c14cd1b371365ad4144f7551795ddc
7
- data.tar.gz: 76cfeaa9cdb7067afd482b96fe3de73a8e72d9baeb5ba5f775878c09c89fb962d466199b69d294b82b5512c54e6fa6fc5f9d50c89bf5e3800c0a086d0779d126
6
+ metadata.gz: d4d4bd93c7b5f861f7bc1fb575bb613ee9756a5a285f618821ce4b649592cdf3fd83fec2be636213088eeb19944cfde31c0d3dc738b6dd2665bb0a2b37dec110
7
+ data.tar.gz: 80879158acec4db8a26fdc654ff8329f410ff92c6b93f0c31a2ed99e09991afdce1ec12d2492c98e8653e915ba2b0a38f955b63bf96f5e716bed449ab641c80d
data/lib/daemonic/pool.rb CHANGED
@@ -13,11 +13,12 @@ module Daemonic
13
13
 
14
14
  STOP_SIGNAL = StopSignal.new
15
15
 
16
- def initialize(thread_count, worker, logger)
17
- @worker = worker
18
- @jobs = SizedQueue.new(thread_count)
19
- @logger = logger
20
- @threads = thread_count.times.map {|worker_num|
16
+ attr_reader :producer
17
+
18
+ def initialize(producer)
19
+ @producer = producer
20
+ @jobs = SizedQueue.new(producer.queue_size)
21
+ @threads = producer.concurrency.times.map {|worker_num|
21
22
  Thread.new do
22
23
  dispatch(worker_num)
23
24
  end
@@ -25,7 +26,7 @@ module Daemonic
25
26
  end
26
27
 
27
28
  def enqueue(job)
28
- @logger.debug { "Enqueueing #{job.inspect}" }
29
+ logger.debug { "Enqueueing #{job.inspect}" }
29
30
  @jobs.push(job)
30
31
  end
31
32
  alias_method :<<, :enqueue
@@ -40,26 +41,38 @@ module Daemonic
40
41
  private
41
42
 
42
43
  def dispatch(worker_num)
43
- @logger.debug { "T#{worker_num}: Starting" }
44
+ logger.debug { "T#{worker_num}: Starting" }
44
45
  loop do
45
46
  job = @jobs.pop
46
47
  if STOP_SIGNAL.equal?(job)
47
- @logger.debug { "T#{worker_num}: Received stop signal, terminating." }
48
+ logger.debug { "T#{worker_num}: Received stop signal, terminating." }
48
49
  break
49
50
  end
50
51
  begin
51
- @logger.debug { "T#{worker_num}: Consuming #{job.inspect}" }
52
- @worker.consume(job)
52
+ logger.debug { "T#{worker_num}: Consuming #{job.inspect}" }
53
+ worker.consume(job)
53
54
  Thread.pass
54
55
  rescue Object => error
55
- @logger.warn { "T#{worker_num}: Error while processing #{job}: #{error.class}: #{error}" }
56
- @logger.info { error.backtrace.join("\n") }
56
+ if error.is_a?(SystemExit) # allow app to exit
57
+ logger.warn { "T#{worker_num}: Received SystemExit, shutting down" }
58
+ producer.stop
59
+ else
60
+ logger.warn { "T#{worker_num}: #{error.class} while processing #{job}: #{error}" }
61
+ logger.info { error.backtrace.join("\n") }
62
+ end
57
63
  Thread.pass
58
64
  end
59
65
  end
60
- @logger.debug { "T#{worker_num}: Stopped" }
66
+ logger.debug { "T#{worker_num}: Stopped" }
67
+ end
68
+
69
+ def worker
70
+ producer.worker
61
71
  end
62
72
 
73
+ def logger
74
+ producer.logger
75
+ end
63
76
 
64
77
  end
65
78
  end
@@ -1,15 +1,15 @@
1
1
  module Daemonic
2
2
  class Producer
3
3
 
4
- attr_reader :worker, :queue, :concurrency, :options
4
+ attr_reader :worker, :concurrency, :options, :queue_size
5
5
 
6
6
  def initialize(worker, options)
7
- @worker = worker
8
- @options = options
7
+ @worker = worker
8
+ @options = options
9
9
  @concurrency = options.fetch(:concurrency) { 4 }
10
-
11
- @queue = SizedQueue.new(concurrency) { concurrency + 1 }
12
- @running = true
10
+ @queue_size = options.fetch(:queue_size) { @concurrency + 1 }
11
+ @logger = options[:logger]
12
+ @running = true
13
13
  end
14
14
 
15
15
  def run
@@ -18,7 +18,7 @@ module Daemonic
18
18
  Signal.trap("INT") { stop }
19
19
  Signal.trap("TERM") { stop }
20
20
 
21
- pool = Pool.new(concurrency, worker, logger)
21
+ pool = Pool.new(self)
22
22
 
23
23
  producer = Thread.new do
24
24
  while @running
@@ -1,3 +1,3 @@
1
1
  module Daemonic
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daemonic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - iain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-15 00:00:00.000000000 Z
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler