jobs: update jobs dashboard to work with GoodJob.

Update the jobs dashboard at /delayed_jobs to work with GoodJob instead
of DelayedJob.
This commit is contained in:
evazion
2022-01-02 21:05:10 -06:00
parent c06bfa64f5
commit f7784d2340
7 changed files with 59 additions and 173 deletions

View File

@@ -4,31 +4,31 @@ class DelayedJobsController < ApplicationController
respond_to :html, :xml, :json, :js
def index
@delayed_jobs = authorize Delayed::Job.order("run_at asc").extending(PaginationExtension).paginate(params[:page], :limit => params[:limit]), policy_class: DelayedJobPolicy
respond_with(@delayed_jobs)
@jobs = authorize GoodJob::ActiveJobJob.order(created_at: :desc).extending(PaginationExtension).paginate(params[:page], limit: params[:limit]), policy_class: GoodJobPolicy
respond_with(@jobs)
end
def cancel
@job = authorize Delayed::Job.find(params[:id]), policy_class: DelayedJobPolicy
@job.fail! unless @job.locked_at?
@job = authorize GoodJob::ActiveJobJob.find(params[:id]), policy_class: GoodJobPolicy
@job.discard_job("Canceled")
respond_with(@job)
end
def retry
@job = authorize Delayed::Job.find(params[:id]), policy_class: DelayedJobPolicy
@job.update(failed_at: nil, attempts: 0) unless @job.locked_at?
@job = authorize GoodJob::ActiveJobJob.find(params[:id]), policy_class: GoodJobPolicy
@job.retry_job
respond_with(@job)
end
def run
@job = authorize Delayed::Job.find(params[:id]), policy_class: DelayedJobPolicy
@job.update(run_at: Time.zone.now) unless @job.locked_at?
@job = authorize GoodJob::ActiveJobJob.find(params[:id]), policy_class: GoodJobPolicy
@job.reschedule_job
respond_with(@job)
end
def destroy
@job = authorize Delayed::Job.find(params[:id]), policy_class: DelayedJobPolicy
@job.destroy unless @job.locked_at?
@job = authorize GoodJob::ActiveJobJob.find(params[:id]), policy_class: GoodJobPolicy
@job.destroy
respond_with(@job)
end
end

View File

