-
-
Save eliotsykes/a2f9c5fe5d8a5aa8c0b78c912d769ddb to your computer and use it in GitHub Desktop.
Active Admin for DelayedJob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Place this file in app/admin/jobs.rb | |
ActiveAdmin.register Delayed::Job, as: 'DelayedJob' do | |
# menu parent: 'Some Existing Menu' # optional | |
permit_params :priority, :queue, :run_at | |
actions :index, :show, :edit, :update, :destroy | |
config.batch_actions = true | |
index do | |
selectable_column | |
id_column | |
column :name | |
column :status do |job| | |
if job.failed? | |
status_tag 'Failed', class: :error | |
span job.last_error.truncate(50) | |
elsif job.locked_at | |
status_tag 'Running', class: :warning | |
span "for #{time_ago_in_words(job.locked_at)} @ #{job.locked_by}" | |
elsif job.run_at.future? | |
status_tag 'Scheduled', class: :ok | |
span "for #{time_ago_in_words(job.run_at)} from now" | |
else | |
status_tag 'Pending', class: :info | |
span "for #{time_ago_in_words(job.run_at)}" | |
end | |
end | |
column :attempts | |
column :failed_at | |
column :run_at | |
column :created_at | |
column :queue | |
column :priority | |
column :locked_by | |
column :locked_at | |
actions | |
end | |
show do |job| | |
attributes_table do | |
row :id | |
row :priority | |
row :attempts | |
row :handler | |
row :last_error do | |
div { pre { job.last_error } } | |
end | |
row :run_at | |
row :locked_at | |
row :locked_by | |
row :failed_at | |
row :queue | |
row :created_at | |
row :updated_at | |
end | |
end | |
form do |f| | |
f.inputs 'Scheduling' do | |
f.input :priority | |
f.input :queue | |
f.input :run_at, as: :date_time_picker | |
end | |
f.inputs 'Details' do | |
f.input :id, input_html: { disabled: true } | |
f.input :created_at, as: :date_time_picker, input_html: { disabled: true } | |
f.input :updated_at, as: :date_time_picker, input_html: { disabled: true } | |
f.input :handler, input_html: { disabled: true } | |
end | |
f.inputs 'Diagnostics' do | |
f.input :attempts, input_html: { disabled: true } | |
f.input :failed_at, as: :date_time_picker, input_html: { disabled: true } | |
f.input :last_error, input_html: { disabled: true } | |
f.input :locked_at, as: :date_time_picker, input_html: { disabled: true } | |
f.input :locked_by, input_html: { disabled: true } | |
end | |
f.actions | |
end | |
action_item :schedule_now, only: [:show, :edit] do | |
link_to 'Schedule now', schedule_now_admin_delayed_job_path(resource), data: { method: :post }, | |
title: 'Schedule a future job to run now on a worker.' | |
end | |
action_item :reset, only: [:show, :edit] do | |
link_to 'Reset Job', reset_admin_delayed_job_path(resource), data: { method: :post }, | |
title: 'Reset the job state so it will be retried by a worker ASAP.' | |
end | |
member_action :schedule_now, method: :post do | |
resource.update! run_at: Time.current | |
redirect_to({ action: :index }, { notice: 'Job scheduled to now' }) | |
end | |
member_action :reset, method: :post do | |
resource.update! locked_at: nil, locked_by: nil, attempts: 0, last_error: nil, failed_at: nil, run_at: Time.current | |
redirect_to({ action: :index }, { notice: 'Job was reset' }) | |
end | |
batch_action 'Reset', confirm: 'Are you sure you want to reset these delayed jobs?' do |ids| | |
Delayed::Job.where(id: ids) | |
.update_all(locked_at: nil, locked_by: nil, attempts: 0, last_error: nil, failed_at: nil, run_at: Time.current) | |
redirect_to({ action: :index }, { notice: 'Selected jobs were reset' }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment