Skip to content

Commit

Permalink
Merge f00c211 into b215aa2
Browse files Browse the repository at this point in the history
  • Loading branch information
giriss committed Jul 17, 2018
2 parents b215aa2 + f00c211 commit 076c2e2
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 27 deletions.
71 changes: 56 additions & 15 deletions lib/sidekiq/cron/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ def enque! time = Time.now.utc
nil
end

if klass_const
if defined?(ActiveJob::Base) && klass_const < ActiveJob::Base
enqueue_active_job(klass_const)
else
enqueue_sidekiq_worker(klass_const)
end
else
if @active_job
Sidekiq::Client.push(active_job_message)
jid =
if klass_const
if defined?(ActiveJob::Base) && klass_const < ActiveJob::Base
jid = enqueue_active_job(klass_const).try :provider_job_id
else
jid = enqueue_sidekiq_worker(klass_const)
end
else
Sidekiq::Client.push(sidekiq_worker_message)
if @active_job
jid = Sidekiq::Client.push(active_job_message)
else
jid = Sidekiq::Client.push(sidekiq_worker_message)
end
end
end

save_last_enqueue_time
add_jid_history jid
logger.debug { "enqueued #{@name}: #{@message}" }
end

Expand All @@ -82,14 +84,10 @@ def is_active_job?

def enqueue_active_job(klass_const)
klass_const.set(queue: @queue).perform_later(*@args)

true
end

def enqueue_sidekiq_worker(klass_const)
klass_const.set(queue: queue_name_with_prefix).perform_async(*@args)

true
end

# siodekiq worker message
Expand Down Expand Up @@ -349,6 +347,12 @@ def disabled?
!enabled?
end

def pretty_message
JSON.pretty_generate Sidekiq.load_json(message)
rescue JSON::ParserError
message
end

def status_from_redis
out = "enabled"
if fetch_missing_args
Expand All @@ -370,6 +374,18 @@ def last_enqueue_time_from_redis
out
end

def jid_history_from_redis
out =
Sidekiq.redis do |conn|
conn.lrange(jid_history_key, 0, -1) rescue nil
end

# returns nil if out nil
out && out.map do |jid_history_raw|
Sidekiq.load_json jid_history_raw
end
end

