Class: Longleaf::ServiceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/services/service_manager.rb

Overview

Manager which provides preservation service definitions based on their mappings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_manager:, mapping_manager:, app_manager:) ⇒ ServiceManager

Returns a new instance of ServiceManager

Parameters:

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/longleaf/services/service_manager.rb', line 13

def initialize(definition_manager:, mapping_manager:, app_manager:)
  raise ArgumentError.new('Service definition manager required') if definition_manager.nil?
  raise ArgumentError.new('Service mappings manager required') if mapping_manager.nil?
  raise ArgumentError.new('Storage location manager required') if app_manager.nil?
  @definition_manager = definition_manager
  @mapping_manager = mapping_manager
  @app_manager = app_manager
  @service_class_cache = ServiceClassCache.new(app_manager)
end

Instance Attribute Details

#definition_managerObject (readonly)

Returns the value of attribute definition_manager



7
8
9
# File 'lib/longleaf/services/service_manager.rb', line 7

def definition_manager
  @definition_manager
end

#mapping_managerObject (readonly)

Returns the value of attribute mapping_manager



8
9
10
# File 'lib/longleaf/services/service_manager.rb', line 8

def mapping_manager
  @mapping_manager
end

Instance Method Details

#applicable_for_event?(service_name, event) ⇒ Boolean

Determines if a service is applicable for a specific preservation event

Parameters:

  • service_name (String)

    name of the service being evaluated

  • event (String)

    name of the event to check against

Returns:

  • (Boolean)

    true if the service is applicable for the event



61
62
63
# File 'lib/longleaf/services/service_manager.rb', line 61

def applicable_for_event?(service_name, event)
  service(service_name).is_applicable?(event)
end

#list_service_definitions(location: nil, event: nil) ⇒ Array

List definitions for services which are applicable to the given criteria

Parameters:

  • location (String)

    name of the locations to lookup

  • event (String)

    name of the preservation event taking place

Returns:

  • (Array)

    List of service definitions which match the provided criteria



52
53
54
55
# File 'lib/longleaf/services/service_manager.rb', line 52

def list_service_definitions(location: nil, event: nil)
  names = list_services(location: location, event: event)
  names.map { |name| @definition_manager.services[name] }
end

#list_services(location: nil, event: nil) ⇒ Array

List the names of services which are applicable to the given criteria

Parameters:

  • location (String)

    name of the locations to lookup

  • event (String)

    name of the preservation event taking place

Returns:

  • (Array)

    a list of service names which match the provided criteria



38
39
40
41
42
43
44
45
46
# File 'lib/longleaf/services/service_manager.rb', line 38

def list_services(location: nil, event: nil)
  service_names = @mapping_manager.list_services(location)
  if !event.nil?
    # Filter service names down by event
    service_names.select { |name| applicable_for_event?(name, event) }
  else
    service_names
  end
end

#perform_service(service_name, file_rec, event) ⇒ Object

Perform the specified service on the file record, in the context of the specified event

Parameters:

  • service_name (String)

    name of the service

  • file_rec (FileRecord)

    file record to perform service upon

  • event (String)

    name of the event service is being performed within.



97
98
99
100
101
102
# File 'lib/longleaf/services/service_manager.rb', line 97

def perform_service(service_name, file_rec, event)
  definition = @definition_manager.services[service_name]

  service = @service_class_cache.service_instance(definition)
  service.perform(file_rec, event)
end

#service(service_name) ⇒ Object

Return a service instance instance for provided service name.

Parameters:

  • service_name (String)

    name of the service

Returns:

  • Preservation service class for the provided name

Raises:

  • ArgumentError if service_name does not reference an existing service



27
28
29
30
31
32
# File 'lib/longleaf/services/service_manager.rb', line 27

def service(service_name)
  raise ArgumentError.new('Service name is required') if service_name.nil? || service_name.empty?
  raise ArgumentError.new("No service with name #{service_name}") unless @definition_manager.services.key?(service_name)
  definition = @definition_manager.services[service_name]
  @service_class_cache.service_instance(definition)
end

#service_needed?(service_name, md_rec) ⇒ Boolean

Determine if a service should run for a particular file based on the service's definition and the file's service related metadata.

Parameters:

  • service_name (String)

    name of the service being evaluated

  • md_rec (MetadataRecord)

    metadata record for the file being evaluated

Returns:

  • (Boolean)

    true if the service should be run.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/longleaf/services/service_manager.rb', line 70

def service_needed?(service_name, md_rec)
  # If service not recorded for file, then it is needed
  present_services = md_rec.list_services
  return true unless present_services.include?(service_name)

  service_rec = md_rec.service(service_name)

  return true if service_rec.run_needed
  return true if service_rec.timestamp.nil?

  definition = @definition_manager.services[service_name]

  # Check if the amount of time defined in frequency has passed since the service timestamp
  frequency = definition.frequency
  unless frequency.nil?
    service_timestamp = service_rec.timestamp
    now = ServiceDateHelper.formatted_timestamp

    return true if now > ServiceDateHelper.add_to_timestamp(service_timestamp, frequency)
  end
  false
end