clockwork_web 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of clockwork_web might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7c3867ecea98c7c51988649055e143e26ba30e4
4
- data.tar.gz: 14e364f74e9f89798dd7a399bf7d78fee1f40cc9
3
+ metadata.gz: 925bad3f5186da79cd04a72e1aee6eac89341d9e
4
+ data.tar.gz: 7c141e2b984332b34e16f8ad947d72203468a31a
5
5
  SHA512:
6
- metadata.gz: db09daf6bff8cc75ce2829a9501a32c60ed4951ea7b6a54c2a0ca443b195fe738464b8a4ecfcaef0da75ea40390d4c2b5946644660e365b45dc947326d39970e
7
- data.tar.gz: 7423110942b760aa63b881a455a1cec2884c9acbb7a097023f32233ba1914d536179b13636f7a236653e243acc9b36de29e1aa01950ff42b1f0d5110c3887cf3
6
+ metadata.gz: 24497bfea53482d963022e171e0910cc21ce79f01ebc101a3265aac470fe25b7888e9d8237516b8eb5532e677c08ab141496a21277bdb6c1fe78880637f02925
7
+ data.tar.gz: da8ffb43b16dd28346b0e3aa099f440646a787da6b81b607921b8d95e52eeef86d002d76ee81e667fa82e0f8875f527af5ce99d773483b30082d2ed4e3e97e21
data/README.md CHANGED
@@ -1,13 +1,15 @@
1
1
  # Clockwork Web
2
2
 
