Module: ClockworkWeb

Defined in:
lib/clockwork_web.rb,
lib/clockwork_web/engine.rb,
lib/clockwork_web/version.rb,
app/helpers/clockwork_web/home_helper.rb,
app/controllers/clockwork_web/home_controller.rb

Defined Under Namespace

Modules: HomeHelper Classes: Engine, HomeController

Constant Summary collapse

LAST_RUNS_KEY =
"clockwork:last_runs"
DISABLED_KEY =
"clockwork:disabled"
HEARTBEAT_KEY =
"clockwork:heartbeat"
STATUS_KEY =
"clockwork:status"
VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clock_pathObject

Returns the value of attribute clock_path.



17
18
19
# File 'lib/clockwork_web.rb', line 17

def clock_path
  @clock_path
end

.monitorObject

Returns the value of attribute monitor.



19
20
21
# File 'lib/clockwork_web.rb', line 19

def monitor
  @monitor
end

.on_job_updateObject

Returns the value of attribute on_job_update.



21
22
23
# File 'lib/clockwork_web.rb', line 21

def on_job_update
  @on_job_update
end

.redisObject

Returns the value of attribute redis.



18
19
20
# File 'lib/clockwork_web.rb', line 18

def redis
  @redis
end

.running_thresholdObject

Returns the value of attribute running_threshold.



20
21
22
# File 'lib/clockwork_web.rb', line 20

def running_threshold
  @running_threshold
end

Class Method Details

.disable(job) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/clockwork_web.rb', line 35

def self.disable(job)
  if redis
    redis.sadd(DISABLED_KEY, job)
    true
  else
    false
  end
end

.disabled_jobsObject



52
53
54
55
56
57
58
# File 'lib/clockwork_web.rb', line 52

def self.disabled_jobs
  if redis
    Set.new(redis.smembers(DISABLED_KEY))
  else
    Set.new
  end
end

.enable(job) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/clockwork_web.rb', line 26

def self.enable(job)
  if redis
    redis.srem(DISABLED_KEY, job)
    true
  else
    false
  end
end

.enabled?(job) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/clockwork_web.rb', line 44

def self.enabled?(job)
  if redis
    !redis.sismember(DISABLED_KEY, job)
  else
    true
  end
end

.heartbeatObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/clockwork_web.rb', line 83

def self.heartbeat
  if redis
    heartbeat = Time.now.to_i
    if heartbeat % 10 == 0
      prev_heartbeat = redis.getset(HEARTBEAT_KEY, heartbeat).to_i
      if prev_heartbeat >= heartbeat
        redis.setex(STATUS_KEY, 60, "multiple")
      end
    end
  end
end

.last_heartbeatObject



74
75
76
77
78
79
80
81
# File 'lib/clockwork_web.rb', line 74

def self.last_heartbeat
  if redis
    timestamp = redis.get(HEARTBEAT_KEY)
    if timestamp
      Time.at(timestamp.to_i)
    end
  end
end

.last_runsObject



60
61
62
63
64
65
66
# File 'lib/clockwork_web.rb', line 60

def self.last_runs
  if redis
    Hash[ redis.hgetall(LAST_RUNS_KEY).map{|job, timestamp| [job, Time.at(timestamp.to_i)] }.sort_by{|job, time| [time, job] } ]
  else
    {}
  end
end

.multiple?Boolean

Returns:

  • (Boolean)


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

def self.multiple?
  redis && redis.get(STATUS_KEY) == "multiple"
end

.running?Boolean

Returns:

  • (Boolean)


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

def self.running?
  last_heartbeat && last_heartbeat > Time.now - running_threshold
end

.set_last_run(job) ⇒ Object



68
69
70
71
72
# File 'lib/clockwork_web.rb', line 68

def self.set_last_run(job)
  if redis
    redis.hset(LAST_RUNS_KEY, job, Time.now.to_i)
  end
end