Class: Longleaf::ReindexCommand

Inherits:
Object
  • Object
show all
Includes:
EventStatusTracking, Logging
Defined in:
lib/longleaf/commands/reindex_command.rb

Overview

Command for reindexing metadata

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(app_manager) ⇒ ReindexCommand

Returns a new instance of ReindexCommand



11
12
13
14
# File 'lib/longleaf/commands/reindex_command.rb', line 11

def initialize(app_manager)
  @app_manager = app_manager
  @index_manager = @app_manager.index_manager
end

Instance Method Details

#all_storage_locations_selectorObject (private)



86
87
88
89
90
# File 'lib/longleaf/commands/reindex_command.rb', line 86

def all_storage_locations_selector
  storage_loc_names = @app_manager.location_manager.locations.keys

  RegisteredFileSelector.new(storage_locations: storage_loc_names, app_config: @app_manager)
end

#execute(only_if_stale: false) ⇒ Integer

Execute the reindex command

Parameters:

  • only_if_stale (boolean)

    if true, then the reindex command will perform no operation unless the index is stale.

Returns:

  • (Integer)

    status code



19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
# File 'lib/longleaf/commands/reindex_command.rb', line 19

def execute(only_if_stale: false)
  if !@index_manager.using_index?
    record_failure("Cannot perform reindex, no index is configured")
    return return_status
  end

  if only_if_stale && !@index_manager.index_stale?
    record_success("Index is not stale, performing no action")
    return return_status
  end

  start_time = Time.now
  logger.info('Performing full reindex')
  results = nil
  begin
    start_time = Time.now.utc

    selector = all_storage_locations_selector

    # Repopulate the index
    results = index_all(selector)

    # List and then clear all files which were not reindexed
    @index_manager.each_registered_path(selector, older_than: start_time) do |file_path|
      logger.warn("Clearing '#{file_path}' from index, file is no longer present.")
    end
    @index_manager.clear_index(start_time)

    # Update the state of the index to indicate it has been reindexed
    @index_manager.update_index_state
  rescue => err
    record_failure("Encountered error while reindexing", error: err)
  end

  if results['fail'] > 0
    record_success("Completed reindexing, #{results['success']} successful, #{results['fail']} failed.")
  else
    record_success("Completed reindexing, #{results['success']} successful.")
  end

  logger.info("Completed full reindex in #{Time.now - start_time}s")
  return_status
end

#index_all(selector) ⇒ Object (private)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/longleaf/commands/reindex_command.rb', line 64

def index_all(selector)
  count = 0
  failures = 0

  selector.each do |file_path|
    begin
      storage_loc = @app_manager.location_manager.get_location_by_path(file_path)
      file_rec = FileRecord.new(file_path, storage_loc)

      @app_manager.md_manager.load(file_rec)
      @index_manager.index(file_rec)

      record_success("Reindexed #{file_rec.path}")
      count += 1
    rescue LongleafError => err
      record_failure("Failed to reindex #{file_rec.path}: #{err.message}")
      failures += 1
    end
  end
  {'success' => count, 'fail' => failures}
end