Class: Itchy::EventHandlers::BaseEventHandler
- Inherits:
-
Object
- Object
- Itchy::EventHandlers::BaseEventHandler
- 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.
Direct Known Subclasses
AvailablePostfixEventHandler, AvailablePrefixEventHandler, ExpirePostfixEventHandler, ExpirePrefixEventHandler, ProcessPostfixEventHandler, ProcessPrefixEventHandler, SubscriptionImageNewEventHandler
Constant Summary collapse
- TEMPFILE_BASE =
'vmcatcher_event_metadata_archive'
- EVENT_FILE_REGEXP =
/^(?<time>\d+)_(?<type>[[:alnum:]]+)_(?<dc_identifier>[[[:alnum:]]-]+)\.json$/
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#vmcatcher_configuration ⇒ Object
readonly
Returns the value of attribute vmcatcher_configuration.
Instance Method Summary collapse
-
#archive!(vmcatcher_event) ⇒ Object
Triggers an archiving procedure on the registered event.
-
#handle!(vmcatcher_event, _event_file) ⇒ Object
Handling procedure.
-
#initialize(vmcatcher_configuration, options) ⇒ BaseEventHandler
constructor
Event handler constructor.
-
#save_descriptor(descriptor, name) ⇒ Object
Save created descriptor to descriptor directory.
- #set_file_permissions(file) ⇒ Object
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, ) unless vmcatcher_configuration.is_a?(Itchy::VmcatcherConfiguration) fail ArgumentError, '\'vmcatcher_configuration\' must be an instance of ' \ 'Itchy::VmcatcherConfiguration!' end @vmcatcher_configuration = vmcatcher_configuration @options = || ::Hashie::Mash.new end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 8 def @options end |
#vmcatcher_configuration ⇒ Object (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 51 |
# 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( ., "#{::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) (permanent_file_path) temp_file.close true end |
#handle!(vmcatcher_event, _event_file) ⇒ Object
Handling procedure
54 55 56 57 58 59 |
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 54 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.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 65 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.}" fail Itchy::Errors::PrepareEnvError, ex end # file permissions for descriptor (File.join(@options.descriptor_dir, File.basename(name))) end |
#set_file_permissions(file) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 77 def (file) Itchy::Log.debug "[#{self.class.name}] Setting file permissions for file #{file} to #{@options.}." begin ::FileUtils.chmod @options..to_i(8), file rescue SystemCallError => ex Itchy::Log.error 'Failed to set permissions!!!' fail Itchy::Errors::PrepareEnvError, ex end end |