Class: KManager::Watcher

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/k_manager/watcher.rb

Overview

NOTE: Does Watcher belong in this GEM?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watch_folder, boot_file) ⇒ Watcher

Returns a new instance of Watcher.



14
15
16
17
# File 'lib/k_manager/watcher.rb', line 14

def initialize(watch_folder, boot_file)
  @watch_folder = watch_folder
  @boot_file = boot_file
end

Instance Attribute Details

#boot_fileObject (readonly)

Returns the value of attribute boot_file.



12
13
14
# File 'lib/k_manager/watcher.rb', line 12

def boot_file
  @boot_file
end

#watch_folderObject (readonly)

Returns the value of attribute watch_folder.



11
12
13
# File 'lib/k_manager/watcher.rb', line 11

def watch_folder
  @watch_folder
end

Instance Method Details

#startObject

rubocop:disable Lint/RescueException, Metrics/AbcSize, Metrics/MethodLength



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
62
63
64
65
66
67
68
# File 'lib/k_manager/watcher.rb', line 24

def start
  boot_up

  update_dashboard

  watcher = Filewatcher.new(watch_folder)

  watcher.watch do |changes|
    watcher.pause
    if changes.length > 1
      log.kv 'HOW MANY CHANGES', changes.length
      log.block changes
    end

    changes.each do |filename, event|
      # NOTE: KManager will not support space in file name, but this will at least deal with file copies, where " copy" is added to a file name.
      filename = filename.gsub(' ', '%20')

      log_file_event(event, filename)

      uri = URI::File.build(host: nil, path: filename)

      start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      KManager.resource_changed(uri, event)
      finish_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      time_taken = finish_time - start_time

      sleep KManager.opts.sleep if KManager.opts.sleep.positive?

      clear_screen

      log_file_event(event, filename)
      log.kv 'Time Taken', time_taken.to_d.round(3, :truncate).to_f if KManager.opts.show.time_taken

      update_dashboard

      puts KManager.opts.show.finished_message if KManager.opts.show.finished
    end
    watcher.resume
  end
rescue Interrupt, SystemExit
  raise
rescue Exception => e
  log.exception(e)
end