Class: QueueBus::Heartbeat

Inherits:
Object
  • Object
show all
Includes:
Worker
Defined in:
lib/queue_bus/heartbeat.rb

Overview

publishes event about the current time

Class Method Summary collapse

Methods included from Worker

included, #perform

Class Method Details

.environment_name ⇒ Object



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

def environment_name
  ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["BUS_ENV"]
end

.get_saved_minute! ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/queue_bus/heartbeat.rb', line 51

def get_saved_minute!
  key = ::QueueBus.redis { |redis| redis.get(redis_key) }
  return nil if key.nil?
  case environment_name
  when 'development', 'test'
    # only 3 minutes in development; otherwise, TONS of events if not run in a while
    three_ago = Time.now.to_i - 3*60*60
    key = three_ago if key.to_i < three_ago
  end
  return key.to_i
end

.lock! ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/queue_bus/heartbeat.rb', line 16

def lock!
  now = Time.now.to_i
  timeout = now + lock_seconds + 2

  ::QueueBus.redis do |redis|
    # return true if we successfully acquired the lock
    return timeout if redis.setnx(lock_key, timeout)

    # see if the existing timeout is still valid and return false if it is
    # (we cannot acquire the lock during the timeout period)
    return 0 if now <= redis.get(lock_key).to_i

    # otherwise set the timeout and ensure that no other worker has
    # acquired the lock
    if now > redis.getset(lock_key, timeout).to_i
      return timeout
    else
      return 0
    end
  end
end

.lock_key ⇒ Object



8
9
10
# File 'lib/queue_bus/heartbeat.rb', line 8

def lock_key
  "bus:heartbeat:lock"
end

.lock_seconds ⇒ Object



12
13
14
# File 'lib/queue_bus/heartbeat.rb', line 12

def lock_seconds
  60
end

.perform ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/queue_bus/heartbeat.rb', line 67

def perform
  real_now = Time.now.to_i
  run_until = lock! - 2
  return if run_until < real_now

  while((real_now = Time.now.to_i) < run_until)
    minutes = real_now.to_i / 60
    last = get_saved_minute!
    if last
      break if minutes <= last
      minutes = last + 1
    end

    seconds = minutes * (60)
    hours   = minutes / (60)
    days    = minutes / (60*24)

    now     = Time.at(seconds)

    attributes = {}
    attributes["epoch_seconds"] = seconds
    attributes["epoch_minutes"] = minutes
    attributes["epoch_hours"]   = hours
    attributes["epoch_days"]    = days

    attributes["minute"] = now.min
    attributes["hour"]   = now.hour
    attributes["day"]    = now.day
    attributes["month"]  = now.month
    attributes["year"]   = now.year
    attributes["yday"]   = now.yday
    attributes["wday"]   = now.wday

    ::QueueBus.publish("heartbeat_minutes", attributes)
    set_saved_minute!(minutes)
  end

  unlock!
end

.redis_key ⇒ Object



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

def redis_key
  "bus:heartbeat:timestamp"
end

.set_saved_minute!(epoch_minute) ⇒ Object



63
64
65
# File 'lib/queue_bus/heartbeat.rb', line 63

def set_saved_minute!(epoch_minute)
  ::QueueBus.redis { |redis| redis.set(redis_key, epoch_minute) }
end

.unlock! ⇒ Object



38
39
40
# File 'lib/queue_bus/heartbeat.rb', line 38

def unlock!
  ::QueueBus.redis { |redis| redis.del(lock_key) }
end