sidekiq_monitor 0.0.9 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -113,6 +113,15 @@ Sidekiq::Monitor.options[:graphs] = {
113
113
 
114
114
  `ALL` and `OTHER` are special keys: `ALL` will show all queues and `OTHER` will show all queues that aren't matched by the regex keys.
115
115
 
116
+ #### Poll Interval
117
+
118
+ The UI uses polling to update its data. By default, the polling interval is 3000ms, but you can adjust this like so:
119
+
120
+ ```ruby
121
+ # config/initializers/sidekiq_monitor.rb
122
+ Sidekiq::Monitor.options[:poll_interval] = 5000
123
+ ```
124
+
116
125
  ### Authentication
117
126
 
118
127
  You'll likely want to restrict access to this interface in a production setting. To do this, you can use routing constraints:
@@ -91,9 +91,21 @@ class SidekiqMonitor.AbstractJobsTable
91
91
 
92
92
  result_html = ''
93
93
  if status == 'failed'
94
+ rows_html = ''
95
+ for key, value of result
96
+ if key != 'message' && key != 'backtrace'
97
+ rows_html += "<tr><td>#{key}</td><td>#{JSON.stringify(value, null, 2)}</td></tr>"
98
+ if rows_html
99
+ rows_html = """
100
+ <h4>Result</h4>
101
+ <table class="table table-striped">
102
+ #{rows_html}
103
+ </table>
104
+ """
94
105
  result_html = """
95
106
  <h4>Error</h4>
96
107
  #{result.message}
108
+ #{rows_html}
97
109
  <h5>Backtrace</h5>
98
110
  <pre>
99
111
  #{result.backtrace.join("\n")}
@@ -180,7 +192,7 @@ class SidekiqMonitor.AbstractJobsTable
180
192
  start_polling: =>
181
193
  setInterval =>
182
194
  @on_poll()
183
- , 3000
195
+ , SidekiqMonitor.settings.poll_interval
184
196
 
185
197
  format_time_ago: (time) =>
186
198
  if time?
@@ -3,7 +3,7 @@ class SidekiqMonitor.Graph
3
3
  constructor: ->
4
4
  options =
5
5
  selector: '.graph'
6
- poll_interval: 3000
6
+ poll_interval: SidekiqMonitor.settings.poll_interval
7
7
  @initialize(options)
8
8
 
9
9
  initialize: (options) =>
@@ -4,3 +4,4 @@ window.SidekiqMonitor =
4
4
  api_url: (path) ->
5
5
  "<%= url_helpers.sidekiq_monitor_path %>api/#{path}"
6
6
  graphs: <%= Sidekiq::Monitor.options[:graphs].to_json %>
7
+ poll_interval: <%= Sidekiq::Monitor.options[:poll_interval] %>
@@ -1,7 +1,7 @@
1
1
  module Sidekiq
2
2
  module Monitor
3
3
  class Job < ActiveRecord::Base
4
- attr_accessible :args, :class_name, :enqueued_at, :finished_at, :jid, :name, :queue, :result, :retry, :started_at, :status if ActiveRecord::VERSION::MAJOR < 4
4
+ attr_accessible :args, :class_name, :enqueued_at, :finished_at, :jid, :name, :queue, :result, :retry, :started_at, :status if ActiveRecord::VERSION::MAJOR < 4 || ActiveRecord.constants.include?(:MassAssignmentSecurity)
5
5
 
6
6
  serialize :args
7
7
  serialize :result
@@ -26,4 +26,6 @@ html lang="en"
26
26
  .container
27
27
  = yield
28
28
 
29
- = javascript_include_tag "sidekiq/monitor/application"
29
+ = javascript_include_tag "sidekiq/monitor/application"
30
+ - Sidekiq::Monitor.options[:javascripts].each do |javascript|
31
+ = javascript_include_tag javascript
@@ -2,60 +2,25 @@ module Sidekiq
2
2
  module Monitor
3
3
  class Processor
4
4
  def queue(worker_class, item, queue)
5
- args = item['args']
6
- name = job_name(worker_class, args)
7
- Sidekiq::Monitor::Job.find_or_create_by_jid(
8
- jid: item['jid'],
9
- queue: queue,
10
- class_name: worker_class.is_a?(String) ? worker_class : worker_class.name,
11
- args: args,
12
- retry: item['retry'],
13
- enqueued_at: DateTime.now,
14
- status: 'queued',
15
- name: name
16
- )
5
+ job = find_or_initialize_job(worker_class, item, queue)
6
+ job.save
17
7
  end
18
8
 
19
- def start(worker, msg, queue)
20
- jid = msg['jid']
21
- args = msg['args']
22
- now = DateTime.now
23
- job = Sidekiq::Monitor::Job.find_by_jid(jid)
24
- if job.blank?
25
- name = job_name(worker.class, args)
26
- job = Sidekiq::Monitor::Job.new(
27
- jid: jid,
28
- queue: queue,
29
- class_name: worker.class.name,
30
- args: args,
31
- retry: msg['retry'],
32
- enqueued_at: now,
33
- name: name
34
- )
35
- end
9
+ def start(worker, item, queue)
10
+ job = find_or_initialize_job(worker, item, queue)
36
11
  job.update_attributes(
37
- started_at: now,
12
+ started_at: DateTime.now,
38
13
  status: 'running'
39
14
  )
40
15
  end
41
16
 
42
- def error(worker, msg, queue, exception)
43
- result = {
44
- message: "#{exception.class.name}: #{exception.message}",
45
- backtrace: exception.backtrace
46
- }
47
- job = find_job(msg)
48
- return unless job
49
- job.update_attributes(
50
- finished_at: DateTime.now,
51
- status: 'failed',
52
- result: result
53
- )
17
+ def error(worker, item, queue, exception)
18
+ job = find_or_initialize_job(worker, item, queue)
19
+ set_error(job, exception)
54
20
  end
55
21
 
56
- def complete(worker, msg, queue, return_value)
57
- job = find_job(msg)
58
- return unless job
22
+ def complete(worker, item, queue, return_value)
23
+ job = find_or_initialize_job(worker, item, queue)
59
24
  job.update_attributes(
60
25
  finished_at: DateTime.now,
61
26
  status: 'complete',
@@ -65,12 +30,63 @@ module Sidekiq
65
30
 
66
31
  protected
67
32
 
68
- def find_job(msg)
69
- Sidekiq::Monitor::Job.find_by_jid(msg['jid'])
33
+ def find_or_initialize_job(worker, item, queue, options={})
34
+ defaults = {
35
+ set_name: true
36
+ }
37
+ options.reverse_merge!(defaults)
38
+
39
+ worker_class = nil
40
+ if worker.is_a?(String)
41
+ worker_class = worker.constantize
42
+ elsif worker.is_a?(Class)
43
+ worker_class = worker
44
+ else
45
+ worker_class = worker.class
46
+ end
47
+
48
+ job = Sidekiq::Monitor::Job.find_by_jid(item['jid'])
49
+ if job.blank?
50
+ attributes = {
51
+ jid: item['jid'],
52
+ queue: queue,
53
+ class_name: worker_class.name,
54
+ args: item['args'],
55
+ retry: item['retry'],
56
+ enqueued_at: DateTime.now,
57
+ status: 'queued'
58
+ }
59
+ if options[:set_name] == true
60
+ attributes[:name] = job_name(worker_class, item, queue)
61
+ end
62
+ job = Sidekiq::Monitor::Job.new(attributes)
63
+ end
64
+ job
65
+ end
66
+
67
+ def job_name(worker_class, item, queue)
68
+ args = item['args']
69
+ begin
70
+ worker_class.respond_to?(:job_name) ? worker_class.job_name(*args) : nil
71
+ rescue Exception => exception
72
+ # If the job doesn't exist yet, we'll need to create it
73
+ job = find_or_initialize_job(worker_class, item, queue, set_name: false)
74
+ set_error(job, exception)
75
+ raise exception
76
+ end
70
77
  end
71
78
 
72
- def job_name(worker_class, args)
73
- worker_class.respond_to?(:job_name) ? worker_class.job_name(*args) : nil
79
+ def set_error(job, exception)
80
+ result = job.result.present? ? job.result.symbolize_keys : {}
81
+ result.merge!({
82
+ message: "#{exception.class.name}: #{exception.message}",
83
+ backtrace: exception.backtrace
84
+ })
85
+ job.update_attributes(
86
+ finished_at: DateTime.now,
87
+ status: 'failed',
88
+ result: result
89
+ )
74
90
  end
75
91
  end
76
92
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Monitor
3
- VERSION = '0.0.9'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -10,7 +10,9 @@ Dir.glob("#{directory}/../../app/datatables/sidekiq/monitor/jobs_datatable.rb")
10
10
  module Sidekiq
11
11
  module Monitor
12
12
  DEFAULTS = {
13
- :graphs => nil
13
+ :graphs => nil,
14
+ :javascripts => [],
15
+ :poll_interval => 3000
14
16
  }
15
17
 
16
18
  def self.options
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-25 00:00:00.000000000 Z
12
+ date: 2014-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sidekiq