Class: AssetLink::BuilderJob

Inherits:
Struct
  • Object
show all
Defined in:
app/jobs/asset_link/builder_job.rb

Overview

An AssetLink::BuilderJob receives an array of [parent_id, child_id] and builds asset links between them

Returns:

  • []

Direct Known Subclasses

Job

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute links

Returns:

  • (Object)

    the current value of links



5
6
7
# File 'app/jobs/asset_link/builder_job.rb', line 5

def links
  @links
end

Class Method Details

.createObject



28
29
30
# File 'app/jobs/asset_link/builder_job.rb', line 28

def self.create(*)
  Delayed::Job.enqueue(new(*))
end

.create_nowObject



32
33
34
# File 'app/jobs/asset_link/builder_job.rb', line 32

def self.create_now(*)
  new(*).perform
end

Instance Method Details

#performObject

rubocop:todo Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/jobs/asset_link/builder_job.rb', line 7

def perform # rubocop:todo Metrics/MethodLength
  # For memory reasons we need to limit transaction size to 10 links at a time
  transaction_count = 10

  links
    .uniq
    .each_slice(transaction_count) do |link_group|
      ActiveRecord::Base.transaction do
        link_group.each do |parent, child|
          # Create edge can accept either a model (which it converts to an endpoint) or
          # an endpoint itself. Using the endpoints directly we avoid the unnecessary
          # database calls, but more importantly avoid the need to instantiate a load of
          # active record objects.
          parent_endpoint = Dag::Standard::EndPoint.new(parent)
          child_endpoint = Dag::Standard::EndPoint.new(child)
          AssetLink.create_edge(parent_endpoint, child_endpoint)
        end
      end
    end
end