Lookup global max_job_runtime at runtime#436
Conversation
The existing approach set `JobIteration.max_job_runtime` as the default value of the `job_iteration_max_job_runtime` `class_attribute`. However, this approach would close around the value at the time the `Iteration` module was included in the host `class`. This means that if a job class is defined before the host application sets `max_job_runtime`, the setting will not be honoured. For example, if using the `maintenance_tasks` gem, which defines a `TaskJob`, the default would be read when the gem is loaded, prior to initializers running, which is where the host application typically sets the global `max_job_runtime`. This commit removes the static default passed the `class_attribute`, and instead overrides the reader it provides with an implementation that delegates to `JobIteration.max_job_runtime`, ensuring we use the current global value at runtime. It should be noted that this does mean it's possible for the restriction on _increasing_ the `max_job_runtime` to fail to hold: ```ruby class MyJob < ApplicationJob include JobIteration::Iteration self.job_iteration_max_job_runtime = 1.hour # ... end JobIteration.max_job_runtime = 1.minute ``` but this is unlikely to occur in practice outside of jobs classes provided by gems (as the host app's classes would be defined after setting the global max in an initializer), but gems probably should be making decisions about max job runtime anyway.
fafb645 to
e6c3763
Compare
| def job_iteration_max_job_runtime | ||
| # Lookup default every time, instead of closing around its value when the class is defined. | ||
| JobIteration.max_job_runtime | ||
| end |
There was a problem hiding this comment.
If I'm reading this code a month from now, I'm not sure it would be clear to me why we're doing this.
What is the advantage of this method vs doing 'job_iteration_max_job_runtime || JobIteration.max_job_runtimeinjob_should_exit?`
You could even do [job_iteration_max_job_runtime, JobIteration.max_job_runtime].compact.min to enforce that a job can't override the global default.
There was a problem hiding this comment.
+1, I feel like this simplifies the logic, versus having to worry about whether the job-level config is shadowing the global one properly. I don't see that we explicitly mentioned anywhere that job_iteration_max_job_runtime defaults to the value of JobIteration.max_job_runtime, so I don't think changing the way this works would be a breaking change.
| class << base | ||
| prepend(PrependedClassMethods) | ||
|
|
||
| def job_iteration_max_job_runtime |
There was a problem hiding this comment.
I think I'm missing something about this. Wouldn't it always return the default, instead of the override?
Why define this method here, rather than with the other methods added by this class, such as job_should_exit??
There was a problem hiding this comment.
What's doing on here is that we're using class_attribute, and the way it implements "inheritable attributes" is that when you invoke the setter method, it redefines the method on the receiver.
So stepping through it, this is more or less what happens:
class ParentJob < ActiveJob::Base
include JobIteration::Iteration
endThe include causes the included block to run which first invokes
class ParentJob < ActiveJob::Base
class_attribute(
:job_iteration_max_job_runtime,
instance_writer: false,
instance_predicate: false,
)
endwhich basically does
class ParentJob < ActiveJob::Base
class << self
def job_iteration_max_job_runtime
nil # or whatever default is passed in
end
def job_iteration_max_job_runtime=(value)
# ...redefine job_iteration_max_job_runtime to return value...
end
end
endBecause there's no way to pass in a dynamic default (we want it to delegate to JobIteration.max_job_runtime, not use the value at the time the method was defined; that's the bug), the only way to do that is to define the method ourselves, which is what this new part of included does
class ParentJob < ActiveJob::Base
class << self
def job_iteration_max_job_runtime
JobIteration.max_job_runtime
end
end
endFrom there, if a consumer invokes the setter method,
class ChildJob < ParentJob
self.job_iteration_max_job_runtime = 1.minute
endit will redefine the method, "shadowing" the parent one
class ChildJob < ParentJob
class << self
def job_iteration_max_job_runtime
1.minute
end
end
endThe semantics of class_attribute match what we want, it just doesn't allow for a dynamic default, hence why we have to do this.
We can't just put the method in ClassMethods, because the method defined by class_attributes would shadow it, but it does occur to me we could put it in PrependedClassMethods, which should shadow the class_attributes implementation, while still allowing consumers to override it.
There was a problem hiding this comment.
I think part of my confusion comes from class_attribute adding both class and instance accessor methods. This implementation only overrides one of them, but not the other, right?
The existing approach set
JobIteration.max_job_runtimeas the default value of thejob_iteration_max_job_runtimeclass_attribute. However, this approach would close around the value at the time theIterationmodule was included in the hostclass.This means that if a job class is defined before the host application sets
max_job_runtime, the setting will not be honoured. For example, if using themaintenance_tasksgem, which defines aTaskJob, the default would be read when the gem is loaded, prior to initializers running, which is where the host application typically sets the globalmax_job_runtime.This PR removes the static default passed the
class_attribute, and instead overrides the reader it provides with an implementation that delegates toJobIteration.max_job_runtime, ensuring we use the current global value at runtime.See #240 (comment) for context.
It should be noted that this does mean it's possible for the restriction on increasing the
max_job_runtimeto fail to hold:but this is unlikely to occur in practice outside of jobs classes provided by gems (as the host app's classes would be defined after setting the global max in an initializer), but gems probably should be making decisions about max job runtime anyway.