-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Sometimes, we want to add a new attribute to a Job class, which includes Sqewer::SimpleJob, but it's not possible because Sqewer::SimpleJob.new raises an error when it detects that there are missing attributes.
Example:
Assuming that exists the following job:
class MyJob
include Sqewer::SimpleJob
attr_accessor :width
def run
puts 'run'
end
endAnd we want to add the attribute height:
class MyJob
include Sqewer::SimpleJob
attr_accessor :width, :height
def run
puts 'run'
end
endBy doing this, when the new version is deployed, if there are old jobs enqueued, it will raise MissingAttribute because of this piece of code:
# lib/sqewer/simple_job.rb
accessors = methods.grep(EQ_END).map{|method_name| method_name.to_s.gsub(EQ_END, '\1').to_sym }
settable_attributes = Set.new(accessors)
missing_attributes = settable_attributes - touched_attributes
missing_attributes.each do | attr |
raise MissingAttribute, "Missing job attribute #{attr.inspect}"
endSo, the only option to add a new attribute is to add a new job, duplicating the previous one, for example:
class MyJobV2
include Sqewer::SimpleJob
attr_accessor :width, :height
def run
puts 'run'
end
endThis has the advantage of being safer when changing the job's payload, but it complicates the analysis if the PR because a whole new file has been added.
My suggestion is adding a method to allow changing this behaviour, such as:
class MyJob
include Sqewer::SimpleJob
allow_missing_attributes
attr_accessor :width, :heightBy doing this we can use the new attribute assuming that it may not exist, for example:
class MyJob
include Sqewer::SimpleJob
allow_missing_attributes # <-- new method
attr_accessor :width, :height
def run
if height.present?
# do something
end
end
endwdyt?
cc @julik @linkyndy @nitika080289 @martijnvermaat @lorenzograndi4