Class: Itchy::EventProcesser

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

Overview

Process stored evenets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vmc_configuration, options) ⇒ EventProcesser

Creates and processer instance for stored vmcatcher events.

Parameters:

  • vmc_configuration (Itchy::Vmcatcher::Configuration)

    vmcatcher configuration

  • options (Hashie::Mash)

    hask-like structure with options



10
11
12
13
14
15
16
# File 'lib/itchy/event_processer.rb', line 10

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

  @vmc_configuration = vmc_configuration
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/itchy/event_processer.rb', line 4

def options
  @options
end

#vmc_configurationObject (readonly)

Returns the value of attribute vmc_configuration.



4
5
6
# File 'lib/itchy/event_processer.rb', line 4

def vmc_configuration
  @vmc_configuration
end

Instance Method Details

#archived_events(&block) ⇒ Object

Prepares archived event for processing. That means inspect directory with stored event files, and create VmcatcherEvent from each file.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/itchy/event_processer.rb', line 48

def archived_events(&block)
  arch_events = ::Dir.glob(::File.join(options., '*.json'))
  arch_events.sort!
  Itchy::Log.debug "[#{self.class.name}] Foud events: #{arch_events.inspect}"
  arch_events.each do |json|
    json_short = json.split(::File::SEPARATOR).last

    unless Itchy::EventHandlers::BaseEventHandler::EVENT_FILE_REGEXP =~ json_short
      Itchy::Log.error "[#{self.class.name}] #{json.inspect} doesn't match the required format"
      next
    end

    vmc_event_from_json = read_event(json)
    block.call(vmc_event_from_json, json) if vmc_event_from_json
  end
end

#check_descriptor_dirObject

Checks if description directory exists and its readable.



95
96
97
98
99
100
101
# File 'lib/itchy/event_processer.rb', line 95

def check_descriptor_dir
  Itchy::Log.debug "[#{self.class.name}] Checking root descriptor dir #{options.descriptor_dir.inspect}"
  fail ArgumentError, 'Root descriptor directory' \
                      'is not a directory!' unless File.directory? options.descriptor_dir
  fail ArgumentError, 'Root descriptor directory' \
    'is not readable!' unless File.readable? opitons.descriptor_dir
end

#clean_event!(_event, event_file) ⇒ Object

Deletes event file.

Parameters:

  • event (VmcatcherEvent)

    event for cleaning

  • event_file (String)

    path to file containing event info



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/itchy/event_processer.rb', line 82

def clean_event!(_event, event_file)
  Itchy::Log.info "[#{self.class.name}] Cleaning up"
  Itchy::Log.debug "[#{self.class.name}] Deleting file #{event_file}"

  begin
    ::FileUtils.rm_f event_file
  rescue SystemCallError => ex
    Itchy::Log.error 'Failed to clean up event!!!'
    return ex
  end
end

#process!Object

Processing method for events. For each event calls respective event handler. And after processing of each event, stored event file is deleted.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/itchy/event_processer.rb', line 21

def process!
  Itchy::Log.info "[#{self.class.name}] Processing eventes stored in #{options.}"

  archived_events do |event, event_file|
    begin
      begin
        event_handler = Itchy::EventHandlers.const_get("#{event.type}EventHandler")
        event_handler = event_handler.new(vmc_configuration, options)
        event_handler.handle!(event, event_file)
      rescue Itchy::Errors::EventHandleError => ex
        Itchy::Log.error "[#{self.class.name}] Due to error '#{ex.message}' event '#{event_file}'" \
          " was not processed!!! Aborting."
          fail RuntimeError, "Unprocessed event"
      end

      begin
        clean_event!(event, event_file)
      rescue SystemCallError => ex
        Itchy::Log.error "[#{self.class.name}] Event #{event_file} was processed, but not cleaned!!!"
      end

    end
  end
end

#read_event(json) ⇒ VmcatcherEvent

Creates VmcatcherEvent instance.

of vmcatcher event

Parameters:

  • json (String)

    path to file containing json representation

Returns:



71
72
73
74
75
76
# File 'lib/itchy/event_processer.rb', line 71

def read_event(json)
  Itchy::VmcatcherEvent.new(::File.read(json))
rescue => ex
  Itchy::Log.error 'Failed to load event!!!'
  return false
end