throttle-queue 0.1.0 → 0.2.0

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: 0d0f4a538c9d23d2a21d79b63122c4a0074e4095
4
- data.tar.gz: 16859c8f40a47777526ec299ed4721ba955ca55f
3
+ metadata.gz: 4a42a085e91e37e9a9096ecd3c653409c28d6196
4
+ data.tar.gz: e4fbd3485adecc8a6145b8f54dd6d8e26e32cf49
5
5
  SHA512:
6
- metadata.gz: a39194f269635e9b8ece84714010df1e1cedd0194c5133a77fdfb277d71227aa4bf88e1d57e4b5fba2c84e470831a04b5ede2d8f8f287dd83baac54154f29229
7
- data.tar.gz: dc984657a36e8238294813d3f10e36ae837bd77764ee0c6a8c2be81648cedaeb2dda1a5334658a75ffab4cf01435ab6e02fe1f24284b73cb74c9029345c7b0ba
6
+ metadata.gz: ead308e25e05e0dd4ea5b45a87fb6e91a8ce73089580fe5a9bc301729af96afaf4fbb3e04eb196f42a05a8f21ff81bf7c9981b51792eb81725cb05d2ff370a3f
7
+ data.tar.gz: 4d9f1ca8b89f057f53fea6ecf862b47b72aa10e73da9375548bf299131290bf0ab829b33d72aa884267f2019bd680352e87b3c4a92c5cc2601ffb436a44c600f
data/README.md CHANGED
@@ -57,8 +57,12 @@ Wait for everything to finish
57
57
 
58
58
  Each resource is assumed to have a unique identifier, e.g. a filename or a reproducible
59
59
  hash value. The queue does not check if the resource exists first, but it will check if
60
- the id has already been queued. Any time an id is added to the queue, the previous block
61
- is dropped in favor of the new.
60
+ the id has already been queued. If an id is already in the queue and the same id is
61
+ added as a background job, the block already in the queue is held onto; the new block is
62
+ dropped. If an id is in the background queue, adding it to the foreground will cause the
63
+ background block to be dropped. If an id is in the foreground queue, adding it again
64
+ from another thread will cause the new block to be dropped, and the existing block to be
65
+ waited upon.
62
66
 
63
67
  Once an id has made it through the queue and been processed, the same id can be added
64
68
  again and will be blindly processed again. It is assumed the user of the object knows
@@ -1,5 +1,6 @@
1
1
  require 'drb'
2
2
  require 'fileutils'
3
+ require 'tmpdir'
3
4
  require_relative 'single-process.rb'
4
5
 
5
6
  class ThrottleQueue
@@ -19,24 +20,34 @@ class ThrottleQueue
19
20
  # If this is the first instace of the shared queue, it becomes the master queue and
20
21
  # starts a DRbServer instace. If a DRbServer is already running, it connects to the
21
22
  # queue as a remote DRbObject.
22
- def initialize(limit, name = 'ThrottleQueue')
23
- tmp = "/tmp/#{name}.sock"
23
+ #
24
+ # The ephemeral server port is written to /tmp/ThrottleQueue.sock - use opt[:name]
25
+ # to override the default queue name and support multiple instances.
26
+ #
27
+ # The server will bind according to the machine's hostname - use opt[:host] to
28
+ # override this, e.g. if there are restrictions on which addresses may be used.
29
+ def initialize(limit, opt = {})
30
+ opt[:name] ||= 'ThrottleQueue'
31
+ opt[:host] ||= Socket.gethostbyname[0] rescue 'localhost'
32
+
33
+ tmp = "#{Dir.tmpdir}/#{opt[:name]}.sock"
24
34
  FileUtils.touch tmp
25
35
  File.open(tmp, 'r+') {|f|
26
36
  f.flock File::LOCK_EX
27
37
  begin
28
38
  port = f.read.to_i
39
+ uri = "druby://#{opt[:host]}:#{port}"
29
40
  if port == 0
30
41
  @queue = ThrottleQueue.new(limit)
31
- @drb = DRb.start_service nil, @queue
42
+ @drb = DRb.start_service uri, @queue
32
43
  f.seek 0, IO::SEEK_SET
33
44
  f.truncate 0
34
45
  f.write @drb.uri[/\d+$/]
35
46
  f.flock File::LOCK_UN
36
47
  else
37
- @queue = DRbObject.new_with_uri("druby://localhost:#{port}")
48
+ @queue = DRbObject.new_with_uri(uri)
38
49
  @queue.idle?
39
- @drb = DRb.start_service
50
+ @drb = DRb.start_service "druby://#{opt[:host]}:0"
40
51
  f.flock File::LOCK_UN
41
52
  end
42
53
  rescue DRb::DRbConnError
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: throttle-queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Calhoun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-09 00:00:00.000000000 Z
11
+ date: 2015-01-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A thread-safe rate-limited work queue, which allows for background and
14
14
  foreground operations.