Class: CI::Queue::Redis::Worker

Inherits:
Base
  • Object
show all
Defined in:
lib/ci/queue/redis/worker.rb

Constant Summary

Constants inherited from Base

Base::CONNECTION_ERRORS

Constants included from Common

Common::CONNECTION_ERRORS

Instance Attribute Summary collapse

Attributes included from Common

#config

Instance Method Summary collapse

Methods inherited from Base

#exhausted?, #progress, #queue_initialized?, #size, #to_a, #wait_for_master, #workers_count

Methods included from Common

#flaky?, #report_failure!, #report_success!, #rescue_connection_errors

Constructor Details

#initialize(redis, config) ⇒ Worker

Returns a new instance of Worker.



16
17
18
19
20
# File 'lib/ci/queue/redis/worker.rb', line 16

def initialize(redis, config)
  @reserved_test = nil
  @shutdown_required = false
  super(redis, config)
end

Instance Attribute Details

#total ⇒ Object (readonly)

Returns the value of attribute total.



14
15
16
# File 'lib/ci/queue/redis/worker.rb', line 14

def total
  @total
end

Instance Method Details

#acknowledge(test) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/ci/queue/redis/worker.rb', line 70

def acknowledge(test)
  test_key = test.id
  raise_on_mismatching_test(test_key)
  eval_script(
    :acknowledge,
    keys: [key('running'), key('processed')],
    argv: [test_key],
  ) == 1
end

#build ⇒ Object



66
67
68
# File 'lib/ci/queue/redis/worker.rb', line 66

def build
  @build ||= CI::Queue::Redis::BuildRecord.new(self, redis, config)
end

#master? ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ci/queue/redis/worker.rb', line 41

def master?
  @master
end

#poll ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ci/queue/redis/worker.rb', line 45

def poll
  wait_for_master
  until shutdown_required? || config.circuit_breaker.open? || exhausted?
    if test = reserve
      yield index.fetch(test)
    else
      sleep 0.05
    end
  end
rescue *CONNECTION_ERRORS
end

#populate(tests, random: Random.new) ⇒ Object



22
23
24
25
26
27
# File 'lib/ci/queue/redis/worker.rb', line 22

def populate(tests, random: Random.new)
  @index = tests.map { |t| [t.id, t] }.to_h
  tests = Queue.shuffle(tests, random)
  push(tests.map(&:id))
  self
end

#populated? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ci/queue/redis/worker.rb', line 29

def populated?
  !!defined?(@index)
end

#requeue(test, offset: Redis.requeue_offset) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ci/queue/redis/worker.rb', line 80

def requeue(test, offset: Redis.requeue_offset)
  test_key = test.id
  raise_on_mismatching_test(test_key)
  global_max_requeues = config.global_max_requeues(total)

  requeued = config.max_requeues > 0 && global_max_requeues > 0 && eval_script(
    :requeue,
    keys: [key('processed'), key('requeues-count'), key('queue'), key('running')],
    argv: [config.max_requeues, global_max_requeues, test_key, offset],
  ) == 1

  @reserved_test = test_key unless requeued
  requeued
end

#retry_queue ⇒ Object



57
58
59
60
# File 'lib/ci/queue/redis/worker.rb', line 57

def retry_queue
  log = redis.lrange(key('worker', worker_id, 'queue'), 0, -1).reverse.uniq
  Retry.new(log, config, redis: redis)
end

#shutdown! ⇒ Object



33
34
35
# File 'lib/ci/queue/redis/worker.rb', line 33

def shutdown!
  @shutdown_required = true
end

#shutdown_required? ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ci/queue/redis/worker.rb', line 37

def shutdown_required?
  @shutdown_required
end

#supervisor ⇒ Object



62
63
64
# File 'lib/ci/queue/redis/worker.rb', line 62

def supervisor
  Supervisor.new(redis_url, config)
end