Class: ResearchMetadataBatch::Base

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/research_metadata_batch/base.rb

Overview

Note:

Not to be used directly

Instance Method Summary collapse

Methods included from Shared

#act, #init, #preflight, #valid?

Constructor Details

#initialize(pure_config:, log_file: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • log_file (String) (defaults to: nil)


15
16
17
18
19
20
21
22
# File 'lib/research_metadata_batch/base.rb', line 15

def initialize(pure_config:, log_file: nil)
  @pure_config = pure_config
  if log_file
    @logger = Ougai::Logger.new File.new(log_file, 'a'), 20, 'daily'
  else
    @logger = Ougai::Logger.new(STDOUT)
  end
end

Instance Method Details

#process(params: {}, max: nil, delay: 0) ⇒ Object

Parameters:

  • params (Hash) (defaults to: {})

    Combined GET and POST parameters for all records

  • max (Fixnum) (defaults to: nil)

    Number of records to act upon. Omit to act upon as many as possible.

  • delay (Fixnum) (defaults to: 0)

    Delay in seconds between batches.



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/research_metadata_batch/base.rb', line 27

def process(params: {}, max: nil, delay: 0)
  offset = params[:offset]
  records_available = resource_count params
  log_records_available records_available
  begin
    preflight_h = preflight
    @logger.info({preflight: preflight_h}) if preflight_h.is_a?(Hash) && !preflight_h.empty?
  rescue => error
    @logger.error({preflight: error})
  end

  if max
    if max >= 0 && max <= records_available
      qty_to_find = max
    end
  else
    qty_to_find = records_available
  end

  if !offset || offset < 0 || offset > records_available - 1
    offset = 0
  end

  qty_obtained = 0
  position = offset

  while position < records_available
    # extract from Pure
    begin
      params[:offset] = position
      result = resource_batch params
    rescue => error
      @logger.error({metadata_extraction: error})
      sleep 10
      redo
    end

    result.each do |i|

      unless valid? i
        @logger.info log_entry_core(position, i.uuid).merge({valid: false})
        position += 1
        next
      end

      begin
        act_h = act i
        act_log_entry = log_entry_core(position, i.uuid)
        act_log_entry.merge!(act_h) if act_h.is_a?(Hash) && !act_h.empty?
        @logger.info act_log_entry
      rescue => error
        act_error_log_entry = log_entry_core(position, i.uuid)
        act_error_log_entry.merge!({error: error})
        @logger.error act_error_log_entry
      end

      position += 1
      qty_obtained += 1

      break if qty_obtained == qty_to_find
    end

    break if qty_obtained == qty_to_find

    # handle error response
    if result.empty?
      @logger.error({pure_record: position, metadata_extraction: 'No data'})
      position += 1
    end

    sleep delay
  end

  log_records_available records_available

end