3
- A web interface for [Clockwork](https://github.com/tomykaira/clockwork)
3
+ A web interface for Clockwork
4
+
5
+ [View the demo](https://clockwork-web.herokuapp.com/)
6
+
7
+ [screenshot goes here]
4
8
 
5
9
  - see list of jobs
6
- - see last run
10
+ - monitor jobs
7
11
  - disable jobs
8
12
 
9
- Screenshot, demo, and better UI coming soon
10
-
11
13
  ## Installation
12
14
 
13
15
  Add this line to your application’s Gemfile:
@@ -24,7 +26,7 @@ mount ClockworkWeb::Engine, at: "clockwork"
24
26
 
25
27
  Be sure to secure the dashboard in production.
26
28
 
27
- To see the last run and disable jobs, hook up Redis.
29
+ To see the last run and disable jobs, hook up Redis in an initializer.
28
30
 
29
31
  ```ruby
30
32
  ClockworkWeb.redis = Redis.new
@@ -47,6 +49,13 @@ authenticate :user, lambda {|user| user.admin? } do
47
49
  end
48
50
  ```
49
51
 
52
+ ## Monitoring
53
+
54
+ ```ruby
55
+ ClockworkWeb.running?
56
+ ClockworkWeb.multiple?
57
+ ```
58
+
50
59
  ## Customize
51
60
 
52
61
  Change clock path
@@ -55,9 +64,14 @@ Change clock path
55
64
  ClockworkWeb.clock_path = Rails.root.join("clock") # default
56
65
  ```
57
66
 
67
+ Turn off monitoring
68
+
69
+ ```ruby
70
+ ClockworkWeb.monitor = false
71
+ ```
72
+
58
73
  ## TODO
59
74
 
60
- - demo
61
75
  - better design
62
76
 
63
77
  ## Contributing
@@ -17,16 +17,9 @@ module ClockworkWeb
17
17
  ]
18
18
  end
19
19
 
20
- @last_run = {}
21
- @disabled = {}
22
- if ClockworkWeb.redis
23
- keys = @events.flat_map{|e| ["clockwork:last_run:#{e.job}", "clockwork:disabled:#{e.job}"] }
24
- values = ClockworkWeb.redis.mget(keys)
25
- @events.each_with_index do |event, i|
26
- @last_run[event.job] = values[i * 2]
27
- @disabled[event.job] = values[i * 2 + 1]
28
- end
29
- end
20
+ @last_runs = ClockworkWeb.last_runs
21
+ @disabled = ClockworkWeb.disabled_jobs
22
+ @last_heartbeat = ClockworkWeb.last_heartbeat
30
23
  end
31
24
 
32
25
  def job
@@ -13,9 +13,9 @@ module ClockworkWeb
13
13
  end
14
14
  end
15
15
 
16
- def last_run(timestamp)
17
- if timestamp
18
- time_ago_in_words(Time.at(timestamp.to_i))
16
+ def last_run(time)
17
+ if time
18
+ time_ago_in_words(time)
19
19
  end
20
20
  end
21
21
 
@@ -51,6 +51,25 @@
51
51
  </head>
52
52
  <body>
53
53
  <div class="container">
54
+ <% if ClockworkWeb.redis %>
55
+ <% if ClockworkWeb.monitor %>
56
+ <% if ClockworkWeb.multiple? %>
57
+ <p>Multiple clockwork processes detected</p>
58
+ <% elsif ClockworkWeb.running? %>
59
+ <p>Clockwork is running</p>
60
+ <% else %>
61
+ <p>
62
+ Clockwork is not running
63
+ <% if @last_heartbeat %>
64
+ - last heartbeat was <%= time_ago_in_words(@last_heartbeat) %> ago
65
+ <% end %>
66
+ </p>
67
+ <% end %>
68
+ <% end %>
69
+ <% else %>
70
+ <p>Add Redis for monitoring and disabling jobs</p>
71
+ <% end %>
72
+
54
73
  <table>
55
74
  <thead>
56
75
  <tr>
@@ -62,7 +81,7 @@
62
81
  </thead>
63
82
  <tbody>
64
83
  <% @events.each do |event| %>
65
- <% enabled = !@disabled[event.job] %>
84
+ <% enabled = !@disabled.include?(event.job) %>
66
85
  <tr class="<%= enabled ? "" : "disabled" %>">
67
86
  <td><%= event.job %></td>
68
87
  <td>
@@ -75,8 +94,8 @@
75
94
  if __
76
95
  <% end %>
77
96
  </td>
78
- <td><%= last_run(@last_run[event.job]) %></td>
79
- <td><%= button_to enabled ? "Disable" : "Enable", home_job_path(job: event.job, enable: !enabled) %></td>
97
+ <td><%= last_run(@last_runs[event.job]) %></td>
98
+ <td><%= button_to enabled ? "Disable" : "Enable", home_job_path(job: event.job, enable: !enabled), disabled: !ClockworkWeb.redis %></td>
80
99
  </tr>
81
100
  <% end %>
82
101
  </tbody>
@@ -8,14 +8,20 @@ require "robustly"
8
8
  require "clockwork_web/engine"
9
9
 
10
10
  module ClockworkWeb
11
+ LAST_RUNS_KEY = "clockwork:last_runs"
12
+ DISABLED_KEY = "clockwork:disabled"
13
+ HEARTBEAT_KEY = "clockwork:heartbeat"
14
+
11
15
  class << self
12
16
  attr_accessor :clock_path
13
17
  attr_accessor :redis
18
+ attr_accessor :monitor
14
19
  end
20
+ self.monitor = true
15
21
 
16
22
  def self.enable(job)
17
23
  if redis
18
- redis.del("clockwork:disabled:#{job}")
24
+ redis.srem(DISABLED_KEY, job)
19
25
  true
20
26
  else
21
27
  false
@@ -24,7 +30,7 @@ module ClockworkWeb
24
30
 
25
31
  def self.disable(job)
26
32
  if redis
27
- redis.set("clockwork:disabled:#{job}", 1)
33
+ redis.sadd(DISABLED_KEY, job)
28
34
  true
29
35
  else
30
36
  false
@@ -33,20 +39,82 @@ module ClockworkWeb
33
39
 
34
40
  def self.enabled?(job)
35
41
  if redis
36
- !redis.exists("clockwork:disabled:#{job}")
42
+ !redis.sismember(DISABLED_KEY, job)
37
43
  else
38
44
  true
39
45
  end
40
46
  end
47
+
48
+ def self.disabled_jobs
49
+ if redis
50
+ Set.new(redis.smembers(DISABLED_KEY))
51
+ else
52
+ Set.new
53
+ end
54
+ end
55
+
56
+ def self.last_runs
57
+ if redis
58
+ Hash[ redis.hgetall(LAST_RUNS_KEY).map{|job, timestamp| [job, Time.at(timestamp.to_i)] }.sort_by{|job, time| [time, job] } ]
59
+ else
60
+ {}
61
+ end
62
+ end
63
+
64
+ def self.set_last_run(job)
65
+ if redis
66
+ redis.hset(LAST_RUNS_KEY, job, Time.now.to_i)
67
+ end
68
+ end
69
+
70
+ def self.last_heartbeat
71
+ if redis
72
+ timestamp = redis.get(HEARTBEAT_KEY)
73
+ if timestamp
74
+ Time.at(timestamp.to_i)
75
+ end
76
+ end
77
+ end
78
+
79
+ def self.heartbeat
80
+ if redis
81
+ heartbeat = Time.now.to_i
82
+ if heartbeat % 10 == 0
83
+ prev_heartbeat = redis.getset(HEARTBEAT_KEY, heartbeat).to_i
84
+ if heartbeat == prev_heartbeat
85
+ # TODO debounce
86
+ # TODO try to surface hostnames when this condition is detected
87
+ # TODO hook to take action
88
+ redis.setex("clockwork:status", 20, "multiple")
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ def self.running?
95
+ last_heartbeat && last_heartbeat > 60.seconds.ago
96
+ end
97
+
98
+ def self.multiple?
99
+ redis && redis.get("clockwork:status") == "multiple"
100
+ end
41
101
  end
42
102
 
43
103
  module Clockwork
44
- on(:before_run) do |t|
104
+ on(:before_tick) do
105
+ ClockworkWeb.heartbeat if ClockworkWeb.monitor
106
+ true
107
+ end
108
+
109
+ on(:before_run) do |event, t|
45
110
  run = true
46
111
  safely do
47
- run = ClockworkWeb.enabled?(t.job)
48
- if run && ClockworkWeb.redis
49
- ClockworkWeb.redis.set("clockwork:last_run:#{t.job}", Time.now.to_i)
112
+ run = ClockworkWeb.enabled?(event.job)
113
+ if run
114
+ ClockworkWeb.set_last_run(event.job)
115
+ else
116
+ manager.log "Skipping '#{event}'"
117
+ event.last = event.convert_timezone(t)
50
118
  end
51
119
  end
52
120
  run
@@ -4,7 +4,7 @@ module ClockworkWeb
4
4
 
5
5
  initializer "clockwork_web" do |app|
6
6
  ClockworkWeb.clock_path ||= Rails.root.join("clock")
7
- require ClockworkWeb.clock_path
7
+ require ClockworkWeb.clock_path.to_s
8
8
  end
9
9
  end
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module ClockworkWeb
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clockwork_web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-13 00:00:00.000000000 Z
11
+ date: 2015-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clockwork