Class: Itchy::EventHandlers::BaseEventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/itchy/event_handlers/base_event_handler.rb

Overview

Basic handler implementing required methods. Can be used as a dummy for testing purposes.

Constant Summary collapse

TEMPFILE_BASE =
'vmcatcher_event_metadata_archive'
EVENT_FILE_REGEXP =
/^(?<time>\d+)_(?<type>[[:alnum:]]+)_(?<dc_identifier>[[[:alnum:]]-]+)\.json$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vmcatcher_configuration, options) ⇒ BaseEventHandler

Event handler constructor.



14
15
16
17
18
19
20
21
22
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 14

def initialize(vmcatcher_configuration, options)
  unless vmcatcher_configuration.is_a?(Itchy::VmcatcherConfiguration)
    fail ArgumentError, '\'vmcatcher_configuration\' must be an instance of ' \
                        'Itchy::VmcatcherConfiguration!'
  end

  @vmcatcher_configuration = vmcatcher_configuration
  @options = options || ::Hashie::Mash.new
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 8

def options
  @options
end

#vmcatcher_configurationObject (readonly)

Returns the value of attribute vmcatcher_configuration.



8
9
10
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 8

def vmcatcher_configuration
  @vmcatcher_configuration
end

Instance Method Details

#archive!(vmcatcher_event) ⇒ Object

Triggers an archiving procedure on the registered event.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 27

def archive!(vmcatcher_event)
  unless vmcatcher_event.is_a?(Itchy::VmcatcherEvent)
    fail ArgumentError, '\'vmcatcher_event\' must be an instance of ' \
                        'Itchy::VmcatcherEvent!'
  end

  Itchy::Log.info "[#{self.class.name}] Saving " \
                         "#{vmcatcher_event.type.inspect} " \
                         "for #{vmcatcher_event.dc_identifier.inspect}"

  temp_file = ::Tempfile.new(TEMPFILE_BASE)
  permanent_file_path = ::File.join(
    options.,
    "#{::Time.now.to_i}_#{vmcatcher_event.type || 'Unknown'}_#{vmcatcher_event.dc_identifier || 'NoID'}.json"
  )

  temp_file.write(vmcatcher_event.to_pretty_json)
  temp_file.flush

  ::FileUtils.cp(temp_file.path, permanent_file_path)
  temp_file.close

  true
end

#handle!(vmcatcher_event, _event_file) ⇒ Object

Handling procedure



53
54
55
56
57
58
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 53

def handle!(vmcatcher_event, _event_file)
  unless vmcatcher_event.is_a?(Itchy::VmcatcherEvent)
    fail ArgumentError, '\'vmcatcher_event\' must be an instance of ' \
            'Itchy::VmcatcherEvent!'
        end
end

#save_descriptor(descriptor, name) ⇒ Object

Save created descriptor to descriptor directory.



64
65
66
67
68
69
70
71
72
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 64

def save_descriptor(descriptor, name)
  begin
    File.open("#{@options.descriptor_dir}/#{File.basename(name)}", 'w') { |f| f.write(descriptor) }
  rescue SystemCallError => ex
    Itchy::Log.fatal "[#{self.class.name}] Failed to save a descriptor #{File.basename(name)}. " \
      "Attempt failed with error #{ex.message}"
      fail Itchy::Errors::PrepareEnvError, ex
  end
end