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
end
And we want to add the attribute height:
class MyJob
include Sqewer::SimpleJob
attr_accessor :width, :height
def run
puts 'run'
end
end
By 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}"
end
So, 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
end
This 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, :height
By 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
end
wdyt?
cc @julik @linkyndy @nitika080289 @martijnvermaat @lorenzograndi4
Sometimes, we want to add a new attribute to a Job class, which includes
Sqewer::SimpleJob, but it's not possible becauseSqewer::SimpleJob.newraises an error when it detects that there are missing attributes.Example:
Assuming that exists the following job:
And we want to add the attribute
height:By doing this, when the new version is deployed, if there are old jobs enqueued, it will raise
MissingAttributebecause of this piece of code:So, the only option to add a new attribute is to add a new job, duplicating the previous one, for example:
This 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:
By doing this we can use the new attribute assuming that it may not exist, for example:
wdyt?
cc @julik @linkyndy @nitika080289 @martijnvermaat @lorenzograndi4