Class: Fairway::Scripts

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, namespace) ⇒ Scripts

Returns a new instance of Scripts.



12
13
14
15
# File 'lib/fairway/scripts.rb', line 12

def initialize(redis, namespace)
  @redis     = redis
  @namespace = namespace
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fairway/scripts.rb', line 55

def method_missing(method_name, *args)
  loaded = false

  if multi?(method_name)
    redis.with_each do |conn|
      conn.evalsha(script_sha(method_name), [namespace], args)
    end
  elsif first?(method_name)
    first_pool do |conn|
      conn.evalsha(script_sha(method_name), [namespace], args)
    end
  else
    redis.with do |conn|
      conn.evalsha(script_sha(method_name), [namespace], args)
    end
  end
rescue Redis::CommandError => ex
  if ex.message.include?("NOSCRIPT") && !loaded
    redis.with_each do |conn|
      conn.script(:load, script_source(method_name))
    end

    loaded = true
    retry
  else
    raise
  end
end

Instance Attribute Details

#redisObject (readonly)

Returns the value of attribute redis.



10
11
12
# File 'lib/fairway/scripts.rb', line 10

def redis
  @redis
end

Class Method Details

.script_shasObject



6
7
8
# File 'lib/fairway/scripts.rb', line 6

def self.script_shas
  @script_shas ||= {}
end

Instance Method Details

#fairway_pull(timestamp, wait, queue_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fairway/scripts.rb', line 35

def fairway_pull(timestamp, wait, queue_name)
  loaded = false

  first_pool do |conn|
    conn.evalsha(script_sha(:fairway_pull), [namespace, timestamp, wait], [queue_name])
  end

rescue Redis::CommandError => ex
  if ex.message.include?("NOSCRIPT") && !loaded
    redis.with_each do |conn|
      conn.script(:load, script_source(:fairway_pull))
    end

    loaded = true
    retry
  else
    raise
  end
end

#register_queue(name, channel) ⇒ Object



17
18
19
20
21
# File 'lib/fairway/scripts.rb', line 17

def register_queue(name, channel)
  redis.with_each do |conn|
    conn.hset(registered_queues_key, name, channel)
  end
end

#registered_queuesObject



29
30
31
32
33
# File 'lib/fairway/scripts.rb', line 29

def registered_queues
  redis.with do |conn|
    conn.hgetall(registered_queues_key)
  end
end

#unregister_queue(name) ⇒ Object



23
24
25
26
27
# File 'lib/fairway/scripts.rb', line 23

def unregister_queue(name)
  redis.with_each do |conn|
    conn.hdel(registered_queues_key, name)
  end
end