#export job data to hash
def to_hash
{
Expand Down Expand Up @@ -456,6 +472,20 @@ def save_last_enqueue_time
end
end

def add_jid_history(jid)
jid_history = {
jid: jid,
enqueued: @last_enqueue_time
}
@history_size ||= (Sidekiq.options[:cron_history_size] || 10).to_i - 1
Sidekiq.redis do |conn|
conn.lpush jid_history_key,
Sidekiq.dump_json(jid_history)
# keep only last 10 entries in a fifo manner
conn.ltrim jid_history_key, 0, @history_size
end
end

# remove job from cron jobs by name
# input:
# first arg: name (string) - name of job (must be same - case sensitive)
Expand All @@ -467,6 +497,9 @@ def destroy
#delete runned timestamps
conn.del job_enqueued_key

# delete jid_history
conn.del jid_history_key

#delete main job
conn.del redis_key
end
Expand Down Expand Up @@ -579,12 +612,20 @@ def self.job_enqueued_key name
"cron_job:#{name}:enqueued"
end

def self.jid_history_key name
"cron_job:#{name}:jid_history"
end

# Redis key for storing one cron job run times
# (when poller added job to queue)
def job_enqueued_key
self.class.job_enqueued_key @name
end

def jid_history_key
self.class.jid_history_key @name
end

# Give Hash
# returns array for using it for redis.hmset
def hash_to_redis hash
Expand Down
1 change: 1 addition & 0 deletions lib/sidekiq/cron/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ en:
'Last enque': Last enqueued
disabled: disabled
enabled: enabled
NoHistoryWereFound: No history were found
4 changes: 3 additions & 1 deletion lib/sidekiq/cron/views/cron.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
<tr>
<td style="<%= style %>"><%= t job.status %></td>
<td style="<%= style %>">
<b><%= job.name %></b>
<a href="<%= root_path %>cron/<%= CGI.escape(job.name).gsub('+', '%20') %>">
<b><%= job.name %></b>
</a>
<hr style="margin:3px;border:0;">
<small>
<% if job.message and job.message.to_s.size > 100 %>
Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq/cron/views/cron.slim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ header.row
tr
td[style="#{style}"]= job.status
td[style="#{style}"]
b job.name
a href="#{root_path}cron/#{CGI.escape(job.name).gsub('+', '%20')}"
b = job.name
hr style="margin:3px;border:0;"
small
- if job.message and job.message.to_s.size > 100
Expand Down
84 changes: 84 additions & 0 deletions lib/sidekiq/cron/views/cron_show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<header class="row">
<div class="span col-sm-5 pull-left">
<h3>
<%= "#{t('Cron')} #{t('Job')}" %>
<small><%= @job.name %></small>
</h3>
</div>
<div class="span col-sm-7 pull-right" style="margin-top: 20px; margin-bottom: 10px;">
<% cron_job_path = "#{root_path}cron/#{CGI.escape(@job.name).gsub('+', '%20')}" %>
<form action="<%= cron_job_path %>/enque?redirect=<%= cron_job_path %>" class="pull-right" method="post">
<%= csrf_tag if respond_to?(:csrf_tag) %>
<input class="btn btn-small pull-left" name="enque" type="submit" value="<%= t('EnqueueNow') %>" />
</form>
<% if @job.status == 'enabled' %>
<form action="<%= cron_job_path %>/disable?redirect=<%= cron_job_path %>" class="pull-right" method="post">
<%= csrf_tag if respond_to?(:csrf_tag) %>
<input class="btn btn-small pull-left" name="disable" type="submit" value="<%= t('Disable') %>" />
</form>
<% else %>
<form action="<%= cron_job_path %>/enable?redirect=<%= cron_job_path %>" class="pull-right" method="post">
<%= csrf_tag if respond_to?(:csrf_tag) %>
<input class="btn btn-small pull-left" name="enable" type="submit" value="<%= t('Enable') %>" />
</form>
<form action="<%= cron_job_path %>/delete" class="pull-right" method="post">
<%= csrf_tag if respond_to?(:csrf_tag) %>
<input class="btn btn-danger btn-small" data-confirm="<%= t('AreYouSureDeleteCronJob', :job => @job.name) %>" name="delete" type="submit" value="<%= t('Delete') %>" />
</form>
<% end %>
</div>
</header>

<table class="table table-bordered table-striped">
<tbody>
<tr>
<th><%= t 'Status' %></th>
<td><%= @job.status %></td>
</tr>
<tr>
<th><%= t 'Name' %></th>
<td><%= @job.name %></td>
</tr>
<tr>
<th><%= t 'Message' %></th>
<td><pre><%= @job.pretty_message %></pre></td>
</tr>
<tr>
<th><%= t 'Cron' %></th>
<td><%= @job.cron.gsub(" ", "&nbsp;") %></td>
</tr>
<tr>
<th><%= t 'Last enque' %></th>
<td><%= @job.last_enqueue_time ? relative_time(@job.last_enqueue_time) : "-" %></td>
</tr>
</tbody>
</table>

<header class="row">
<div class="col-sm-12">
<h4>
<%= t 'History' %>
</h4>
</div>
</header>

<% if @job.jid_history_from_redis.size > 0 %>
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th><%= t 'Enqueued' %></th>
<th><%= t 'JID' %></th>
</tr>
</thead>
<tbody>
<% @job.jid_history_from_redis.each do |jid_history| %>
<tr>
<td><%= jid_history['enqueued'] %></td>
<td><%= jid_history['jid'] %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div class='alert alert-success'><%= t 'NoHistoryWereFound' %></div>
<% end %>
58 changes: 58 additions & 0 deletions lib/sidekiq/cron/views/cron_show.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
header.row
.span.col-sm-5.pull-left
h3
= "#{t('Cron')} #{t('Job')}"
small= @job.name
.span.col-sm-7.pull-right style=("margin-top: 20px; margin-bottom: 10px;")
- cron_job_path = "#{root_path}cron/#{CGI.escape(@job.name).gsub('+', '%20')}"
form.pull-right action="#{cron_job_path}/enque?redirect=#{cron_job_path}" method="post"
= csrf_tag if respond_to?(:csrf_tag)
input.btn.btn-small.pull-left name="enque" type="submit" value="#{t('EnqueueNow')}"
- if @job.status == 'enabled'
form.pull-right action="#{cron_job_path}/disable?redirect=#{cron_job_path}" method="post"
= csrf_tag if respond_to?(:csrf_tag)
input.btn.btn-small.pull-left name="disable" type="submit" value="#{t('Disable')}"
- else
form.pull-right action="#{cron_job_path}/enable?redirect=#{cron_job_path}" method="post"
= csrf_tag if respond_to?(:csrf_tag)
input.btn.btn-small.pull-left name="enable" type="submit" value="#{t('Enable')}"
form.pull-right action="#{cron_job_path}/delete" method="post"
= csrf_tag if respond_to?(:csrf_tag)
input.btn.btn-danger.btn-small data-confirm="#{t('AreYouSureDeleteCronJob' job =@job.name)}" name="delete" type="submit" value="#{t('Delete')}" /

table.table.table-bordered.table-striped
tbody
tr
th= t 'Status'
td= @job.status
tr
th= t 'Name'
td= @job.name
tr
th= t 'Message'
td
pre= @job.pretty_message
tr
th= t 'Cron'
td= @job.cron.gsub(" ", "&nbsp;")
tr
th= t 'Last enque'
td= @job.last_enqueue_time ? relative_time(@job.last_enqueue_time) : "-"

header.row
.col-sm-12
h4= t 'History'

- if @job.jid_history_from_redis.size > 0
table.table.table-hover.table-bordered.table-striped
thead
tr
th= t 'Enqueued'
th= t 'JID'
tbody
- @job.jid_history_from_redis.each do |jid_history|
tr
td= jid_history['enqueued']
td= jid_history['jid']
- else
.alert.alert-success= t 'NoHistoryWereFound'
23 changes: 20 additions & 3 deletions lib/sidekiq/cron/web_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,31 @@ def self.registered(app)
end
end

# display job detail + jid history
app.get '/cron/:name' do
view_path = File.join(File.expand_path("..", __FILE__), "views")

@job = Sidekiq::Cron::Job.find(route_params[:name])
if @job.present?
#if Slim renderer exists and sidekiq has layout.slim in views
if defined?(Slim) && File.exists?(File.join(settings.views,"layout.slim"))
render(:slim, File.read(File.join(view_path, "cron_show.slim")))
else
render(:erb, File.read(File.join(view_path, "cron_show.erb")))
end
else
redirect "#{root_path}cron"
end
end

#enque cron job
app.post '/cron/:name/enque' do
if route_params[:name] === '__all__'
Sidekiq::Cron::Job.all.each(&:enque!)
elsif job = Sidekiq::Cron::Job.find(route_params[:name])
job.enque!
end
redirect "#{root_path}cron"
redirect params['redirect'] || "#{root_path}cron"
end

#delete schedule
Expand All @@ -47,7 +64,7 @@ def self.registered(app)
elsif job = Sidekiq::Cron::Job.find(route_params[:name])
job.enable!
end
redirect "#{root_path}cron"
redirect params['redirect'] || "#{root_path}cron"
end

#disable job
Expand All @@ -57,7 +74,7 @@ def self.registered(app)
elsif job = Sidekiq::Cron::Job.find(route_params[:name])
job.disable!
end
redirect "#{root_path}cron"
redirect params['redirect'] || "#{root_path}cron"
end

end
Expand Down
21 changes: 16 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def perform args = {}

module ActiveJob
class Base
attr_accessor *%i[job_class provider_job_id queue_name arguments]

def initialize
yield self if block_given?
self.provider_job_id ||= SecureRandom.hex(12)
end

def self.queue_name_prefix
@queue_name_prefix
end
Expand All @@ -80,12 +87,16 @@ def self.set(options)
self
end

def try(method, *args, &block)
send method, *args, &block if respond_to? method
end

def self.perform_later(*args)
{
"job_class" => self.class.name,
"queue_name" => @queue,
"args" => [*args],
}
new do |instance|
instance.job_class = self.class.name
instance.queue_name = @queue
instance.arguments = [*args]
end
end
end
end
Expand Down

0 comments on commit 076c2e2

Please sign in to comment.