Module: Sqewer::SimpleJob
- Defined in:
- lib/sqewer/simple_job.rb
Overview
A module that you can include into your Job class. It adds the following features:
- initialize() will have keyword access to all accessors, and will ensure you have called each one of them
- to_h() will produce a symbolized Hash with all the properties defined using attr_accessor, and the job_class_name
- inspect() will provide a sensible default string representation for logging
This module validates if the attributes defined in the job class are the same as
those persisted in the queue. More details on Sqewer::SimpleJob#initialize
.
Because of this, it's required to create a new job class when adding or removing
an attribute.
This mechanism guarantees strong consistency. Without it, a new deployed job class
could process old incompatible payloads.
Constant Summary collapse
- UnknownJobAttribute =
Class.new(Sqewer::Error)
- MissingAttribute =
Class.new(Sqewer::Error)
- EQ_END =
/(\w+)(\=)$/
Instance Method Summary collapse
-
#initialize(**jobargs) ⇒ Object
Initializes a new Job with the given job args.
-
#inspect ⇒ String
Returns the inspection string with the job and all of it's instantiation keyword attributes.
-
#inspectable_attributes ⇒ Array<Symbol>
Returns the list of methods on the object that have corresponding accessors.
- #to_h ⇒ Object
Instance Method Details
#initialize(**jobargs) ⇒ Object
Initializes a new Job with the given job args. Will check for presence of accessor methods for each of the arguments, and call them with the arguments given.
If one of the accessors was not triggered during the call, an exception will be raised (because you most likely forgot a parameter for a job, or the job class changed whereas the queue still contains jobs in old formats).
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sqewer/simple_job.rb', line 55 def initialize(**) @simple_job_args = .keys touched_attributes = Set.new .each do |(k,v)| accessor = "#{k}=" touched_attributes << k unless respond_to?(accessor) raise UnknownJobAttribute, "Unknown attribute #{k.inspect} for #{self.class}" end send("#{k}=", v) end 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 end |
#inspect ⇒ String
Returns the inspection string with the job and all of it's instantiation keyword attributes.
If inspectable_attributes
has been overridden, the attributes returned by that method will be the
ones returned in the inspection string.
j = SomeJob.new(retries: 4, param: 'a')
j.inspect #=> "<SomeJob:{retries: 4, param: \"a\"}>"
38 39 40 41 42 43 44 45 |
# File 'lib/sqewer/simple_job.rb', line 38 def inspect key_attrs = inspectable_attributes hash_repr = to_h h = key_attrs.each_with_object({}) do |k, o| o[k] = hash_repr[k] end "<#{self.class}:#{h.inspect}>" end |
#inspectable_attributes ⇒ Array<Symbol>
Returns the list of methods on the object that have corresponding accessors. This is then used by #inspect to compose a list of the job parameters, formatted as an inspected Hash.
25 26 27 28 |
# File 'lib/sqewer/simple_job.rb', line 25 def inspectable_attributes # All the attributes that have accessors methods.grep(EQ_END).map{|e| e.to_s.gsub(EQ_END, '\1')}.map(&:to_sym) end |
#to_h ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/sqewer/simple_job.rb', line 78 def to_h keys_and_values = @simple_job_args.each_with_object({}) do |k, h| h[k] = send(k) end keys_and_values end |