Class: CanvasSync::Jobs::TermBatchesJob

Inherits:
CanvasSync::Job show all
Defined in:
lib/canvas_sync/jobs/term_batches_job.rb

Instance Attribute Summary

Attributes inherited from CanvasSync::Job

#job_log

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CanvasSync::Job

#create_job_log, #report_checker_wait_time, #update_or_create_model

Class Method Details

.batch_finished(status, opts) ⇒ Object



34
35
36
37
38
# File 'lib/canvas_sync/jobs/term_batches_job.rb', line 34

def self.batch_finished(status, opts)
  ctx = JobBatches::Batch.current_context
  term_id = ctx[:canvas_term_id]
  CanvasSync.redis.set(last_sync_key(term_id), ctx[:batch_start_time])
end

.last_sync_key(term_id) ⇒ Object



40
41
42
43
# File 'lib/canvas_sync/jobs/term_batches_job.rb', line 40

def self.last_sync_key(term_id)
  ctx = JobBatches::Batch.current_context
  "#{CanvasSync.redis_prefix}:#{ctx[:batch_genre]}:#{term_id}:last_sync"
end

Instance Method Details

#get_term_id(term) ⇒ Object



45
46
47
# File 'lib/canvas_sync/jobs/term_batches_job.rb', line 45

def get_term_id(term)
  term.try(:canvas_id) || term.canvas_term_id
end

#perform(options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/canvas_sync/jobs/term_batches_job.rb', line 4

def perform(options)
  if (jobs = options[:sub_jobs]).present?
    context = options[:context] || {}
    if options[:term_scope]
      Term.send(options[:term_scope]).find_each.map do |term|
        term_id = get_term_id(term)
        local_context = context.merge(canvas_term_id: term_id)

        # Override the delta-syncing date if:
        # 1. the Term hasn't been synced before or
        # 2. the Term underwent a period of not syncing
        term_last_sync = CanvasSync.redis.get(self.class.last_sync_key(term_id))
        if batch_context[:updated_after]
          if !term_last_sync.present? || batch_context[:updated_after] > term_last_sync
            local_context[:updated_after] = term_last_sync.presence
          end
        end

        JobBatches::ManagedBatchJob.make_batch(jobs, ordered: false, concurrency: true) do |b|
          b.description = "TermBatchJob(#{term_id}) Root"
          b.context = local_context
          b.on(:success, "#{self.class.to_s}.batch_finished") unless options[:mark_synced] == false
        end
      end
    else
      JobBatches::ConcurrentBatchJob.make_batch(jobs, context: context)
    end
  end
end