Class: Sq::Dbsync::BatchLoadAction

Inherits:
LoadAction show all
Defined in:
lib/sq/dbsync/batch_load_action.rb

Overview

Load action to reload an entire table in full. The table will be loaded in parallel to the existing one, then atomically swapped in on completion.

Constant Summary collapse

MAX_LAG =
60 * 5

Constants inherited from LoadAction

LoadAction::EPOCH

Instance Method Summary collapse

Methods inherited from LoadAction

#call, #do_prepare, #initialize, stages, #tag

Constructor Details

This class inherits a constructor from Sq::Dbsync::LoadAction

Instance Method Details

#extract_dataObject



22
23
24
25
26
# File 'lib/sq/dbsync/batch_load_action.rb', line 22

def extract_data
  @start_time  = now.call
  @file, @last_row_at = measure(:extract) { extract_to_file(nil) }
  self
end

#load_dataObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sq/dbsync/batch_load_action.rb', line 28

def load_data
  measure(:load) do
    TempfileFactory.split(@file, 1_000_000, logger) do |path|
      db.load_from_file(
        plan.prefixed_table_name,
        plan.columns,
        path
      )
    end
    @file.close!
  end
  self
end

#operationObject



9
# File 'lib/sq/dbsync/batch_load_action.rb', line 9

def operation; 'batch'; end

#post_loadObject



42
43
44
45
46
47
48
49
50
# File 'lib/sq/dbsync/batch_load_action.rb', line 42

def post_load
  while @start_time <= now.call - MAX_LAG
    @start_time = now.call
    catchup
  end

  switch_tables
  self
end

#prepareObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/sq/dbsync/batch_load_action.rb', line 11

def prepare
  return false if plan.batch_load == false

  if super
    if target.table_exists?(plan.prefixed_table_name)
      target.drop_table(plan.prefixed_table_name)
    end
    true
  end
end