Class: Longleaf::RegisterEvent

Inherits:
Object
  • Object
show all
Includes:
EventStatusTracking
Defined in:
lib/longleaf/events/register_event.rb

Overview

Event to register a file with longleaf

Instance Method Summary collapse

Methods included from EventStatusTracking

#record_failure, #record_success, #return_status, #track_failure, #track_status, #track_success

Methods included from Logging

#initialize_logger, initialize_logger, logger, #logger

Constructor Details

#initialize(file_rec:, app_manager:, force: false, digest_provider: nil) ⇒ RegisterEvent

Returns a new instance of RegisterEvent.

Parameters:

  • file_rec (FileRecord)

    file record

  • app_manager (ApplicationConfigManager)

    the application configuration

  • force (boolean) (defaults to: false)

    if true, then already registered files will be re-registered

  • digest_provider (#get_digests) (defaults to: nil)

    object which provides digests for files being registered

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/longleaf/events/register_event.rb', line 18

def initialize(file_rec:, app_manager:, force: false, digest_provider: nil)
  raise ArgumentError.new('Must provide a file_rec parameter') if file_rec.nil?
  raise ArgumentError.new('Parameter file_rec must be a FileRecord') \
      unless file_rec.is_a?(FileRecord)
  raise ArgumentError.new('Must provide an ApplicationConfigManager') if app_manager.nil?
  raise ArgumentError.new('Parameter app_manager must be an ApplicationConfigManager') \
      unless app_manager.is_a?(ApplicationConfigManager)

  @app_manager = app_manager
  @file_rec = file_rec
  @force = force
  @digest_provider = digest_provider
end

Instance Method Details

#performObject

Perform a registration event on the given file

Raises:

  • RegistrationError if a file cannot be registered



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/longleaf/events/register_event.rb', line 34

def perform
  begin
    # Only need to re-register file if the force flag is provided
    if @file_rec. && !@force
      raise RegistrationError.new("Unable to register '#{@file_rec.path}', it is already registered.")
    end

    # create metadata record
    md_rec = MetadataRecord.new(registered: Time.now.utc.iso8601(3))
    @file_rec. = md_rec

    # retain significant details from former record
    if @file_rec.
      retain_existing_properties
    end

    populate_file_properties

    if !@digest_provider.nil?
      checksums = @digest_provider.get_digests(@file_rec.path)
      md_rec.checksums.merge!(checksums) unless checksums.nil?
    end

    # persist the metadata
    @app_manager.md_manager.persist(@file_rec)

    record_success(EventNames::REGISTER, @file_rec.path)
  rescue RegistrationError => err
    record_failure(EventNames::REGISTER, @file_rec.path, err.message)
  rescue InvalidStoragePathError => err
    record_failure(EventNames::REGISTER, @file_rec.path, err.message)
  end

  return_status
end