From eaeec8dd706a58497f96caa20d271cae59d3f095 Mon Sep 17 00:00:00 2001 From: Paul Sadauskas Date: Wed, 12 Jul 2023 16:08:38 -0600 Subject: [PATCH 1/2] Added BaseExecution#job_display_name --- README.md | 20 ++++++++++++++++++++ app/models/good_job/base_execution.rb | 8 ++++++++ app/views/good_job/batches/_jobs.erb | 2 +- app/views/good_job/jobs/_table.erb | 2 +- app/views/good_job/jobs/show.html.erb | 2 +- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dfb679d8a..a3cca32d1 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,26 @@ end The Dashboard can be set to automatically refresh by checking "Live Poll" in the Dashboard header, or by setting `?poll=10` with the interval in seconds (default 30 seconds). +#### Customizing Job Display Name + +The Dashboard displays jobs by default using the name of the Job class. If you want to customize this name, for example you have a generic job and want to include some part of the job arguments within the name, you can override `GoodJob::BaseExecution#job_display_name`: + +```ruby +module GoodJobCustomDisplayName + def job_display_name + super + serialized_params.dig("arguments", 0, "my_specific_name") + end +end + +# In a Rails initializer: +Rails.application.configure do + # :after_initialize, so GoodJob has had a chance to load its models + ActiveSupport.on_load(:after_initialize) do + GoodJob::BaseExecution.prepend(GoodJobCustomDisplayName) + end +end +``` + ### Concurrency controls GoodJob can extend ActiveJob to provide limits on concurrently running jobs, either at time of _enqueue_ or at _perform_. Limiting concurrency can help prevent duplicate, double or unnecessary jobs from being enqueued, or race conditions when performing, for example when interacting with 3rd-party APIs. diff --git a/app/models/good_job/base_execution.rb b/app/models/good_job/base_execution.rb index 537895e4c..6a8ff62e9 100644 --- a/app/models/good_job/base_execution.rb +++ b/app/models/good_job/base_execution.rb @@ -54,6 +54,14 @@ def job_class discrete? ? attributes['job_class'] : serialized_params['job_class'] end + # Used when displaying this job in the GoodJob dashboard. This will only ever + # be used when displaying jobs, so is available to monkeypatch if needed + # without interfering with the operation of GoodJob itself. + # @return [String] + def job_display_name + job_class + end + def discrete? self.class.discrete_support? && is_discrete? end diff --git a/app/views/good_job/batches/_jobs.erb b/app/views/good_job/batches/_jobs.erb index 9455b4577..d09dceb65 100644 --- a/app/views/good_job/batches/_jobs.erb +++ b/app/views/good_job/batches/_jobs.erb @@ -23,7 +23,7 @@
<%= tag.code link_to(job.id, job_path(job), class: "small text-muted text-decoration-none") %> - <%= tag.h5 tag.code(link_to(job.job_class, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %> + <%= tag.h5 tag.code(link_to(job.job_display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %>
<%=t "good_job.models.job.queue" %>
diff --git a/app/views/good_job/jobs/_table.erb b/app/views/good_job/jobs/_table.erb index 38c77007b..d0ae79165 100644 --- a/app/views/good_job/jobs/_table.erb +++ b/app/views/good_job/jobs/_table.erb @@ -64,7 +64,7 @@ <%= check_box_tag 'job_ids[]', job.id, false, id: dom_id(job, :checkbox), data: { "checkbox-toggle-each": "job_ids" } %>
<%= tag.code link_to(job.id, job_path(job), class: "small text-muted text-decoration-none") %> - <%= tag.h5 tag.code(link_to(job.job_class, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %> + <%= tag.h5 tag.code(link_to(job.job_display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %> <% if job.error %>
<%=t "good_job.shared.error" %>: diff --git a/app/views/good_job/jobs/show.html.erb b/app/views/good_job/jobs/show.html.erb index 2e59f45db..9e2e58f00 100644 --- a/app/views/good_job/jobs/show.html.erb +++ b/app/views/good_job/jobs/show.html.erb @@ -13,7 +13,7 @@
-

<%= tag.code @job.job_class %>

+

<%= tag.code @job.job_display_name %>

<%= t "good_job.models.job.queue" %>
From fcfef3bceaa9375ffd83f83663465a6319c69625 Mon Sep 17 00:00:00 2001 From: "Ben Sheldon [he/him]" Date: Thu, 13 Jul 2023 11:33:04 -0700 Subject: [PATCH 2/2] Narrow to `GoodJob::Job#display_name` and remove documentation from readme --- README.md | 20 -------------------- app/models/good_job/base_execution.rb | 8 -------- app/models/good_job/job.rb | 6 ++++++ app/views/good_job/batches/_jobs.erb | 2 +- app/views/good_job/jobs/_table.erb | 2 +- app/views/good_job/jobs/show.html.erb | 2 +- 6 files changed, 9 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index a3cca32d1..dfb679d8a 100644 --- a/README.md +++ b/README.md @@ -413,26 +413,6 @@ end The Dashboard can be set to automatically refresh by checking "Live Poll" in the Dashboard header, or by setting `?poll=10` with the interval in seconds (default 30 seconds). -#### Customizing Job Display Name - -The Dashboard displays jobs by default using the name of the Job class. If you want to customize this name, for example you have a generic job and want to include some part of the job arguments within the name, you can override `GoodJob::BaseExecution#job_display_name`: - -```ruby -module GoodJobCustomDisplayName - def job_display_name - super + serialized_params.dig("arguments", 0, "my_specific_name") - end -end - -# In a Rails initializer: -Rails.application.configure do - # :after_initialize, so GoodJob has had a chance to load its models - ActiveSupport.on_load(:after_initialize) do - GoodJob::BaseExecution.prepend(GoodJobCustomDisplayName) - end -end -``` - ### Concurrency controls GoodJob can extend ActiveJob to provide limits on concurrently running jobs, either at time of _enqueue_ or at _perform_. Limiting concurrency can help prevent duplicate, double or unnecessary jobs from being enqueued, or race conditions when performing, for example when interacting with 3rd-party APIs. diff --git a/app/models/good_job/base_execution.rb b/app/models/good_job/base_execution.rb index 6a8ff62e9..537895e4c 100644 --- a/app/models/good_job/base_execution.rb +++ b/app/models/good_job/base_execution.rb @@ -54,14 +54,6 @@ def job_class discrete? ? attributes['job_class'] : serialized_params['job_class'] end - # Used when displaying this job in the GoodJob dashboard. This will only ever - # be used when displaying jobs, so is available to monkeypatch if needed - # without interfering with the operation of GoodJob itself. - # @return [String] - def job_display_name - job_class - end - def discrete? self.class.discrete_support? && is_discrete? end diff --git a/app/models/good_job/job.rb b/app/models/good_job/job.rb index fadf6eb37..57eabbd2d 100644 --- a/app/models/good_job/job.rb +++ b/app/models/good_job/job.rb @@ -149,6 +149,12 @@ def display_serialized_params }) end + # Used when displaying this job in the GoodJob dashboard. + # @return [String] + def display_name + job_class + end + # Tests whether the job is being executed right now. # @return [Boolean] def running? diff --git a/app/views/good_job/batches/_jobs.erb b/app/views/good_job/batches/_jobs.erb index d09dceb65..fa40b14d5 100644 --- a/app/views/good_job/batches/_jobs.erb +++ b/app/views/good_job/batches/_jobs.erb @@ -23,7 +23,7 @@
<%= tag.code link_to(job.id, job_path(job), class: "small text-muted text-decoration-none") %> - <%= tag.h5 tag.code(link_to(job.job_display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %> + <%= tag.h5 tag.code(link_to(job.display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %>
<%=t "good_job.models.job.queue" %>
diff --git a/app/views/good_job/jobs/_table.erb b/app/views/good_job/jobs/_table.erb index d0ae79165..cc486a7b3 100644 --- a/app/views/good_job/jobs/_table.erb +++ b/app/views/good_job/jobs/_table.erb @@ -64,7 +64,7 @@ <%= check_box_tag 'job_ids[]', job.id, false, id: dom_id(job, :checkbox), data: { "checkbox-toggle-each": "job_ids" } %>
<%= tag.code link_to(job.id, job_path(job), class: "small text-muted text-decoration-none") %> - <%= tag.h5 tag.code(link_to(job.job_display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %> + <%= tag.h5 tag.code(link_to(job.display_name, job_path(job), class: "text-reset text-decoration-none")), class: "text-reset mb-0" %> <% if job.error %>
<%=t "good_job.shared.error" %>: diff --git a/app/views/good_job/jobs/show.html.erb b/app/views/good_job/jobs/show.html.erb index 9e2e58f00..7a5dee116 100644 --- a/app/views/good_job/jobs/show.html.erb +++ b/app/views/good_job/jobs/show.html.erb @@ -13,7 +13,7 @@
-

<%= tag.code @job.job_display_name %>

+

<%= tag.code @job.display_name %>

<%= t "good_job.models.job.queue" %>