Class: CanvasSync::Jobs::BeginSyncChainJob

Inherits:
CanvasSync::Job
  • Object
show all
Defined in:
lib/canvas_sync/jobs/begin_sync_chain_job.rb

Instance Attribute Summary collapse

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

Instance Attribute Details

#globalsObject (readonly)

Returns the value of attribute globals.



4
5
6
# File 'lib/canvas_sync/jobs/begin_sync_chain_job.rb', line 4

def globals
  @globals
end

Class Method Details

.batch_completed(status, options) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/canvas_sync/jobs/begin_sync_chain_job.rb', line 90

def self.batch_completed(status, options)
  sbatch = SyncBatch.find(options['sync_batch_id'])
  sbatch.update!(
    status: status.success? ? 'completed' : 'failed',
    completed_at: DateTime.now,
  )
end

Instance Method Details

#genreObject



86
87
88
# File 'lib/canvas_sync/jobs/begin_sync_chain_job.rb', line 86

def genre
  globals[:batch_genre] || "default"
end

#last_full_syncObject



82
83
84
# File 'lib/canvas_sync/jobs/begin_sync_chain_job.rb', line 82

def last_full_sync
  last_full_sync_record&.started_at
end

#last_full_sync_recordObject



78
79
80
# File 'lib/canvas_sync/jobs/begin_sync_chain_job.rb', line 78

def last_full_sync_record
  @last_full_sync_record ||= SyncBatch.where(status: ['completed', 'processing'], full_sync: true, batch_genre: genre).last
end

#perform(chain_definition, globals = {}) ⇒ Object



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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/canvas_sync/jobs/begin_sync_chain_job.rb', line 6

def perform(chain_definition, globals = {})
  @globals = globals

  if globals[:updated_after] == nil && Rails.env.development?
    globals[:updated_after] = false
  end

  if globals[:updated_after] == false
    globals[:updated_after] = nil
  elsif !globals[:updated_after].present? || globals[:updated_after] == true
    last_batch = SyncBatch.where(status: 'completed', batch_genre: genre).last
    globals[:full_sync_every] ||= "sunday/2"
    globals[:updated_after] = ((last_batch.started_at - 1.day).iso8601 rescue nil)
  end

  # Refuse to run syncs of the same genre if there is a running full sync
  if last_full_sync_record&.status == 'processing' && last_full_sync > 12.hours.ago
    Rails.logger.warn("Attempted to start a '#{genre}' sync while a full-sync is still processing.")
    return
  end

  if should_full_sync?(globals[:full_sync_every])
    globals[:updated_after] = nil
  end

  sync_batch = SyncBatch.create!(
    started_at: DateTime.now,
    full_sync: globals[:updated_after] == nil,
    batch_genre: genre,
    status: 'processing',
  )

  globals[:batch_genre] = genre
  globals[:batch_start_time] = sync_batch.started_at.iso8601
  globals[:sync_batch_id] = sync_batch.id

  JobBatches::Batch.new.tap do |b|
    b.description = "CanvasSync Root Batch (SyncBatch##{sync_batch.id})"
    b.on(:complete, "#{self.class.to_s}.batch_completed", sync_batch_id: sync_batch.id)
    b.on(:success, "#{self.class.to_s}.batch_completed", sync_batch_id: sync_batch.id)
    b.context = globals
    b.jobs do
      JobBatches::SerialBatchJob.perform_now(chain_definition)
    end
    sync_batch.update(batch_bid: b.bid)
  end
end

#should_full_sync?(opt) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/canvas_sync/jobs/begin_sync_chain_job.rb', line 54

def should_full_sync?(opt)
  return true unless last_full_sync.present?
  return false unless opt.is_a?(String)

  case opt.strip
  when %r{^(sunday|monday|tuesday|wednesday|thursday|friday|saturday)(?:/(\d+))?$}
    m = Regexp.last_match
    day = m[1]
    skip = m[2] || "1"
    DateTime.now.send(:"#{day}?") && last_full_sync.end_of_day <= (skip.to_i.weeks.ago.end_of_day)
  when %r{^(\d+)\%$}
    m = Regexp.last_match
    rand(100) < m[1].to_i
  when %r{^(\d+) ?days$}
    m = Regexp.last_match
    last_full_sync.end_of_day <= m[1].to_i.days.ago.end_of_day
  when %r{^(\d+)$} # N.days is converted to a string of seconds
    m = Regexp.last_match
    last_full_sync.end_of_day <= m[1].to_i.seconds.ago.end_of_day
  else
    false
  end
end