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) ⇒ Object

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

Returns the number of files enqueued.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/specbandit/publisher.rb', line 21

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

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

  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