Class: Resque::Worker

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

Instance Method Summary collapse

Instance Method Details

#heartbeat!Object



32
33
34
35
36
37
# File 'lib/resque/heartbeat.rb', line 32

def heartbeat!
  heartbeat_redis.sadd(:workers, self)
  heartbeat_redis.set("worker:#{self}:heartbeat", Time.now.to_s)
rescue Exception => e
  p e
end

#heartbeat_redisObject

apparently the Redis connection is not thread-safe, so we connect another instance see github.com/ezmobius/redis-rb/issues#issue/75



25
26
27
28
29
30
# File 'lib/resque/heartbeat.rb', line 25

def heartbeat_redis
  @heartbeat_redis ||= begin
    redis = Redis.connect(:url => Travis.config['redis']['url'])
    Redis::Namespace.new(:resque, :redis => redis)
  end
end

#last_heartbeatObject



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

def last_heartbeat
  redis.get("worker:#{self}:heartbeat") || started
end

#last_heartbeat_before?(seconds) ⇒ Boolean

Returns:

  • (Boolean)


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

def last_heartbeat_before?(seconds)
  Time.parse(last_heartbeat).utc < (Time.now.utc - seconds)
end

#startup_with_heartbeatObject Also known as: startup



11
12
13
14
15
16
17
18
19
# File 'lib/resque/heartbeat.rb', line 11

def startup_with_heartbeat
  startup_without_heartbeat
  Thread.new do
    loop do
      sleep(2)
      heartbeat!
    end
  end
end