clockwork_web 0.0.1 → 0.0.2

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: 8d4554a13250d7249b7074982f164055299db2de
4
- data.tar.gz: 079e42ce8ce88456cc44d5000a085a88e2c16ce7
3
+ metadata.gz: f7c3867ecea98c7c51988649055e143e26ba30e4
4
+ data.tar.gz: 14e364f74e9f89798dd7a399bf7d78fee1f40cc9
5
5
  SHA512:
6
- metadata.gz: 6ba42432d2220db8080cfc978a4c0a39a9f6e0ababe50d6218b976a061b518e8ff75fe1d5858497123262b24ef09922acd3e81e1dd19c0d80d67347ab3799858
7
- data.tar.gz: 5dc43184b56bc5b15cf5716d4209cf91c05491b2cac070e55eaee6ab90a0d2049e2a4d6eb2efddb5e033161662825a3b5550d47684a01c724126b4443917b40c
6
+ metadata.gz: db09daf6bff8cc75ce2829a9501a32c60ed4951ea7b6a54c2a0ca443b195fe738464b8a4ecfcaef0da75ea40390d4c2b5946644660e365b45dc947326d39970e
7
+ data.tar.gz: 7423110942b760aa63b881a455a1cec2884c9acbb7a097023f32233ba1914d536179b13636f7a236653e243acc9b36de29e1aa01950ff42b1f0d5110c3887cf3
data/README.md CHANGED
@@ -47,6 +47,14 @@ authenticate :user, lambda {|user| user.admin? } do
47
47
  end
