Class: Resque::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/resque/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tabsObject



282
283
284
# File 'lib/resque/server.rb', line 282

def self.tabs
  @tabs ||= ["Overview", "Working", "Failed", "Queues", "Workers", "Stats"]
end

Instance Method Details

#distance_of_time_in_words(from_time, to_time = 0, include_seconds = true, options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/resque/server.rb', line 139

def distance_of_time_in_words(from_time, to_time = 0, include_seconds = true, options = {})
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  distance_in_minutes = (((to_time - from_time).abs)/60).round
  distance_in_seconds = ((to_time - from_time).abs).round

  ago = from_time > to_time ? ' ago' : ''
  
  
  case distance_in_minutes
  when 0..1
    return distance_in_minutes == 0 ?
    "less than 1 minute" + ago :
      "#{distance_in_minutes} minutes" + ago unless include_seconds
    
    case distance_in_seconds
    when 0..4   then "less than 5 seconds" + ago
    when 5..9   then "less than 10 seconds" + ago
    when 10..19 then "less than 20 seconds" + ago
    when 20..39 then "half a minute" + ago
    when 40..59 then "less than 1 minute" + ago
    else             "1 minute" + ago
    end
    
  when 2..44           then "#{distance_in_minutes} minutes" + ago
  when 45..89          then "about 1 hour" + ago
  when 90..1439        then "about #{(distance_in_minutes.to_f / 60.0).round} hours" + ago
  when 1440..2529      then "about 1 day" + ago
  when 2530..43199     then "about #{(distance_in_minutes.to_f / 1440.0).round} days" + ago
  when 43200..86399    then "about 1 month" + ago
  when 86400..525599   then "about #{(distance_in_minutes.to_f / 43200.0).round} months" + ago
  else
    distance_in_years           = distance_in_minutes / 525600
    minute_offset_for_leap_year = (distance_in_years / 4) * 1440
    remainder                   = ((distance_in_minutes - minute_offset_for_leap_year) % 525600)
    if remainder < 131400
      "about #{distance_in_years} years" + ago
    elsif remainder < 394200
      "over #{distance_in_years} years" + ago
    else
      "almost #{distance_in_years} years" + ago
    end
  end
end

#enqueued_at(resque_enqueue_timestamp) ⇒ Object



132
133
134
135
136
137
# File 'lib/resque/server.rb', line 132

def enqueued_at(resque_enqueue_timestamp)
  return 'Unknown' if resque_enqueue_timestamp.nil?
  now = Time.now
  time = distance_of_time_in_words(now, resque_enqueue_timestamp)
  return time
end

#processes_in(delay_until) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/resque/server.rb', line 124

def processes_in(delay_until)
  return 'Immediately' if delay_until.nil?
  now = Time.now
  time = distance_of_time_in_words(now, delay_until)
  return "Immediately (#{time})" if now > delay_until
  return time
end

#resqueObject



278
279
280
# File 'lib/resque/server.rb', line 278

def resque
  Resque
end

#show(page, layout = true) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/resque/server.rb', line 109

def show(page, layout = true)
  response["Cache-Control"] = "max-age=0, private, must-revalidate"
  begin
    erb page.to_sym, {:layout => layout}, :resque => Resque
  rescue Mongo::ConnectionError, Mongo::ConnectionFailure
    erb :error, {:layout => false}, :error => "Can't connect to MongoDB!"
  end
end

#show_for_polling(page) ⇒ Object



118
119
120
121
122
# File 'lib/resque/server.rb', line 118

def show_for_polling(page)
  content_type "text/html"
  @polling = true
  show(page.to_sym, false).gsub(/\s{1,}/, ' ')
end