Skip to content

Sqewer::SimpleJob doesn't help adding new attributes to a Job class #66

@fabioperrella

Description

@fabioperrella

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions