Class: Itchy::EventProcesser
- Inherits:
-
Object
- Object
- Itchy::EventProcesser
- Defined in:
- lib/itchy/event_processer.rb
Overview
Process stored evenets.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#vmc_configuration ⇒ Object
readonly
Returns the value of attribute vmc_configuration.
Instance Method Summary collapse
-
#archived_events(&block) ⇒ Object
Prepares archived event for processing.
-
#check_descriptor_dir ⇒ Object
Checks if description directory exists and its readable.
-
#clean_event!(_event, event_file) ⇒ Object
Deletes event file.
-
#initialize(vmc_configuration, options) ⇒ EventProcesser
constructor
Creates and processer instance for stored vmcatcher events.
-
#process! ⇒ Object
Processing method for events.
-
#read_event(json) ⇒ VmcatcherEvent
Creates VmcatcherEvent instance.
Constructor Details
#initialize(vmc_configuration, options) ⇒ EventProcesser
Creates and processer instance for stored vmcatcher events.
10 11 12 13 14 15 16 |
# File 'lib/itchy/event_processer.rb', line 10 def initialize(vmc_configuration, ) fail ArgumentError, '"vmc_configuration" must be an instance ' \ 'of Itchy::VmcatcherConfiguration' unless vmc_configuration.is_a? Itchy::VmcatcherConfiguration @vmc_configuration = vmc_configuration @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/itchy/event_processer.rb', line 4 def @options end |
#vmc_configuration ⇒ Object (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(., '*.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_dir ⇒ Object
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 #{.descriptor_dir.inspect}" fail ArgumentError, 'Root descriptor directory' \ 'is not a directory!' unless File.directory? .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.
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 #{.}" archived_events do |event, event_file| begin begin event_handler = Itchy::EventHandlers.const_get("#{event.type}EventHandler") event_handler = event_handler.new(vmc_configuration, ) event_handler.handle!(event, event_file) rescue Itchy::Errors::EventHandleError => ex Itchy::Log.error "[#{self.class.name}] Due to error '#{ex.}' 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
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 |