@@ -1,131 +0,0 @@
# frozen_string_literal: true
module DelayedJobsHelper
def print_name(job)
case job.name
when "Tag.increment_post_counts"
"<strong>increment post counts</strong>"
when "Tag.decrement_post_counts"
"<strong>decrement post counts</strong>"
when "Post.expire_cache"
"<strong>expire post cache</strong>"
when "Moderator::TagBatchChange"
"<strong>tag batch change</strong>"
when "Class#expire_cache"
"<strong>expire post count cache</strong>"
when "Upload#process!"
"<strong>upload post</strong>"
when "Tag#update_related"
"<strong>update related tags</strong>"
when "TagAlias#process!"
"<strong>alias</strong>"
when "TagImplication#process!"
"<strong>implication</strong>"
when "Class#clear_cache_for"
"<strong>expire tag alias cache</strong>"
when "Tag#update_category_cache"
"<strong>update tag category cache</strong>"
when "Tag#update_category_post_counts"
"<strong>update category post counts</strong>"
when "Class#remove_iqdb"
"<strong>remove from iqdb</strong>"
when "Post#update_iqdb"
"<strong>update iqdb</strong>"
when "Class#convert"
"<strong>convert ugoira</strong>"
when "Class#increment_post_counts"
"<strong>increment post counts</strong>"
when "Class#decrement_post_counts"
"<strong>decrement post counts</strong>"
when "Pool#update_category_pseudo_tags_for_posts"
"<strong>update pool category pseudo tags for posts</strong>"
when "Post.delete_files"
"<strong>delete old files</strong>"
when "BulkRevert#process"
"<strong>bulk revert</strong>"
else
h(job.name)
end
end
def print_handler(job)
case job.name
when "Tag.increment_post_counts", "Tag.decrement_post_counts"
""
when "Post.expire_cache"
""
when "Moderator::TagBatchChange"
h(job.payload_object.antecedent) + " -> " + h(job.payload_object.consequent)
when "Class#expire_cache"
h(job.payload_object.args.flatten.join(" "))
when "Upload#process!"
%{<a href="/uploads/#{job.payload_object.object.id}">record</a>}
when "Tag#update_related"
h(job.payload_object.name)
when "TagAlias#process!"
h(job.payload_object.antecedent_name) + " -> " + h(job.payload_object.consequent_name)
when "TagImplication#process!"
h(job.payload_object.antecedent_name) + " -> " + h(job.payload_object.consequent_name)
when "Class#clear_cache_for"
h(job.payload_object.args.flatten.join(" "))
when "Tag#update_category_cache"
h(job.payload_object.name)
when "Tag#update_category_post_counts"
h(job.payload_object.name)
when "Class#process", "Class#remove_iqdb"
h(job.payload_object.args.flatten.join(" "))
when "Post#update_iqdb"
h(job.payload_object.id)
when "Class#convert"
h(job.payload_object.args[0])
when "Class#increment_post_counts"
h(job.payload_object.args.join(" "))
when "Class#decrement_post_counts"
h(job.payload_object.args.join(" "))
when "Pool#update_category_pseudo_tags_for_posts"
%{<a href="/pools/#{job.payload_object.id}">#{h(job.payload_object.name)}</a>}
when "Post.delete_files"
%{<a href="/posts/#{job.payload_object.args.first}">post ##{job.payload_object.args.first}</a>}
when "BulkRevert#process"
h(job.payload_object.args.join(" "))
end
end
end

View File

@@ -1,6 +1,10 @@
# frozen_string_literal: true
class DelayedJobPolicy < ApplicationPolicy
class GoodJobPolicy < ApplicationPolicy
def index?
true
end
def update?
user.is_admin?
end

View File

@@ -2,42 +2,41 @@
<div id="a-index">
<h1>Delayed Jobs</h1>
<%= table_for @delayed_jobs, class: "striped autofit" do |t| %>
<% t.column :queue %>
<%= table_for @jobs, class: "striped autofit" do |t| %>
<% t.column "Name" do |job| %>
<%= raw print_name(job) %>
<%= job.job_class.titleize.delete_suffix(" Job") %>
<% end %>
<% t.column "Handler", td: {class: "col-expand"} do |job| %>
<%= raw print_handler(job) %>
<% t.column "Details", td: { class: "col-expand" } do |job| %>
<%= job.serialized_params["arguments"] %>
<% end %>
<% t.column :attempts %>
<% t.column "Last error", td: {class: "col-expand"} do |job| %>
<% if job.last_error %>
<%= job.last_error.split(/\n/)[0] %>
<%= job.last_error.split(/\n/)[1..-1].grep(/releases/).join("\n") %>
<% t.column "Error", td: { class: "col-expand" } do |job| %>
<% if job.error.present? %>
<%= job.error %>
<% end %>
<% end %>
<% t.column "Failed at" do |job| %>
<%= time_ago_in_words_tagged(job.failed_at) if job.failed_at %>
<% t.column "Attempts" do |job| %>
<%= job.executions_count %>
<% end %>
<% t.column "Run at" do |job| %>
<%= time_ago_in_words_tagged(job.run_at) %>
<% t.column :status %>
<% t.column "Created" do |job| %>
<%= time_ago_in_words_tagged(job.created_at) %>
<% end %>
<% t.column column: "control" do |job| %>
<% if DelayedJobPolicy.new(CurrentUser.user, job).update? %>
<% if job.locked_at? %>
Running
<% elsif job.failed? %>
<%= link_to "Retry", retry_delayed_job_path(job), method: :put, remote: true %> |
<%= link_to "Delete", delayed_job_path(job), method: :delete, remote: true %>
<% else %>
<%= link_to "Run", run_delayed_job_path(job), method: :put, remote: true %> |
<%= link_to "Cancel", cancel_delayed_job_path(job), method: :put, remote: true %>
<% end %>
<% if GoodJobPolicy.new(CurrentUser.user, job).update? %>
<%= link_to "Run", run_delayed_job_path(job), method: :put, remote: true %> |
<%= link_to "Retry", retry_delayed_job_path(job), method: :put, remote: true %> |
<%= link_to "Cancel", cancel_delayed_job_path(job), method: :put, remote: true %> |
<%= link_to "Delete", delayed_job_path(job), method: :delete, remote: true %>
<% end %>
<% end %>
<% end %>
<%= numbered_paginator(@delayed_jobs) %>
<%= numbered_paginator(@jobs) %>
</div>
</div>

View File

@@ -1,5 +0,0 @@
FactoryBot.define do
factory :delayed_job, class: Delayed::Job do
handler { "" }
end
end

View File

@@ -0,0 +1,15 @@
FactoryBot.define do
factory :good_job, class: GoodJob::ActiveJobJob do
transient do
job { VacuumDatabaseJob.new }
end
id { SecureRandom.uuid }
active_job_id { job.job_id }
queue_name { job.queue_name }
priority { job.priority }
serialized_params do
{ job_class: job.class.name, **job.as_json }
end
end
end

View File

@@ -4,7 +4,7 @@ class DelayedJobsControllerTest < ActionDispatch::IntegrationTest
context "The delayed jobs controller" do
setup do
@user = create(:admin_user)
@job = create(:delayed_job)
@job = create(:good_job)
end
context "index action" do
@@ -16,6 +16,7 @@ class DelayedJobsControllerTest < ActionDispatch::IntegrationTest
context "cancel action" do
should "work" do
GoodJob::ActiveJobJob.any_instance.stubs(:status).returns(:queued)
put_auth cancel_delayed_job_path(@job), @user, xhr: true
assert_response :success
end
@@ -23,6 +24,8 @@ class DelayedJobsControllerTest < ActionDispatch::IntegrationTest
context "retry action" do
should "work" do
@job.head_execution.active_job.class.stubs(:queue_adapter).returns(GoodJob::Adapter.new)
GoodJob::ActiveJobJob.any_instance.stubs(:status).returns(:discarded)
put_auth retry_delayed_job_path(@job), @user, xhr: true
assert_response :success
end
@@ -30,6 +33,7 @@ class DelayedJobsControllerTest < ActionDispatch::IntegrationTest
context "run action" do
should "work" do
GoodJob::ActiveJobJob.any_instance.stubs(:status).returns(:queued)
put_auth run_delayed_job_path(@job), @user, xhr: true
assert_response :success
end