Class: TurbotRunner::Processor

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

Instance Method Summary collapse

Constructor Details

#initialize(runner, script_config, record_handler) ⇒ Processor

Returns a new instance of Processor.



5
6
7
8
9
10
11
# File 'lib/turbot_runner/processor.rb', line 5

def initialize(runner, script_config, record_handler)
  @runner = runner
  @data_type = script_config[:data_type]
  @identifying_fields = script_config[:identifying_fields]
  @record_handler = record_handler
  @seen_uids = script_config[:duplicates_allowed] ? nil : Set.new
end

Instance Method Details

#interruptObject



48
49
50
# File 'lib/turbot_runner/processor.rb', line 48

def interrupt
  @runner.interrupt
end

#process(line, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/turbot_runner/processor.rb', line 13

def process(line, opts={})
  validate = opts[:validate].nil? ? true : opts[:validate]
  begin
    if line.strip == "SNAPSHOT ENDED" || line.strip == "RUN ENDED" # latter is legacy
      @record_handler.handle_snapshot_ended(@data_type)
      @runner.interrupt if @runner
    else
      record = Openc::JsonSchema.convert_dates(schema_path, JSON.parse(line))
      error_message = nil
      if validate
        error_message = Validator.validate(
          @data_type,
          record,
          @identifying_fields,
          @seen_uids
        )
      end

      if error_message.nil?
        begin
          @record_handler.handle_valid_record(record, @data_type)
        rescue InterruptRun
          @runner.interrupt if @runner
        end
      else
        @record_handler.handle_invalid_record(record, @data_type, error_message)
        @runner.interrupt_and_mark_as_failed if @runner
      end
    end
  rescue JSON::ParserError
    @record_handler.handle_invalid_json(line)
    @runner.interrupt_and_mark_as_failed if @runner
  end
end

#schema_pathObject



52
53
54
# File 'lib/turbot_runner/processor.rb', line 52

def schema_path
  TurbotRunner.schema_path(@data_type)
end