Class: OneacctExporter
- Inherits:
-
Object
- Object
- OneacctExporter
- Defined in:
- lib/oneacct_exporter.rb,
lib/oneacct_exporter/log.rb,
lib/oneacct_exporter/version.rb
Overview
Class managing the export
belong to one of the group mode or not
Defined Under Namespace
Modules: Log
Constant Summary collapse
- VERSION =
'0.5.0'
Instance Attribute Summary collapse
-
#blocking ⇒ TrueClass, FalseClass
readonly
says whether to run export in blocking mode or not.
-
#compatibility ⇒ TrueClass, FalseClass
readonly
says whether to run export in compatibility.
-
#groups ⇒ Hash
readonly
user groups, requesting only virtual machines with owners that.
-
#log ⇒ any logger
readonly
logger for the class.
-
#range ⇒ Hash
readonly
range of dates, requesting only virtual machines within the range.
-
#timeout ⇒ Integer
readonly
timeout for blocking mode.
Instance Method Summary collapse
-
#all_workers_done? ⇒ Boolean
Check whether all Sidekiq workers have finished thair work.
-
#clean_output_dir ⇒ Object
Clean output directory of previous entries.
-
#export ⇒ Object
Start export the records.
-
#initialize(options, log) ⇒ OneacctExporter
constructor
A new instance of OneacctExporter.
-
#queue_empty? ⇒ Boolean
Check whether Sidekiq's queue is empty.
-
#wait_for_processing ⇒ Object
When in blocking mode, wait for processing of records to finish.
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(, log) @log = log @range = [:range] @groups = [:groups] @blocking = [:blocking] @timeout = [:timeout] @compatibility = [:compatibility] end |
Instance Attribute Details
#blocking ⇒ TrueClass, FalseClass (readonly)
says whether to run export in blocking mode or not
17 18 19 |
# File 'lib/oneacct_exporter.rb', line 17 def blocking @blocking end |
#compatibility ⇒ TrueClass, FalseClass (readonly)
says whether to run export in compatibility
17 18 19 |
# File 'lib/oneacct_exporter.rb', line 17 def compatibility @compatibility end |
#groups ⇒ Hash (readonly)
user groups, requesting only virtual machines with owners that
17 18 19 |
# File 'lib/oneacct_exporter.rb', line 17 def groups @groups end |
#log ⇒ any logger (readonly)
logger for the class
17 18 19 |
# File 'lib/oneacct_exporter.rb', line 17 def log @log end |
#range ⇒ Hash (readonly)
range of dates, requesting only virtual machines within the range
17 18 19 |
# File 'lib/oneacct_exporter.rb', line 17 def range @range end |
#timeout ⇒ Integer (readonly)
timeout for blocking mode
17 18 19 |
# File 'lib/oneacct_exporter.rb', line 17 def timeout @timeout end |
Instance Method Details
#all_workers_done? ⇒ Boolean
Check whether all Sidekiq workers have finished thair work
89 90 91 |
# File 'lib/oneacct_exporter.rb', line 89 def all_workers_done? Sidekiq::Workers.new.size == 0 end |
#clean_output_dir ⇒ Object
Clean output directory of previous entries
94 95 96 97 98 99 100 |
# File 'lib/oneacct_exporter.rb', line 94 def clean_output_dir output_dir = Dir.new(Settings.output['output_dir']) entries = output_dir.entries.select { |entry| entry != '.' && entry != '..' } entries.each do |entry| File.delete("#{output_dir.path}/#{entry}") end end |
#export ⇒ Object
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 |
# File 'lib/oneacct_exporter.rb', line 30 def export @log.debug('Starting export...') clean_output_dir new_file_number = 1 oda = OneDataAccessor.new(@compatibility, @log) vms = [] # load records of virtual machines in batches while vms = oda.vms(@range, @groups) unless vms.empty? @log.info("Starting worker with next batch.") # add a new job for every batch to the Sidekiq's queue OneWorker.perform_async(vms.join('|'), new_file_number) new_file_number += 1 end 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 "\ "failed with error: #{e.}. Exiting.") end |
#queue_empty? ⇒ Boolean
Check whether Sidekiq's queue is empty
79 80 81 82 83 84 85 86 |
# File 'lib/oneacct_exporter.rb', line 79 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_processing ⇒ Object
When in blocking mode, wait for processing of records to finish
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/oneacct_exporter.rb', line 62 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 |