Class: Spud::Watch

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spud/watch.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver:, task:, ordered:, named:, watches:) ⇒ Watch

Returns a new instance of Watch.



31
32
33
34
35
36
37
38
39
# File 'lib/spud/watch.rb', line 31

def initialize(driver:, task:, ordered:, named:, watches:)
  @driver = driver
  @task = task
  @ordered = ordered
  @named = named
  @watches = watches

  @last_changed = Time.at(0)
end

Class Method Details

.run!(driver:, task:, ordered:, named:, watches:) ⇒ Object



18
19
20
# File 'lib/spud/watch.rb', line 18

def self.run!(driver:, task:, ordered:, named:, watches:)
  new(driver: driver, task: task, ordered: ordered, named: named, watches: watches).run!
end

Instance Method Details

#latest_watch_changeObject



66
67
68
69
70
71
# File 'lib/spud/watch.rb', line 66

def latest_watch_change
  T.unsafe(Dir)[*@watches]
    .map(&File.method(:stat))
    .map(&:mtime)
    .max
end

#run!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/spud/watch.rb', line 42

def run!
  thread = T.let(nil, T.nilable(Thread))

  loop do
    if watches_changed?
      thread&.kill
      Process.kill('SIGKILL', T.must(@driver.subprocess_pid)) if @driver.subprocess_pid

      @last_changed = latest_watch_change
      thread = Thread.new { @driver.invoke(@task, @ordered, @named) }
    end

    sleep(0.1)
  end
rescue SystemExit, Interrupt => error
  puts "handled interrupt #{error}" if @driver.debug?
end

#watches_changed?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/spud/watch.rb', line 61

def watches_changed?
  @last_changed < latest_watch_change
end