Class: Specbandit::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/specbandit/publisher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: Specbandit.configuration.key, key_ttl: Specbandit.configuration.key_ttl, queue: nil, output: $stdout) ⇒ Publisher

Returns a new instance of Publisher.



9
10
11
12
13
14
15
# File 'lib/specbandit/publisher.rb', line 9

def initialize(key: Specbandit.configuration.key, key_ttl: Specbandit.configuration.key_ttl, queue: nil,
               output: $stdout)
  @key = key
  @key_ttl = key_ttl
  @queue = queue || RedisQueue.new
  @output = output
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/specbandit/publisher.rb', line 7

def key
  @key
end

#key_ttlObject (readonly)

Returns the value of attribute key_ttl.



7
8
9
# File 'lib/specbandit/publisher.rb', line 7

def key_ttl
  @key_ttl
end

#outputObject (readonly)

Returns the value of attribute output.



7
8
9
# File 'lib/specbandit/publisher.rb', line 7

def output
  @output
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/specbandit/publisher.rb', line 7

def queue
  @queue
end

Instance Method Details

#publish(files: [], pattern: nil, reset: false) ⇒ Object

Resolve files from the three input sources (priority: stdin > pattern > args) and push them onto the Redis queue.

With reset: true the key is emptied first, so the queue ends up holding exactly this work list even if an earlier attempt already pushed one.

Returns the number of files enqueued.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/specbandit/publisher.rb', line 24

def publish(files: [], pattern: nil, reset: false)
  resolved = resolve_files(files: files, pattern: pattern)

  if resolved.empty?
    output.puts '[specbandit] No files to enqueue.'
    return 0
  end

  # Only reset once there is something to put in its place. Clearing on an
  # empty push would drop the marker too and leave workers crashing on a
  # key that looks like it was never published.
  reset_key if reset

  push_ms = measure { queue.push(key, resolved, ttl: key_ttl) }
  # Record a durable "published" marker so workers can tell a drained
  # queue ("worker arriving late", OK) apart from one that was never
  # pushed ("you didn't push work", crash). Redis auto-deletes empty
  # lists, so the list itself cannot carry this signal.
  mark_ms = measure { queue.mark_published(key, ttl: key_ttl) }

  output.puts "[specbandit] Enqueued #{resolved.size} files onto key '#{key}' (TTL: #{key_ttl}s)."
  output.puts format('[specbandit] Redis latency: push %.1fms, mark published %.1fms.', push_ms, mark_ms)
  resolved.size
end