Class: DelayedJobWeb

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/delayed_job_web/application/app.rb

Instance Method Summary collapse

Instance Method Details

#csrf_tokenObject



95
96
97
98
# File 'lib/delayed_job_web/application/app.rb', line 95

def csrf_token
  # Set up by Rack::Protection
  session[:csrf]
end

#csrf_token_tagObject



100
101
102
103
104
105
106
# File 'lib/delayed_job_web/application/app.rb', line 100

def csrf_token_tag
  # If csrf_token is nil, and we submit a blank string authenticity_token
  # param, Rack::Protection will fail.
  if csrf_token
    "<input type='hidden' name='authenticity_token' value='#{h csrf_token}'>"
  end
end

#current_pageObject



34
35
36
# File 'lib/delayed_job_web/application/app.rb', line 34

def current_page
  url_path request.path_info.sub('/','')
end

#delayed_jobObject



87
88
89
90
91
92
93
# File 'lib/delayed_job_web/application/app.rb', line 87

def delayed_job
  begin
    Delayed::Job
  rescue
    false
  end
end

#delayed_jobs(type, queues = []) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/delayed_job_web/application/app.rb', line 164

def delayed_jobs(type, queues = [])
  rel = delayed_job

  rel =
    case type
    when :working
      rel.where('locked_at IS NOT NULL AND failed_at IS NULL')
    when :failed
      rel.where('last_error IS NOT NULL')
    when :pending
      rel.where(:attempts => 0, :locked_at => nil)
    else
      rel
    end

  rel = rel.where(:queue => queues) unless queues.empty?

  rel
end

#h(text) ⇒ Object



68
69
70
# File 'lib/delayed_job_web/application/app.rb', line 68

def h(text)
  Rack::Utils.escape_html(text)
end

#partial(template, local_vars = {}) ⇒ Object



188
189
190
191
192
193
# File 'lib/delayed_job_web/application/app.rb', line 188

def partial(template, local_vars = {})
  @partial = true
  erb(template.to_sym, {:layout => false}, local_vars)
ensure
  @partial = false
end

#path_prefixObject



72
73
74
# File 'lib/delayed_job_web/application/app.rb', line 72

def path_prefix
  request.env['SCRIPT_NAME']
end

#per_pageObject



42
43
44
# File 'lib/delayed_job_web/application/app.rb', line 42

def per_page
  params[:per_page].to_i > 0 ? params[:per_page].to_i : 20
end

#pollObject



205
206
207
208
209
210
211
212
# File 'lib/delayed_job_web/application/app.rb', line 205

def poll
  if @polling
    text = "Last Updated: #{Time.now.strftime("%H:%M:%S")}"
  else
    text = "<a href='#{u(request.path_info + ".poll")}' rel='poll'>Live Poll</a>"
  end
  "<p class='poll'>#{text}</p>"
end

#queue_path(queue) ⇒ Object



54
55
56
57
58
# File 'lib/delayed_job_web/application/app.rb', line 54

def queue_path(queue)
  with_queue(queue) do
    url_path(:overview)
  end
end

#show_for_polling(page) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/delayed_job_web/application/app.rb', line 214

def show_for_polling(page)
  content_type "text/html"
  @polling = true
  # show(page.to_sym, false).gsub(/\s{1,}/, ' ')
  @jobs = delayed_jobs(page.to_sym, @queues)
  erb(page.to_sym, {:layout => false})
end

#startObject



38
39
40
# File 'lib/delayed_job_web/application/app.rb', line 38

def start
  params[:start].to_i
end

#tabsObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/delayed_job_web/application/app.rb', line 76

def tabs
  [
    {:name => 'Overview', :path => '/overview'},
    {:name => 'Enqueued', :path => '/enqueued'},
    {:name => 'Working', :path => '/working'},
    {:name => 'Pending', :path => '/pending'},
    {:name => 'Failed', :path => '/failed'},
    {:name => 'Stats', :path => '/stats'}
  ]
end

#url_path(*path_parts) ⇒ Object Also known as: u



46
47
48
49
50
# File 'lib/delayed_job_web/application/app.rb', line 46

def url_path(*path_parts)
  url = [ path_prefix, path_parts ].join("/").squeeze('/')
  url += "?queues=#{CGI.escape(@queues.join(","))}" unless @queues.empty?
  url
end

#with_queue(queue, &block) ⇒ Object



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

def with_queue(queue, &block)
  aux_queues = @queues
  @queues = Array(queue)
  result  = block.call
  @queues = aux_queues
  result
end