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

#execute(only_if_stale: false) ⇒ Integer

Execute the reindex command

Parameters:

  • only_if_stale (boolean) (defaults to: false)

    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