Class: WorkerRoulette::WorkerRoulette

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

Constant Summary collapse

JOB_BOARD =
"job_board"
JOB_NOTIFICATIONS =
"new_job_ready"
DEFAULT_POLLING_TIME =
2
DEFAULT_REDIS_CONFIG =
{
  host: 'localhost',
  port: 6379,
  db: 14,
  driver: :hiredis,
  timeout: 5,
  evented: false,
  pool_size: 10,
  polling_time: DEFAULT_POLLING_TIME
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ WorkerRoulette

Returns a new instance of WorkerRoulette.



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

def initialize(config = {})
  config.recursive_symbolize_keys!
  @redis_config               = DEFAULT_REDIS_CONFIG.merge(config)
  @pool_config                = { size: @redis_config.delete(:pool_size), timeout: @redis_config.delete(:timeout) }
  @evented                    = @redis_config.delete(:evented)
  @polling_time               = @redis_config.delete(:polling_time)

  @foreman_connection_pool    = ConnectionPool.new(@pool_config) {new_redis}
  @tradesman_connection_pool  = ConnectionPool.new(@pool_config) {new_redis}

  @preprocessors = []

  configure_queue_tracker(config.delete(:metric_tracker))
end

Instance Attribute Details

#preprocessorsObject (readonly)

Returns the value of attribute preprocessors.



56
57
58
# File 'lib/worker_roulette.rb', line 56

def preprocessors
  @preprocessors
end

Class Method Details

.counter_key(sender, namespace = nil) ⇒ Object



47
48
49
# File 'lib/worker_roulette.rb', line 47

def self.counter_key(sender, namespace = nil)
  "#{namespace + ':' if namespace}counter_key"
end

.dump(obj) ⇒ Object



27
28
29
30
31
# File 'lib/worker_roulette.rb', line 27

def self.dump(obj)
  Oj.dump(obj)
rescue Oj::ParseError => e
  {'error' => e, 'unparsable_string' => obj}
end

.job_board_key(namespace = nil) ⇒ Object



39
40
41
# File 'lib/worker_roulette.rb', line 39

def self.job_board_key(namespace = nil)
  "#{namespace + ':' if namespace}#{WorkerRoulette::JOB_BOARD}"
end

.load(json) ⇒ Object



33
34
35
36
37
# File 'lib/worker_roulette.rb', line 33

def self.load(json)
  Oj.load(json)
rescue Oj::ParseError => e
  {'error' => e, 'unparsable_string' => obj}
end

.sender_key(sender, namespace = nil) ⇒ Object



43
44
45
# File 'lib/worker_roulette.rb', line 43

def self.sender_key(sender, namespace = nil)
  "#{namespace + ':' if namespace}#{sender}"
end

.start(config = {}) ⇒ Object



51
52
53
# File 'lib/worker_roulette.rb', line 51

def self.start(config = {})
  new(config)
end

Instance Method Details

#configure_queue_tracker(config) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/worker_roulette.rb', line 73

def configure_queue_tracker(config)
  return unless config

  QueueMetricTracker.configure(
    {
      server_name: `hostname`.chomp,
      granularity: config[:granularity],
      metric_host: config[:metric_host],
      metric_host_port: config[:metric_host_port],
      metrics: config[:metrics]
    }
  )

  preprocessors << QueueLatency
end

#foreman(sender, namespace = nil) ⇒ Object



89
90
91
92
# File 'lib/worker_roulette.rb', line 89

def foreman(sender, namespace = nil)
  raise "WorkerRoulette not Started" unless @foreman_connection_pool
  Foreman.new(@foreman_connection_pool, sender, namespace, preprocessors)
end

#polling_timeObject



111
112
113
# File 'lib/worker_roulette.rb', line 111

def polling_time
  @polling_time
end

#pool_sizeObject



103
104
105
# File 'lib/worker_roulette.rb', line 103

def pool_size
  (@pool_config ||= {})[:size]
end

#redis_configObject



107
108
109
# File 'lib/worker_roulette.rb', line 107

def redis_config
  (@redis_config ||= {}).dup
end

#tradesman(namespace = nil, polling_time = DEFAULT_POLLING_TIME) ⇒ Object



94
95
96
97
# File 'lib/worker_roulette.rb', line 94

def tradesman(namespace = nil, polling_time = DEFAULT_POLLING_TIME)
  raise "WorkerRoulette not Started" unless @tradesman_connection_pool
  Tradesman.new(@tradesman_connection_pool, @evented, namespace, polling_time || @polling_time, preprocessors)
end

#tradesman_connection_poolObject



99
100
101
# File 'lib/worker_roulette.rb', line 99

def tradesman_connection_pool
  @tradesman_connection_pool
end