48
48
  ```
49
49
 
50
+ ## Customize
51
+
52
+ Change clock path
53
+
54
+ ```ruby
55
+ ClockworkWeb.clock_path = Rails.root.join("clock") # default
56
+ ```
57
+
50
58
  ## TODO
51
59
 
52
60
  - demo
@@ -6,8 +6,27 @@ module ClockworkWeb
6
6
  http_basic_authenticate_with name: ENV["CLOCKWORK_USERNAME"], password: ENV["CLOCKWORK_PASSWORD"] if ENV["CLOCKWORK_PASSWORD"]
7
7
 
8
8
  def index
9
- @events = Clockwork.manager.instance_variable_get(:@events).sort_by{|e| [e.instance_variable_get(:@period), e.job.to_s] }
10
- # TODO fetch all disabled jobs and last runs in one call
9
+ @events =
10
+ Clockwork.manager.instance_variable_get(:@events).sort_by do |e|
11
+ at = e.instance_variable_get(:@at)
12
+ [
13
+ e.instance_variable_get(:@period),
14
+ (at && at.instance_variable_get(:@hour)) || -1,
15
+ (at && at.instance_variable_get(:@min)) || -1,
16
+ e.job.to_s
17
+ ]
18
+ end
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
11
30
  end
12
31
 
13
32
  def job
@@ -3,9 +3,9 @@ module ClockworkWeb
3
3
 
4
4
  def friendly_period(period)
5
5
  if period % 1.day == 0
6
- "#{period / 1.day} day"
6
+ pluralize(period / 1.day, "day")
7
7
  elsif period % 1.hour == 0
8
- "#{period / 1.hour} hour"
8
+ pluralize(period / 1.hour, "hour")
9
9
  elsif period % 1.minute == 0
10
10
  "#{period / 1.minute} min"
11
11
  else
@@ -13,13 +13,17 @@ module ClockworkWeb
13
13
  end
14
14
  end
15
15
 
16
- def last_run(event)
17
- if ClockworkWeb.redis
18
- # TODO get all events at once
19
- timestamp = ClockworkWeb.redis.get("clockwork:last_run:#{event.job}")
20
- if timestamp
21
- time_ago_in_words(Time.at(timestamp.to_i))
22
- end
16
+ def last_run(timestamp)
17
+ if timestamp
18
+ time_ago_in_words(Time.at(timestamp.to_i))
19
+ end
20
+ end
21
+
22
+ def friendly_time_part(time_part)
23
+ if time_part
24
+ time_part.to_s.rjust(2, "0")
25
+ else
26
+ "**"
23
27
  end
24
28
  end
25
29
 
@@ -62,22 +62,20 @@
62
62
  </thead>
63
63
  <tbody>
64
64
  <% @events.each do |event| %>
65
- <% enabled = ClockworkWeb.enabled?(event.job) %>
65
+ <% enabled = !@disabled[event.job] %>
66
66
  <tr class="<%= enabled ? "" : "disabled" %>">
67
67
  <td><%= event.job %></td>
68
68
  <td>
69
69
  <%= friendly_period(event.instance_variable_get(:@period)) %>
70
70
  <% at = event.instance_variable_get(:@at) %>
71
71
  <% if at %>
72
- <div>
73
- <!-- TODO better format -->
74
- min: <%= at.instance_variable_get(:@min) %>
75
- hour: <%= at.instance_variable_get(:@hour) %>
76
- wday: <%= at.instance_variable_get(:@wday) %>
77
- </div>
72
+ at <%= friendly_time_part(at.instance_variable_get(:@hour)) %>:<%= friendly_time_part(at.instance_variable_get(:@min)) %>
73
+ <% end %>
74
+ <% if event.instance_variable_get(:@if) %>
75
+ if __
78
76
  <% end %>
79
77
  </td>
80
- <td><%= last_run(event) %></td>
78
+ <td><%= last_run(@last_run[event.job]) %></td>
81
79
  <td><%= button_to enabled ? "Disable" : "Enable", home_job_path(job: event.job, enable: !enabled) %></td>
82
80
  </tr>
83
81
  <% end %>
@@ -9,21 +9,34 @@ require "clockwork_web/engine"
9
9
 
10
10
  module ClockworkWeb
11
11
  class << self
12
+ attr_accessor :clock_path
12
13
  attr_accessor :redis
13
14
  end
14
15
 
15
16
  def self.enable(job)
16
- ClockworkWeb.redis.del("clockwork:disabled:#{job}")
17
- true
17
+ if redis
18
+ redis.del("clockwork:disabled:#{job}")
19
+ true
20
+ else
21
+ false
22
+ end
18
23
  end
19
24
 
20
25
  def self.disable(job)
21
- ClockworkWeb.redis.set("clockwork:disabled:#{job}", 1)
22
- true
26
+ if redis
27
+ redis.set("clockwork:disabled:#{job}", 1)
28
+ true
29
+ else
30
+ false
31
+ end
23
32
  end
24
33
 
25
34
  def self.enabled?(job)
26
- !ClockworkWeb.redis.exists("clockwork:disabled:#{job}")
35
+ if redis
36
+ !redis.exists("clockwork:disabled:#{job}")
37
+ else
38
+ true
39
+ end
27
40
  end
28
41
  end
29
42
 
@@ -31,11 +44,9 @@ module Clockwork
31
44
  on(:before_run) do |t|
32
45
  run = true
33
46
  safely do
34
- if ClockworkWeb.redis
35
- run = ClockworkWeb.enabled?(t.job)
36
- if run
37
- ClockworkWeb.redis.set("clockwork:last_run:#{t.job}", Time.now.to_i)
38
- end
47
+ run = ClockworkWeb.enabled?(t.job)
48
+ if run && ClockworkWeb.redis
49
+ ClockworkWeb.redis.set("clockwork:last_run:#{t.job}", Time.now.to_i)
39
50
  end
40
51
  end
41
52
  run
@@ -3,7 +3,8 @@ module ClockworkWeb
3
3
  isolate_namespace ClockworkWeb
4
4
 
5
5
  initializer "clockwork_web" do |app|
6
- require Rails.root.join("clock") # hacky
6
+ ClockworkWeb.clock_path ||= Rails.root.join("clock")
7
+ require ClockworkWeb.clock_path
7
8
  end
8
9
  end
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module ClockworkWeb
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clockwork_web
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane