Class: ServerlessRedirector::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/serverless_redirector/syncer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest, destination, logger = ::Logger.new(STDOUT), skip_deletes = false) ⇒ Syncer

Returns a new instance of Syncer.



10
11
12
13
14
15
# File 'lib/serverless_redirector/syncer.rb', line 10

def initialize(manifest, destination, logger = ::Logger.new(STDOUT), skip_deletes = false)
  @manifest = manifest
  @destination = destination
  @logger = logger
  @skip_deletes = skip_deletes
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



8
9
10
# File 'lib/serverless_redirector/syncer.rb', line 8

def destination
  @destination
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/serverless_redirector/syncer.rb', line 8

def logger
  @logger
end

#manifestObject (readonly)

Returns the value of attribute manifest.



8
9
10
# File 'lib/serverless_redirector/syncer.rb', line 8

def manifest
  @manifest
end

#skip_deletesObject (readonly)

Returns the value of attribute skip_deletes.



8
9
10
# File 'lib/serverless_redirector/syncer.rb', line 8

def skip_deletes
  @skip_deletes
end

Instance Method Details

#apply_operations(operations) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/serverless_redirector/syncer.rb', line 51

def apply_operations(operations)
  logger_mutex = Mutex.new
  pool = Concurrent::ThreadPoolExecutor.new min_threads: 1, max_threads: 8, max_queue: 0

  applicator = ServerlessRedirector::Applicator.new(destination, logger)
  operations.each do |operation|
    pool.post { applicator.apply(operation) }
  end
  pool.shutdown
  pool.wait_for_termination
end

#calculate_operationsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/serverless_redirector/syncer.rb', line 34

def calculate_operations
  contents = destination.existing.map { |r| [r.path, r] }.to_h
  targets  = manifest.redirects.map { |r| [r.path, r] }.to_h

  removed = (contents.keys - targets.keys).map { |k| contents[k] }
  added   = (targets.keys - contents.keys).map { |k| targets[k] }
  changed = (targets.keys & contents.keys).select { |k| targets[k] != contents[k] }.map { |k| targets[k] }

  [].tap do |operations|
    unless skip_deletes
      operations.concat removed.map { |r| Operations::RemoveRedirect.new(r) }
    end
    operations.concat added.map { |r| Operations::CreateRedirect.new(r) }
    operations.concat changed.map { |r| Operations::UpdateDestination.new(r) }
  end
end

#debug_operations(operations) ⇒ Object



63
64
65
66
67
68
# File 'lib/serverless_redirector/syncer.rb', line 63

def debug_operations(operations)
  logger.info "Applying #{operations.size} operations:"
  operations.each do |op|
    logger.info "=> #{op.summarize}"
  end
end

#run(dry_run = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/serverless_redirector/syncer.rb', line 17

def run(dry_run = false)
  operations = calculate_operations

  if operations.empty?
    logger.info "Nothing to do, bailing."
    return
  end

  debug_operations operations

  if !dry_run
    logger.info "Applying #{operations.size} operations"
    apply_operations operations
  end

end