Class: CanvasSync::JobBatches::ChainBuilder
- Inherits:
-
Object
- Object
- CanvasSync::JobBatches::ChainBuilder
- Defined in:
- lib/canvas_sync/job_batches/chain_builder.rb
Constant Summary collapse
- VALID_PLACEMENT_PARAMETERS =
%i[before after with].freeze
Instance Attribute Summary collapse
-
#base_job ⇒ Object
readonly
Returns the value of attribute base_job.
Class Method Summary collapse
- ._job_type_definitions ⇒ Object
- .enqueue_job(job_def) ⇒ Object
- .get_chain_parameter(job_def, raise_error: true) ⇒ Object
- .register_chain_job(job_class, chain_parameter, **options) ⇒ Object
Instance Method Summary collapse
- #<<(new_job) ⇒ Object
- #[](key) ⇒ Object
- #get_sub_chain(sub_type) ⇒ Object
-
#initialize(base_type = SerialBatchJob) ⇒ ChainBuilder
constructor
A new instance of ChainBuilder.
- #insert(new_jobs, **kwargs) ⇒ Object
- #insert_at(position, new_jobs) ⇒ Object
-
#merge_options(job, options) ⇒ Object
Legacy Support.
- #normalize!(job_def = self.base_job) ⇒ Object
- #params ⇒ Object
- #process! ⇒ Object
Constructor Details
#initialize(base_type = SerialBatchJob) ⇒ ChainBuilder
Returns a new instance of ChainBuilder.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 8 def initialize(base_type = SerialBatchJob) if base_type.is_a?(Hash) @base_job = base_type else @base_job = { job: base_type, parameters: [], } end end |
Instance Attribute Details
#base_job ⇒ Object (readonly)
Returns the value of attribute base_job.
6 7 8 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 6 def base_job @base_job end |
Class Method Details
._job_type_definitions ⇒ Object
150 151 152 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 150 def _job_type_definitions @job_type_definitions ||= {} end |
.enqueue_job(job_def) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 172 def enqueue_job(job_def) job_class = job_def[:job].constantize = job_def[:parameters] || [] # Legacy Support if job_def[:options] << {} unless [-1].is_a?(Hash) [-1].merge!(job_def[:options]) end if job_class.respond_to? :perform_async job_class.perform_async(*) else job_class.perform_later(*) end end |
.get_chain_parameter(job_def, raise_error: true) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 161 def get_chain_parameter(job_def, raise_error: true) unless _job_type_definitions[job_def[:job].to_s].present? raise "Job Type #{job_def[:job].to_s} does not accept a sub-chain" if raise_error return nil end key = _job_type_definitions[job_def[:job].to_s][:chain_parameter] mapper = ParamsMapper.new(job_def[:parameters]) mapper[key] ||= [] end |
.register_chain_job(job_class, chain_parameter, **options) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 154 def register_chain_job(job_class, chain_parameter, **) _job_type_definitions[job_class.to_s] = { **, chain_parameter: chain_parameter, } end |
Instance Method Details
#<<(new_job) ⇒ Object
36 37 38 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 36 def <<(new_job) insert_at(-1, new_job) end |
#[](key) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 24 def [](key) if key.is_a?(Class) get_sub_chain(key) else @base_job[key] end end |
#get_sub_chain(sub_type) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 88 def get_sub_chain(sub_type) matching_jobs = find_matching_jobs(sub_type) raise "Found multiple \"#{sub_type}\" jobs in the chain" if matching_jobs.count > 1 return nil if matching_jobs.count == 0 new(matching_jobs[0][0]) end |
#insert(new_jobs, **kwargs) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 46 def insert(new_jobs, **kwargs) invalid_params = kwargs.keys - VALID_PLACEMENT_PARAMETERS raise "Invalid placement parameters: #{invalid_params.map(&:to_s).join(', ')}" if invalid_params.present? raise "At most one placement parameter may be provided" if kwargs.values.compact.length > 1 new_jobs = [new_jobs] unless new_jobs.is_a?(Array) if !kwargs.present? insert_at(-1, new_jobs) else placement = kwargs.keys[0] relative_to = kwargs.values[0] matching_jobs = find_matching_jobs(relative_to).to_a raise "Could not find a \"#{relative_to}\" job in the chain" if matching_jobs.count == 0 raise "Found multiple \"#{relative_to}\" jobs in the chain" if matching_jobs.count > 1 relative_job, parent_job, sub_index = matching_jobs[0] needed_parent_type = placement == :with ? ConcurrentBatchJob : SerialBatchJob chain = self.class.get_chain_parameter(parent_job) if parent_job[:job] != needed_parent_type old_job = chain[sub_index] parent_job = chain[sub_index] = { job: needed_parent_type, parameters: [], } sub_index = 0 chain = self.class.get_chain_parameter(parent_job) chain << old_job end if placement == :with chain.insert(-1, *new_jobs) else sub_index += 1 if placement == :after chain.insert(sub_index, *new_jobs) end end end |
#insert_at(position, new_jobs) ⇒ Object
40 41 42 43 44 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 40 def insert_at(position, new_jobs) chain = self.class.get_chain_parameter(base_job) new_jobs = [new_jobs] unless new_jobs.is_a?(Array) chain.insert(-1, *new_jobs) end |
#merge_options(job, options) ⇒ Object
Legacy Support
109 110 111 112 113 114 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 109 def (job, ) find_matching_jobs(job).each do |j, parent, index| j[:options] ||= {} j[:options].deep_merge!() end end |
#normalize!(job_def = self.base_job) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 96 def normalize!(job_def = self.base_job) if job_def.is_a?(ChainBuilder) job_def.normalize! else job_def[:job] = job_def[:job].to_s if (chain = self.class.get_chain_parameter(job_def, raise_error: false)).present? chain.map! { |sub_job| normalize!(sub_job) } end job_def end end |
#params ⇒ Object
32 33 34 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 32 def params ParamsMapper.new(self[:parameters]) end |
#process! ⇒ Object
19 20 21 22 |
# File 'lib/canvas_sync/job_batches/chain_builder.rb', line 19 def process! normalize! self.class.enqueue_job(base_job) end |