Class: Upperkut::Strategy
- Inherits:
-
Object
- Object
- Upperkut::Strategy
show all
- Includes:
- Util
- Defined in:
- lib/upperkut/strategy.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Util
#decode_json_items, #encode_json_items, #to_underscore
Constructor Details
#initialize(worker, redis) ⇒ Strategy
Returns a new instance of Strategy.
9
10
11
12
|
# File 'lib/upperkut/strategy.rb', line 9
def initialize(worker, redis)
self.worker = worker
self.redis = redis
end
|
Instance Attribute Details
#redis ⇒ Object
Returns the value of attribute redis.
7
8
9
|
# File 'lib/upperkut/strategy.rb', line 7
def redis
@redis
end
|
#worker ⇒ Object
Returns the value of attribute worker.
7
8
9
|
# File 'lib/upperkut/strategy.rb', line 7
def worker
@worker
end
|
Instance Method Details
#clear ⇒ Object
44
45
46
|
# File 'lib/upperkut/strategy.rb', line 44
def clear
redis.del(key)
end
|
#fetch_items(batch_size = 1000) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/upperkut/strategy.rb', line 20
def fetch_items(batch_size = 1000)
stop = [batch_size, size].min
items = redis.multi do
stop.times do
redis.lpop(key)
end
end
decode_json_items(items)
end
|
#latency ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/upperkut/strategy.rb', line 36
def latency
item = redis.lrange(key, 0, 0)
item = decode_json_items(item).first
return 0 unless item
now = Time.now.to_f
now - item.fetch('enqueued_at', Time.now).to_f
end
|
#push_items(items = []) ⇒ Object
14
15
16
17
18
|
# File 'lib/upperkut/strategy.rb', line 14
def push_items(items = [])
items = [items] if items.is_a?(Hash)
return false if items.empty?
redis.rpush(key, encode_json_items(items))
end
|
#size ⇒ Object
32
33
34
|
# File 'lib/upperkut/strategy.rb', line 32
def size
redis.llen(key)
end
|