Module: Sidekiq::Status::Web::Helpers

Defined in:
lib/sidekiq-status/helpers.rb

Constant Summary collapse

COMMON_STATUS_HASH_KEYS =
%w(enqueued_at started_at updated_at ended_at jid status worker args label pct_complete total at message elapsed eta)

Instance Method Summary collapse

Instance Method Details

#add_details_to_status(status) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/sidekiq-status/helpers.rb', line 29

def add_details_to_status(status)
  status['label'] = status_label(status['status'])
  status["pct_complete"] ||= pct_complete(status)
  status["elapsed"] ||= elapsed(status).to_s
  status["eta"] ||= eta(status).to_s
  status["custom"] = process_custom_data(status)
  return status
end

#csrf_tagObject



16
17
18
# File 'lib/sidekiq-status/helpers.rb', line 16

def csrf_tag
  "<input type='hidden' name='authenticity_token' value='#{env[:csrf_token]}'/>"
end

#delete_job_actionObject



88
89
90
91
# File 'lib/sidekiq-status/helpers.rb', line 88

def delete_job_action
  Sidekiq::Status.delete(safe_url_params("jid"))
  throw :halt, [302, { "Location" => request.referer }, []]
end

#elapsed(status) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sidekiq-status/helpers.rb', line 47

def elapsed(status)
  started = Sidekiq::Status.started_at(status['jid'])
  return nil unless started
  case status['status']
  when 'complete', 'failed', 'stopped', 'interrupted'
    ended = Sidekiq::Status.ended_at(status['jid'])
    return nil unless ended
    ended - started
  when 'working', 'retrying'
    Time.now.to_i - started
  end
end

#eta(status) ⇒ Object



60
61
62
# File 'lib/sidekiq-status/helpers.rb', line 60

def eta(status)
  Sidekiq::Status.eta(status['jid']) if status['status'] == 'working'
end

#has_sort_by?(value) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/sidekiq-status/helpers.rb', line 77

def has_sort_by?(value)
  ["worker", "status", "updated_at", "pct_complete", "message", "args", "elapsed"].include?(value)
end

#pct_complete(status) ⇒ Object



42
43
44
45
# File 'lib/sidekiq-status/helpers.rb', line 42

def pct_complete(status)
  return 100 if status['status'] == 'complete'
  Sidekiq::Status::pct_complete(status['jid']) || 0
end

#poll_pathObject



20
21
22
# File 'lib/sidekiq-status/helpers.rb', line 20

def poll_path
  "?#{request.query_string}" if safe_url_params("poll")
end

#process_custom_data(hash) ⇒ Object



38
39
40
# File 'lib/sidekiq-status/helpers.rb', line 38

def process_custom_data(hash)
  hash.reject { |key, _| COMMON_STATUS_HASH_KEYS.include?(key) }
end

#retry_job_actionObject



81
82
83
84
85
86
# File 'lib/sidekiq-status/helpers.rb', line 81

def retry_job_action
  job = Sidekiq::RetrySet.new.find_job(safe_url_params("jid"))
  job ||= Sidekiq::DeadSet.new.find_job(safe_url_params("jid"))
  job.retry if job
  throw :halt, [302, { "Location" => request.referer }, []]
end

#safe_route_params(key) ⇒ Object



11
12
13
14
# File 'lib/sidekiq-status/helpers.rb', line 11

def safe_route_params(key)
  return route_params(key) if Sidekiq.major_version >= 8
  env["rack.route_params"][key.to_sym]
end

#safe_url_params(key) ⇒ Object



6
7
8
9
# File 'lib/sidekiq-status/helpers.rb', line 6

def safe_url_params(key)
  return url_params(key) if Sidekiq.major_version >= 8
  request.params[key.to_s]
end

#sidekiq_status_template(name) ⇒ Object



24
25
26
27
# File 'lib/sidekiq-status/helpers.rb', line 24

def sidekiq_status_template(name)
  path = File.join(VIEW_PATH, name.to_s) + ".erb"
  File.open(path).read
end

#status_label(status) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sidekiq-status/helpers.rb', line 64

def status_label(status)
  case status
  when 'complete'
    'success'
  when 'working', 'retrying'
    'warning'
  when 'queued'
    'primary'
  else
    'danger'
  end
end