Skip to content

Commit

Permalink
Add next time execution at recurring jobs view
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Dotta committed Jul 19, 2016
1 parent 5e0620f commit 8865c82
Show file tree
Hide file tree
Showing 10 changed files with 565 additions and 269 deletions.
54 changes: 54 additions & 0 deletions lib/sidekiq-scheduler/job_presenter.rb
@@ -0,0 +1,54 @@
require 'sidekiq/web_helpers'

module SidekiqScheduler
class JobPresenter
attr_reader :name

include Sidekiq::WebHelpers

def initialize(name, attributes)
@name = name
@attributes = attributes
end

# Returns the next time execution for the job
#
# @return [String] with the job's next time
def next_time
execution_time = Sidekiq.redis { |r| r.hget(Sidekiq::Scheduler.next_times_key, name) }

relative_time(Time.parse(execution_time)) if execution_time
end

# Returns the interval for the job
#
# @return [String] with the job's interval
def interval
@attributes.fetch('cron', @attributes['every'])
end

# Returns the queue of the job
#
# @return [String] with the job's queue
def queue
@attributes.fetch('queue', 'default')
end

# Delegates the :[] method to the attributes' hash
#
# @return [String] with the value for that key
def [](key)
@attributes[key]
end

# Builds the presenter instances for the schedule hash
#
# @param schedule_hash [Hash] with the redis schedule
# @return [Array<JobPresenter>] an array with the instances of presenters
def self.build_collection(schedule_hash)
Hash(schedule_hash).map do |name, job_spec|
new(name, job_spec)
end
end
end
end
4 changes: 3 additions & 1 deletion lib/sidekiq-scheduler/web.rb
@@ -1,3 +1,5 @@
require_relative 'job_presenter'

module SidekiqScheduler
# Hook into *Sidekiq::Web* Sinatra app which adds a new '/recurring-jobs' page

Expand All @@ -6,7 +8,7 @@ module Web

def self.registered(app)
app.get '/recurring-jobs' do
@schedule = (Sidekiq.schedule! || [])
@presented_jobs = JobPresenter.build_collection(Sidekiq.schedule!)

erb File.read(File.join(VIEW_PATH, 'recurring_jobs.erb'))
end
Expand Down

0 comments on commit 8865c82

Please sign in to comment.