Class: RelationalExporter::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/relational_exporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/relational_exporter.rb', line 14

def initialize(options={})
  @logger = options[:logger] ? options[:logger] : Logger.new(STDERR)

  @connection_config = options[:connection_config]
  begin
    ActiveRecord::Base.establish_connection @connection_config
    ActiveRecord::Base.connection.active?
  rescue Exception => e
    raise "Database connection failed: #{e.message}"
  end

  @schema = Hashie::Mash.new(options[:schema] || YAML.load_file(options[:schema_file]))

  load_models
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/relational_exporter.rb', line 12

def logger
  @logger
end

#schemaObject

Returns the value of attribute schema.



12
13
14
# File 'lib/relational_exporter.rb', line 12

def schema
  @schema
end

Instance Method Details

#export(output_config, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/relational_exporter.rb', line 30

def export(output_config, &block)
  ActiveRecord::Base.logger = @logger
  Celluloid.logger = @logger

  output_config = Hashie::Mash.new output_config

  main_klass = output_config.output.model.to_s.classify.constantize

  main_klass.set_scope_from_hash output_config.output.scope.as_json

  csv_builder = RelationalExporter::CsvBuilder.new output_config.file_path
  Celluloid::Actor[:csv_builder] = csv_builder
  result = csv_builder.future.start
  pool = RelationalExporter::RecordWorker.pool size: 8
  get_headers = true

  record_sequence = -1
  main_klass.find_all_by_scope(output_config.output.scope.as_json).find_in_batches(batch_size: 100) do |records|
    records.each do |record|
      record_sequence += 1

      args = [record_sequence, record, output_config.output.associations, get_headers]
      if get_headers
        pool.get_csv_row(*args)
        get_headers = false
      else
        pool.async.get_csv_row(*args)
      end
    end
  end

  csv_builder.end_index = record_sequence

  @logger.info "CSV export complete" if result.value === true

  pool.terminate
  csv_builder.terminate
end