solid_queue
solid_queue copied to clipboard
Customize the parent class of SolidQueue::RecurringJob
Introduction
Allow specifying recurring tasks just with a "command" I find this feature very interesting.
However, there is one barrier to its use.
That is, the parent of SolidQueue::RecurringJob is fixed at ActiveJob::Base.
I have defined error handling in ApplicationJob as described below. https://github.com/rails/solid_queue/blob/67b964d5e9d1120e0f198dc1ef538a0aff966133/README.md#error-reporting-on-jobs
This error handling does not work when using βcommandβ.
Proposal
How about allowing customization of the SolidQueue::RecurringJob parent class?
Thinking something similar to MaintenanceTasks.parent_controller.
https://github.com/Shopify/maintenance_tasks/blob/d98544fb3de714a14082b64e6831980945db01cf/README.md#customizing-the-parent-controller-for-the-web-ui
What do you think?
Workaround
Just extend SolidQueue::RecurringJob.
module JobErrorHandling
extend ActiveSupport::Concern
included do
rescue_from(Exception) do |exception|
Rails.error.report(exception)
raise exception
end
end
end
class ApplicationJob < ActiveJob::Base
include JobErrorHandling
end
Rails.application.config.after_initialize do
SolidQueue::RecurringJob.include(JobErrorHandling)
end
Yes, good idea! I'll get this done shortly. I could also allow providing the job class to be used in this case π€ Like config.action_mailer.delivery_job.
Thanks for the response π Yes, such an approach would be more flexible. However, we could not determine if it was the optimal solution π€
I have considered the use case of being able to specify our own Job class.
config.solid_queue.recurring_command_job = "MyRecurringCommandJob"
Allows defining processes other than eval(command)
For example, like this
class MyRecurringCommandJob < ActiveJob::Base
def perform(command)
eval(command)
# something...
end
end
I believe the "command" pattern is important for its simplicity, "just execute the command".
In such a case, it would be better to specify class and args as usual, since this is different from the idea of the "command" pattern.
Allows defining config restricted to jobs executed by "command"
For example, like this
class MyRecurringCommandJob < SolidQueue::RecurringJob
# Error handling for "command" only
rescue_from(Exception) do |exception|
# something
raise exception
end
# Collback for "command" only
around_perform :something
# queue for "command" only
queue_as :solid_queue_command_recurring
end
Mostly I would consider error handling equivalent to RecurringTasks that do not utilize "command". The same goes for callbacks and queue.
Allows changing the parent class
For example, like this
class ApplicationJob < ActiveJob::Base
rescue_from(Exception) do |exception|
Rails.error.report(exception)
raise exception
end
end
class MyRecurringCommandJob < ApplicationJob
def perform(command)
eval(command)
end
end
Yes, this is my use case.
This will still accomplish what I want to do, but I need to copy SolidQueue::RecurringJob#perform.
It is a simple copy, but we will need to keep up with future updates to SolidQueue.
Given these considerations, I thought the best solution would be to allow the SolidQueue::RecurringJob parent class to be customized.
However, these are just my own thoughts.
There may be many other use cases that I cannot imagine.
It would be a good choice to allow customization of the Job class to cater to all of them.
It is also community friendly to put action_mailer.delivery_job and settings together.
Also, I am in no hurry to implement the feature, as my use case can be accomplished with βWorkaroundβ as well. I hope you will carefully consider this and choose the better solution.
thanks :+1:
I just noticed that you already have the ability to customize a job class π (I don't know if this is an official config π)
Rails.application.config.after_initialize do # or to_prepare
SolidQueue::RecurringTask.default_job_class = MyRecurringCommandJob
end
Oh wow, yes, it is! π€£ I added this ages ago when I first started implementing this feature. Then, I had to work on other non-solid queue stuff at work, and came back to it recently. I had completely forgotten I had added that! π€£ I also forgot to expose it or document it π
I think this could work! My initial idea was to also allow people to do different things with their command parameter.
So sorry for the delay! π³ I completely forgot about this π I've just documented the default_job_class option that was completely undocumented π
This is great to know I can customize the job_class. I've got a dumb question though... can my "command" just be a model's class method?
Say I've got a model Post and a class method check_urls that should be run as a recurring job. Can my command just be Post.check_urls?
@nimmolo yes! You can totally use that π