Class: HastCI::AckWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/hastci/ack_worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_client:, error_collector:, queue_size: DEFAULT_QUEUE_SIZE, sleeper: HastCI::DEFAULT_SLEEPER) ⇒ AckWorker

Returns a new instance of AckWorker.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hastci/ack_worker.rb', line 13

def initialize(
  api_client:,
  error_collector:,
  queue_size: DEFAULT_QUEUE_SIZE,
  sleeper: HastCI::DEFAULT_SLEEPER
)
  @api_client = api_client
  @error_collector = error_collector
  @sleeper = sleeper
  @queue = SizedQueue.new(queue_size)
  @thread = nil
  @running = false
  @mutex = Mutex.new
  @state_mutex = Mutex.new
  @idle_condition = ConditionVariable.new
  @processing_count = 0
end

Instance Method Details

#enqueue(result) ⇒ Object



55
56
57
# File 'lib/hastci/ack_worker.rb', line 55

def enqueue(result)
  @queue.push(result)
end

#first_errorObject



31
32
33
# File 'lib/hastci/ack_worker.rb', line 31

def first_error
  @error_collector.first_error
end

#flush(timeout: DEFAULT_FLUSH_TIMEOUT) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hastci/ack_worker.rb', line 59

def flush(timeout: DEFAULT_FLUSH_TIMEOUT)
  deadline = Time.now + timeout

  @state_mutex.synchronize do
    until idle_unlocked?
      remaining = deadline - Time.now
      return false if remaining <= 0

      @idle_condition.wait(@state_mutex, [remaining, 0.1].min)
    end
  end

  true
end

#idle?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/hastci/ack_worker.rb', line 74

def idle?
  @state_mutex.synchronize { idle_unlocked? }
end

#pending_countObject



78
79
80
# File 'lib/hastci/ack_worker.rb', line 78

def pending_count
  @queue.size
end

#running?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/hastci/ack_worker.rb', line 82

def running?
  @mutex.synchronize { @running }
end

#startObject



35
36
37
38
39
40
41
42
# File 'lib/hastci/ack_worker.rb', line 35

def start
  @mutex.synchronize do
    return if @running

    @running = true
    @thread = Thread.new { process_loop }
  end
end

#stopObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/hastci/ack_worker.rb', line 44

def stop
  @mutex.synchronize do
    return unless @running

    @running = false
    @queue.close
  end

  @thread.join(SHUTDOWN_TIMEOUT)
end