Class: Siding::Watch

Inherits:
Object
  • Object
show all
Defined in:
lib/siding/watch.rb

Defined Under Namespace

Classes: RootEntry, Watcher

Constant Summary collapse

EVENTS =
:events
POLL =
:poll

Class Method Summary collapse

Class Method Details

.mode_from(env, logger: nil) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/siding/watch.rb', line 13

def mode_from(env, logger: nil)
  value = env["SIDING_WATCH"]
  return POLL if value == "poll"
  return EVENTS if value.nil? || value == "events"

  logger&.debug("restarter watch: unrecognized SIDING_WATCH=#{value.inspect}; using events")
  EVENTS
end

.roots_for(manifest) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/siding/watch.rb', line 22

def roots_for(manifest)
  recursive_paths = manifest.directory_entries.map(&:path).uniq

  file_dirs = manifest.file_entries.map { |entry| File.dirname(entry.path) }
  bundle_dirs = manifest.bundle_files.map { |path| File.dirname(path) }
  non_recursive_paths = (file_dirs + bundle_dirs).uniq.reject do |path|
    under_recursive_root?(path, recursive_paths)
  end

  entries = recursive_paths.map { |path| RootEntry.new(path, true) } + non_recursive_paths.map { |path| RootEntry.new(path, false) }
  entries.uniq(&:path).select { |entry| File.directory?(entry.path) }
end

.start(manifest:, env:, logger:, &on_change) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/siding/watch.rb', line 35

def start(manifest:, env:, logger:, &on_change)
  mode = mode_from(env, logger: logger)
  entries = roots_for(manifest)
  return nil if entries.empty?

  begin
    executor = build_executor(entries, on_change, force_polling: mode == POLL)
  rescue StandardError => e
    logger.debug("restarter watch: could not start watchcat (#{e.class}: #{e.message}); using poll")
    return Watcher.unavailable("#{e.class}: #{e.message}")
  end

  Watcher.active(executor, mode)
end