Class: OneacctExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/oneacct_exporter.rb,
lib/oneacct_exporter/log.rb,
lib/oneacct_exporter/version.rb

Overview

Class managing the export

Defined Under Namespace

Modules: Log

Constant Summary collapse

CONVERT_FORMAT =
'%014d'
VERSION =
'0.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, log) ⇒ OneacctExporter

Returns a new instance of OneacctExporter.



20
21
22
23
24
25
26
27
# File 'lib/oneacct_exporter.rb', line 20

def initialize(options, log)
  @log = log
  @range = options[:range]
  @groups = options[:groups]
  @blocking = options[:blocking]
  @timeout = options[:timeout]
  @compatibility = options[:compatibility]
end

Instance Attribute Details

#blockingTrueClass, FalseClass (readonly)

says whether to run export in blocking mode or not

Returns:

  • (TrueClass, FalseClass)

    the current value of blocking



15
16
17
# File 'lib/oneacct_exporter.rb', line 15

def blocking
  @blocking
end

#compatibilityTrueClass, FalseClass (readonly)

says whether to run export in compatibility mode or not

Returns:

  • (TrueClass, FalseClass)

    the current value of compatibility



15
16
17
# File 'lib/oneacct_exporter.rb', line 15

def compatibility
  @compatibility
end

#groupsHash (readonly)

user groups, requesting only virtual machines with owners that belong to one of the group

Returns:

  • (Hash)

    the current value of groups



15
16
17
# File 'lib/oneacct_exporter.rb', line 15

def groups
  @groups
end

#logany logger (readonly)

logger for the class

Returns:

  • (any logger)

    the current value of log



15
16
17
# File 'lib/oneacct_exporter.rb', line 15

def log
  @log
end

#rangeHash (readonly)

range of dates, requesting only virtual machines within the range

Returns:

  • (Hash)

    the current value of range



15
16
17
# File 'lib/oneacct_exporter.rb', line 15

def range
  @range
end

#timeoutInteger (readonly)

timeout for blocking mode

Returns:

  • (Integer)

    the current value of timeout



15
16
17
# File 'lib/oneacct_exporter.rb', line 15

def timeout
  @timeout
end

Instance Method Details

#all_workers_done?Boolean

Check whether all Sidekiq workers have finished thair work

Returns:

  • (Boolean)


92
93
94
# File 'lib/oneacct_exporter.rb', line 92

def all_workers_done?
  Sidekiq::Workers.new.size == 0
end

#clean_output_dirObject

Clean output directory of previous entries



97
98
99
100
101
102
# File 'lib/oneacct_exporter.rb', line 97

def clean_output_dir
  output_dir = Dir.new(Settings.output['output_dir'])
  output_dir.entries.each do |entry|
    File.delete("#{Settings.output['output_dir']}/#{entry}") if /[0-9]{14}/ =~ entry
  end
end

#exportObject

Start export the records



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
# File 'lib/oneacct_exporter.rb', line 30

def export
  @log.debug('Starting export...')

  clean_output_dir

  new_file_number = 1
  batch_number = 0
  oda = OneDataAccessor.new(@compatibility, @log)

  vms = []
  #load records of virtual machines in batches
  while vms = oda.vms(batch_number, @range, @groups)
    output_file = CONVERT_FORMAT % new_file_number
    @log.info("Starting worker with batch number: #{batch_number}.")
    unless vms.empty?
      #add a new job for every batch to the Sidekiq's queue
      OneWorker.perform_async(vms.join('|'), "#{Settings.output['output_dir']}/#{output_file}")
      new_file_number += 1
    end
    batch_number += 1
  end

  @log.info('No more records to read.')

  wait_for_processing if @blocking

  @log.info('Exiting.')
rescue Errors::AuthenticationError, Errors::UserNotAuthorizedError,\
       Errors::ResourceNotFoundError, Errors::ResourceStateError,\
       Errors::ResourceRetrievalError => e
  @log.error("Virtual machine retrieval for batch number #{batch_number} "\
             "failed with error: #{e.message}. Exiting.")
end

#queue_empty?Boolean

Check whether Sidekiq's queue is empty

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
# File 'lib/oneacct_exporter.rb', line 82

def queue_empty?
  queue = (Settings['sidekiq'] && Settings.sidekiq['queue']) ? Settings.sidekiq['queue'] : 'default'
  Sidekiq::Stats.new.queues.each_pair do |queue_name, items_in_queue|
    return items_in_queue == 0 if queue_name == queue
  end

  true
end

#wait_for_processingObject

When in blocking mode, wait for processing of records to finish



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/oneacct_exporter.rb', line 65

def wait_for_processing
  @log.info('Processing...')

  end_time = Time.new + @timeout

  until queue_empty? && all_workers_done?
    if end_time < Time.new
      @log.error("Processing time exceeded timeout of #{@timeout} seconds.")
      break
    end
    sleep(5)
  end

  @log.info('All processing ended.')
end