Module: Resque::Scheduler::Server::HelperMethods

Defined in:
lib/resque/scheduler/server.rb

Instance Method Summary collapse

Instance Method Details

#find_job(worker) ⇒ Object


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/resque/scheduler/server.rb', line 138

def find_job(worker)
  worker = worker.downcase
  results = working_jobs_for_worker(worker)

  dels = delayed_jobs_for_worker(worker)
  results += dels.select do |j|
    j['class'].downcase.include?(worker) &&
    j.merge!('where_at' => 'delayed')
  end

  Resque.queues.each do |queue|
    queued = Resque.peek(queue, 0, Resque.size(queue))
    queued = [queued] unless queued.is_a?(Array)
    results += queued.select do |j|
      j['class'].downcase.include?(worker) &&
      j.merge!('queue' => queue, 'where_at' => 'queued')
    end
  end

  results
end

#format_time(t) ⇒ Object


128
129
130
# File 'lib/resque/scheduler/server.rb', line 128

def format_time(t)
  t.strftime('%Y-%m-%d %H:%M:%S %z')
end

#queue_from_class_name(class_name) ⇒ Object


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

def queue_from_class_name(class_name)
  Resque.queue_from_class(
    Resque::Scheduler::Util.constantize(class_name)
  )
end

#rails_env(name) ⇒ Object


202
203
204
# File 'lib/resque/scheduler/server.rb', line 202

def rails_env(name)
  Resque.schedule[name]['rails_env']
end

#schedule_class(config) ⇒ Object


183
184
185
186
187
188
189
# File 'lib/resque/scheduler/server.rb', line 183

def schedule_class(config)
  if config['class'].nil? && !config['custom_job_class'].nil?
    config['custom_job_class']
  else
    config['class']
  end
end

#schedule_interval(config) ⇒ Object


160
161
162
163
164
165
166
167
168
# File 'lib/resque/scheduler/server.rb', line 160

def schedule_interval(config)
  if config['every']
    schedule_interval_every(config['every'])
  elsif config['cron']
    'cron: ' + config['cron'].to_s
  else
    'Not currently scheduled'
  end
end

#schedule_interval_every(every) ⇒ Object


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/resque/scheduler/server.rb', line 170

def schedule_interval_every(every)
  every = [*every]
  s = 'every: ' << every.first

  return s unless every.length > 1

  s << ' ('
  meta = every.last.map do |key, value|
    "#{key.to_s.gsub(/_/, ' ')} #{value}"
  end
  s << meta.join(', ') << ')'
end

#scheduled_in_this_env?(name) ⇒ Boolean

Returns:

  • (Boolean)

197
198
199
200
# File 'lib/resque/scheduler/server.rb', line 197

def scheduled_in_this_env?(name)
  return true if Resque.schedule[name]['rails_env'].nil?
  rails_env(name).split(/[\s,]+/).include?(Resque::Scheduler.env)
end

#scheduler_template(name) ⇒ Object


191
192
193
194
195
# File 'lib/resque/scheduler/server.rb', line 191

def scheduler_template(name)
  File.read(
    File.expand_path("../server/views/#{name}.erb", __FILE__)
  )
end

#scheduler_view(filename, options = {}, locals = {}) ⇒ Object


206
207
208
209
# File 'lib/resque/scheduler/server.rb', line 206

def scheduler_view(filename, options = {}, locals = {})
  source = File.read(File.join(VIEW_PATH, "#{filename}.erb"))
  erb source, options, locals
end