Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GoodJob::Job#display_name to allow customizing dashboard job display #1008

Merged
merged 2 commits into from
Jul 13, 2023

Conversation

paul
Copy link
Contributor

@paul paul commented Jul 12, 2023

Fixes #956

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:

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

@bensheldon
Copy link
Owner

@paul thank you for opening this! I made a few tweaks:

  • I narrowed it so the method is only on GoodJob::Job.display_name. I expect BaseExecution to become deprecated in the next major release, and removed in the following one, whereupon Job and Execution models will point at separate tables.
  • I removed the documentation from the Readme. I don't want to imply that this is "officially supported", even if I want to make it possible.

For monkeypatching, I think the best option is this:

# config/initializers/good_job.rb

module GoodJobCustomDisplayName
  def display_name
    super + serialized_params.dig("arguments", 0, "my_specific_name")
  end
end

Rails.application.autoloaders.each do |zeitwerk|
  zeitwerk.on_load("GoodJob::Job") do
    GoodJob::Job.prepend(GoodJobCustomDisplayName)
  end
end

@bensheldon bensheldon added the refactor Code changes that do not introduce new features label Jul 13, 2023
@bensheldon bensheldon changed the title Added BaseExecution#job_display_name to customize dashboard job display Add GoodJob::Job#display_name to allow customizing dashboard job display Jul 13, 2023
@bensheldon bensheldon merged commit 3ce19f2 into bensheldon:main Jul 13, 2023
19 checks passed
@bensheldon
Copy link
Owner

This has been released in https://github.com/bensheldon/good_job/releases/tag/v3.16.2

Let know how it works for you and I'm open to additional tweaks/improvements 👍🏻

@paul
Copy link
Contributor Author

paul commented Jul 14, 2023

@bensheldon Thanks for the super quick turnaround! We're running it in production now, and its looking great!

image

@mroach
Copy link

mroach commented Jul 15, 2023

I wanted to chime in and thank you guys for this! We also use the command pattern and had the same issue with the dashboard, so this is a great enhancement for us as well.

I took a pretty similar approach, except inverting control to let the job classes set their own display names.

GoodJob::Job.prepend(Module.new do
  def display_name
    klass = job_class.constantize

    return super unless klass.respond_to?(:good_job_display_name)

    super + "[" + klass.good_job_display_name(self).to_s + "]"
  end
end)
class RunCommandJob < ApplicationJob
  class << self
    def good_job_display_name(job)
      job.serialized_params.dig("arguments", 0, "command_class", "value")
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Code changes that do not introduce new features
Projects
Development

Successfully merging this pull request may close these issues.

Support for customized job display name
3 participants