From f7784d2340b5213d04ca4bb5aaea4909b7f5e701 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 2 Jan 2022 21:05:10 -0600 Subject: [PATCH] jobs: update jobs dashboard to work with GoodJob. Update the jobs dashboard at /delayed_jobs to work with GoodJob instead of DelayedJob. --- app/controllers/delayed_jobs_controller.rb | 20 +-- app/helpers/delayed_jobs_helper.rb | 131 ------------------ ...layed_job_policy.rb => good_job_policy.rb} | 6 +- app/views/delayed_jobs/index.html.erb | 49 ++++--- test/factories/delayed_job.rb | 5 - test/factories/good_job.rb | 15 ++ .../delayed_jobs_controller_test.rb | 6 +- 7 files changed, 59 insertions(+), 173 deletions(-) delete mode 100644 app/helpers/delayed_jobs_helper.rb rename app/policies/{delayed_job_policy.rb => good_job_policy.rb} (75%) delete mode 100644 test/factories/delayed_job.rb create mode 100644 test/factories/good_job.rb diff --git a/app/controllers/delayed_jobs_controller.rb b/app/controllers/delayed_jobs_controller.rb index d011d6476..3b376e918 100644 --- a/app/controllers/delayed_jobs_controller.rb +++ b/app/controllers/delayed_jobs_controller.rb @@ -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 diff --git a/app/helpers/delayed_jobs_helper.rb b/app/helpers/delayed_jobs_helper.rb deleted file mode 100644 index 316f0f8ba..000000000 --- a/app/helpers/delayed_jobs_helper.rb +++ /dev/null @@ -1,131 +0,0 @@ -# frozen_string_literal: true - -module DelayedJobsHelper - def print_name(job) - case job.name - when "Tag.increment_post_counts" - "increment post counts" - - when "Tag.decrement_post_counts" - "decrement post counts" - - when "Post.expire_cache" - "expire post cache" - - when "Moderator::TagBatchChange" - "tag batch change" - - when "Class#expire_cache" - "expire post count cache" - - when "Upload#process!" - "upload post" - - when "Tag#update_related" - "update related tags" - - when "TagAlias#process!" - "alias" - - when "TagImplication#process!" - "implication" - - when "Class#clear_cache_for" - "expire tag alias cache" - - when "Tag#update_category_cache" - "update tag category cache" - - when "Tag#update_category_post_counts" - "update category post counts" - - when "Class#remove_iqdb" - "remove from iqdb" - - when "Post#update_iqdb" - "update iqdb" - - when "Class#convert" - "convert ugoira" - - when "Class#increment_post_counts" - "increment post counts" - - when "Class#decrement_post_counts" - "decrement post counts" - - when "Pool#update_category_pseudo_tags_for_posts" - "update pool category pseudo tags for posts" - - when "Post.delete_files" - "delete old files" - - when "BulkRevert#process" - "bulk revert" - - 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!" - %{record} - - 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" - %{#{h(job.payload_object.name)}} - - when "Post.delete_files" - %{post ##{job.payload_object.args.first}} - - when "BulkRevert#process" - h(job.payload_object.args.join(" ")) - end - end -end diff --git a/app/policies/delayed_job_policy.rb b/app/policies/good_job_policy.rb similarity index 75% rename from app/policies/delayed_job_policy.rb rename to app/policies/good_job_policy.rb index bcfdd6e0b..bcc822b0f 100644 --- a/app/policies/delayed_job_policy.rb +++ b/app/policies/good_job_policy.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true -class DelayedJobPolicy < ApplicationPolicy +class GoodJobPolicy < ApplicationPolicy + def index? + true + end + def update? user.is_admin? end diff --git a/app/views/delayed_jobs/index.html.erb b/app/views/delayed_jobs/index.html.erb index a6c2ef44d..5a07b6990 100644 --- a/app/views/delayed_jobs/index.html.erb +++ b/app/views/delayed_jobs/index.html.erb @@ -2,42 +2,41 @@

Delayed Jobs

- <%= 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) %>
diff --git a/test/factories/delayed_job.rb b/test/factories/delayed_job.rb deleted file mode 100644 index 4a617fec0..000000000 --- a/test/factories/delayed_job.rb +++ /dev/null @@ -1,5 +0,0 @@ -FactoryBot.define do - factory :delayed_job, class: Delayed::Job do - handler { "" } - end -end diff --git a/test/factories/good_job.rb b/test/factories/good_job.rb new file mode 100644 index 000000000..f85dcefe5 --- /dev/null +++ b/test/factories/good_job.rb @@ -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 diff --git a/test/functional/delayed_jobs_controller_test.rb b/test/functional/delayed_jobs_controller_test.rb index beb60b69a..ad5508888 100644 --- a/test/functional/delayed_jobs_controller_test.rb +++ b/test/functional/delayed_jobs_controller_test.rb @@ -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